Cocos2d portrait mode not working on iPhone - iphone

I'm building a cocos2d game which is supposed to be in portrait mode. I changed the RootViewController.m to portrait mode:
return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );
Everything works fine, both on the simulator and on my iPad. However, when I run the game on my iPhone, it defaults back to landscape mode.
Any ideas on how to fix this?
Thanks.

in ur app delegate.. Put this line:
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
after
[director setAnimationInterval:1.0/30];
[director setDisplayFPS:YES];

Related

convert game from portrait mode to landscape mode

I am new to cocos2d game developing. Here, my game is developed in portrait mode. I just want to convert landscape mode. and tell me which method is used instead of shouldAutorotateToInterfaceOrientation method because this method is not available in iOS 6?
My whole game is developed portrait mode from the start.
Thank you in advance.
you will find below code in appdelegate.mm file
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
change it to.
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
let me know it is working or not!!!!
Not sure this will solve your problem, but as you mentioned How to handle orientation in OS6, I would like to give you the code what I am using for Handling Device Orientation in iOS 6 and iO5 etc.
So here is the code snip, where you can decide what orientation support you want in your app
Handle Screen Orientations in iOS 5 and iOS 6
//For up-to iOS 5.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported all orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//For iOS 6.0
-(NSInteger)supportedInterfaceOrientations
{
//Supporting only portrait orientation.
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate
{
return NO;
}
Reference
Hope this will help you!!
Go to target and set supported interface orientation.

How to launch a cocos2D game from single view app

I created a regular simple iOS app (a single view application) with one button and I want to launch a cocos2D game from the iOS app. In other words, I want to launch the cocos2D game when I press on the button from my myapp.xib. I don't want to use URL schemes to launch the game because that will require the user to download my app as well as download the game. I want the user to be able to launch the game from my app internally. This is the game I want to launch from my app:
http://www.raywenderlich.com/14439/how-to-make-a-game-like-fruit-ninja-with-box2d-and-cocos2d-part-3
I was able to successfully add this game as a dependency in my xcode project. However, I'm not sure how to go about launching the game from app.
Here's some ideas that I had, but they didn't really work for me.
Is there any way I can:
Call the app delegate of the game (not the app delegate of my app) from the IBAction in my app to launch the game?
Call the didFinishLaunchingWithOptions method of the game (not the didFinishLaunchingWithOptions of my app) from the IBAction in my app to launch the game?
Call the main.m file of the game (not the main.m of my app) from the IBAction in my app to launch the game?
From what I understand, these are three different ways of launching an iOS app. Keep in mind that I am developing an app (not a game) that will allow a user to launch the game above internally through the app. Ideally, it would nice (and also easy) if I can simply do a pushviewcontroller from my app to the game, but I'm not sure if there is an easy way to approach this.
Is there anyway I can launch this game internally through my app? Any advice, suggestions, sample source code would be greatly appreciated.
Short answer: YES
You would need to have just one AppDelegate (the one of your iOS app), and move there all the cocos2D init stuff. Then, you can launch the game from your IBAction with something like this.-
CCDirector *director = [CCDirector sharedDirector];
[director pushScene:[YourFirstGameScene node]];
[director resume];
[self presentModalViewController:director animated:YES];
Please, take next snippet as an example to init/end cocos2d from your appDelegate
- (void) initCocos {
// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
CCGLView *glView = [CCGLView viewWithFrame:[self.window bounds]
pixelFormat:kEAGLColorFormatRGBA8 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
self.director = (CCDirectorIOS*) [CCDirector sharedDirector];
self.director.wantsFullScreenLayout = YES;
// Display FSP and SPF
[self.director setDisplayStats:NO];
// set FPS at 60
[self.director setAnimationInterval:1.0/60];
// attach the openglView to the director
[self.director setView:glView];
// for rotation and other messages
[self.director setDelegate:self];
// 2D projection
[self.director setProjection:kCCDirectorProjection2D];
// [director setProjection:kCCDirectorProjection3D];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [self.director enableRetinaDisplay:NO] )
CCLOG(#"Retina Display Not supported");
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// When in iPhone RetinaDisplay, iPad, iPad RetinaDisplay mode, CCFileUtils will append the "-hd", "-ipad", "-ipadhd" to all loaded files
// If the -hd, -ipad, -ipadhd files are not found, it will load the non-suffixed version
//[CCFileUtils setiPhoneRetinaDisplaySuffix:#"-hd"]; // Default on iPhone RetinaDisplay is "-hd"
[CCFileUtils setiPadSuffix:#""]; // Default on iPad is "" (empty string)
//[CCFileUtils setiPadRetinaDisplaySuffix:#"-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"
// Assume that PVR images have premultiplied alpha
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
}
- (void) endCocos {
CC_DIRECTOR_END();
self.director = nil;
}
Actually, what I do is a call to initCocos just before pushing my director, and endCocos just after closing the game, like
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[[CCDirector sharedDirector] dismissModalViewControllerAnimated:YES];
[appDelegate endCocos];
Hope it helps.

UIWebView - YouTube Video Landscape mode - iOS6

I want to play a YouTube Video in a UIWebView in fullscreen (Landscape mode)
But all my other views to be only in Portrait mode. How is it possible that only the YouTube video is in landscape mode?
Do I have to enable the landscape mode in the app targets summary?
Thanks for help! :-)
If your app is using multiple orientations then you need to define them in the Project Target summary. e.g Portrait, Landscape left & Landscape right.
If you are having your device working for iOS5, use the following method(deprecated in iOS6) for rotating to particular orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
If you are having your device working on iOS6. you should use the following methods.
– shouldAutorotate – supportedInterfaceOrientations & – preferredInterfaceOrientationForPresentation
If your app supporting both versions then you could keep the both methods and check if your view controller is responding to the particular method by
if ([self respondsToSelector:#selector(methodToCheck)])
myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57079633);
[myTableView setTransform:rotate];
self.view = myTableView;
This will also work*
make sure here i have done in tableview

MPMoviePlayerViewController not playing in Landscape orientation under 5.1

Has anything changed in 5.1 which would affect how a MPMoviePlayerViewController works regarding device orientation?
I started getting reports from users today that videos were playing only in portrait mode. I figured out that they were using 5.1 and I quickly upgraded a device to recreate the situation. My code has not changed and works perfectly in 4.x, 5.0, and 5.01.
All the views in my app display in portrait mode except when a user clicks on a video, the movie player is suppose to take over the whole screen and launch into landscape more. The app using the 5.0 SDK but targeting 4.0. Here is the code I am using to display a video:
VideoPlayer *vp = [[VideoPlayer alloc] initWithContentURL:movieURL];
vp.moviePlayer.movieSourceType = src;
vp.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
vp.moviePlayer.shouldAutoplay = TRUE;
[self presentMoviePlayerViewControllerAnimated:vp];
VideoPlayer is a subclass of MPMoviePlayerViewController where the shouldAutorotateToInterfaceOrientation is overridden like so:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIDeviceOrientationLandscapeLeft);
}
This pattern is recommended all over the internet and even by Apple. I don't understand why its not working under iOS 5.1 or why more people aren't complaining about this.
Any help will be greatly appreciated.
I had the same problem also - i was playing the movie on a opengl subview, (im making an interactive ebook in landscape mode so needed my movie - (in a uiview) to play in landscape also)
I corrected this by:
subclassing the open glview to a *viewcontroller then linking that *viewcontroller to the window
So while working with cocos2d i can now use all uikit in the correct orientation.
Sending all uikit views to my subclasses opengl view. (while making sure to add in my app delegate and checking that orientation is stated in plist too.)
"#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
"#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeRight];
"#endif
hope this helps someone :) im very new at cocos2d so it took a while to figure out what i was doing wrong.
I had the same issue in iOS 5. The only way I was able to get it to work was to subclass MPMoviePlayerViewController.
#implementation MovieViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
} else {
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
#end
It looks you have already tried to do this, but this block of code is working for me on the device with iOS 5.1. Not sure about the simulator.
I had a bunch of orientation issues after upgrading to iOS 5.1. For me, it was because the allowed orientations of sibling viewcontrollers up the chain resulted in no allowable orientation for a modal controller I was adding.
Do you have any cases in your view hierarchy where two subviews are added to a view? I was adding two subviews to my window in applicationDidFinishLaunching, and before iOS 5.1, they could have independent allowable orientations. ie, I could have one fixed in portrait orientation while the one on top rotated. Now, the other subview insists on portrait orientation.
My solution was to force the non-rotating view below:
[self.window insertSubview:self.nonRotatingViewController.view belowSubview:self.rotatingViewController.view];
This post helped me figure that out (and has some code):
iOS: Disable Autorotation for a Subview

Cocos2d - Setting Device/Screen Orientation

I am new to the cocos2d API and have noticed that there are a few ways to set the screens orientation within the templates. I have not been able to figure out the correct way to set my orientation to LandscapeRight and keep it that way throughout the entire game. How do I change my orientation so that it maintains LandscapeRight? Any help is appreciated. Thank you!
The answer here has changed with cocos2d 2.0, as CCDirector is now a ViewController on iOS:
CCDirector no longer supports device orientation. All autorotation and
device orientation is handled by RootViewController now. Fortunately,
[[UIDevice currentDevice] orientation] can be used in place of
[[CCDirector sharedDirector] deviceOrientation]. The enums are the
same, except that they begin with UI instead of CC.
Forcing a specific orientation is a simple matter of returning YES
only to the desired orientation in the RootViewController method
shouldAutorotateToInterfaceOrientation.
Choosing between Cocos2D v1.x & 2.x and Tips for updating to Cocos2D 2.0 at learn-cocos2d.com
Modify GameConfig.h from the cocos2d template.
#define GAME_AUTOROTATION kGameAutorotationNone
/* original code is kGameAutorotationUIViewController. */
And modify AppDelegate.m as well.
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeRight];
/* original code is "Left". */
#endif
Use this line:
[[CCDirector sharedDirector] setDeviceOrientation:kkCCDeviceOrientationLandscapeRight];
In the RootViewController.m,search for the line
return ( UIInterfaceOrientationIsPortrait(interfaceOrientation ));
change it to
return ( UIInterfaceOrientationIsLandscape(interfaceOrientation ));
if you added shouldAutorotateToInterfaceOrientation and not solved your problem
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
THEN
Try to add this line to appDelegate.m
[window_ setRootViewController:navController_];
Good Luck
The correct answer - took me a while to find - is in the info.plist, change the supported interface orientations values, item 0 and item 1 have 4 possible values, Portrait (top home button) etc.