Hi All,
Was testing sparrow on ipad and noticed it doesn't switch orientation,
Has anyone got this to work on ipad with function for native orientation switching?
Any help would be most appreciated.
Hi All,
Was testing sparrow on ipad and noticed it doesn't switch orientation,
Has anyone got this to work on ipad with function for native orientation switching?
Any help would be most appreciated.
Yeah, what I did was create a sprite in the center of the stage that holds all my content. Call [[UIDevice currentDevice] beginGeneratingDeviceOrientationNoifcations];
Register the relevant class (probably the Game class) for UIOrientationDidChangeNotification. In the method you assign to receive these notifications, check the current device orientation >> [UIDevice currentDevice].orientation. Now use the juggler to rotate your holder sprite appropriately. I found a tween over 0.3 secs works well.
cheers,
Alex
Here is what I did for that problem:
Added a controller (called it MainController) in the MainWindow.xib, and added SPView in the MainController. Also, created a corresponding UIViewController for the MainController.
After that add the following code in your new UIViewController:
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation) interfaceOrientation {
// You can check the interface orientation and based on its value you can allow the
// rotation if you want to...
// For example the following commented code will allow only portrait mode rotations:
// if (interfaceOrientation == UIInterfaceOrientationPortrait ||
// interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
//
// return YES;
// }
// return NO;
return YES;
}
Now your view should rotate accordingly!
Hope this helps.
Thanks alot guys, that's just what i was looking for. Very helpful comunity here at sparrow. Thanks again...
Actually I posted almost the exact same question as this a few months back. Daniel advised against rotating the SPView itself as this can make life difficult for the OpenGL rendering and have a negative impact on performance. So while the UIViewController approach may be a little more straightforward to implement, it's probably advisable to use the SPSprite based technique in the long run.
cheers,
Alex
Thanks a lot Alex,
Sorry to ask you more questions here, but I'm stuck,
so if you have time could you do me a huge favor and post some code as an example,
I can't seem to get a grasp of what your doing there.
say we're using the barebones scaffold project,
and I create a holder sprite @interface Game ()
SPSprite *mContents;//holder sprite
//-----------------------------------------------------
//Create holding sprite
//-----------------------------------------------------
mContents = [[SPSprite alloc] init];
mContents.rotation = SP_D2R(90);
mContents.x = (float)width;
[self addChild:mContents];
//
@implementation Game
//width and height for portrait (not landscape)
- (id)initWithWidth:(float)width height:(float)height{
if (self = [super initWithWidth:width height:height]){
rotationTime = 0.3;//seconds taken to rotate
rotationL = SP_D2R(90);
rotationR = SP_D2R(-90);
[self initContentHolder:width height:height];
//don't do anything regarding uikit yet - we don't have a nativeView reference!
//dlog(@"stageW %f stageH %f ",stageW,stageH);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onOrientationChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
return self;
}
#pragma mark -
//create sparrow content holders
-(void)initContentHolder:(float)width height:(float)height{
//TODO - set this up appropriately according to whether you want to be landscape or portrait
float stageW;
float stageH;
//dummy var for testing
BOOL landscape = TRUE;
if(landscape){
stageH = width;
stageW = height;
} else {
stageH = height;
stageW = width;
}
//create a pivot sprite
//dlog(@"stageW %f stageH %f ",stageW,stageH);
pivot = [SPSprite sprite];
//position this using the w & h arguments, ignoring whether landscape or not!
pivot.x = width * 0.5;
pivot.y = height * 0.5;
[self addChild:pivot];
//create a rotated holder sprite so that everything can easily work as landscape
content = [SPSprite sprite];
[pivot addChild:content];
if(landscape){
//check if landscape left or right!
UIInterfaceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if(deviceOrientation == UIInterfaceOrientationLandscapeRight){
//dlog(@"landscape right");
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:FALSE];
} else {
//default to landscape left!
//dlog(@"landscape left");
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:FALSE];
}
pivot.rotation = appRotation;
}
content.x = stageW * -0.5;
content.y = stageH * -0.5;
}
#pragma mark -
//
-(void)onOrientationChange:(NSNotification*)notification{
UIInterfaceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
switch(deviceOrientation){
case UIInterfaceOrientationLandscapeLeft:
dlog(@"landscape left");
[self landscapeL];
break;
case UIInterfaceOrientationLandscapeRight:
dlog(@"landscape right");
[self landscapeR];
break;
/*case UIInterfaceOrientationPortrait:
dlog(@"portrait");
break;
case UIInterfaceOrientationPortraitUpsideDown:
dlog(@"upside down");
break;*/
}
}
//
-(void)landscapeL{
float r = rotationR;
if(pivot.rotation == r) return;
//[self tracePos];
SPTween* tween = [SPTween tweenWithTarget:pivot time:rotationTime];
[tween animateProperty:@"rotation" targetValue:r];
[self.stage.juggler addObject:tween];
//make sure the keyboard goes the right way around
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
}
//
-(void)landscapeR{
float r = rotationL;
if(pivot.rotation == r) return;
SPTween* tween = [SPTween tweenWithTarget:pivot time:rotationTime];
[tween animateProperty:@"rotation" targetValue:r];
[self.stage.juggler addObject:tween];
//make sure the keyboard goes the right way around
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}I think the above should work for you - I've had to lift it from something I'm working on, so I had to remove a fair bit of code that wasn't relevant to the functionality in question!
Really Thanks so much for that, fastest response ever...
Will see how I go and get back to you if I run into any issues..... But Thanks again for taking the time to post.
I got this working with the help of your code,
Still some issues to work out with rotation and alignment of the sprite,
but will post the code after I'm done and have it working on device.
Thanks again...
O.k this works, and has been tested on ipad device. So for anyone wanting to know how to do this, here's the code.
I took the core sparrow scaffold project and added the following based on Alex's code and modified, to game.h and game.m
Only thing I haven't done in this code is write the upside-down rotation calculation, wasn't in the mind set after doing 3 other orientations.
Anyway hope this helps someone out....
// Game.h
// base_ipad
//
// Created by ibii on 10/09/09.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Game : SPStage
SPSprite *pivot;
SPTween* tween;
float rot;
float rotTime;//rotation time
@end
//---------------------------------------------
// Game.m
// base_ipad
//
// Created by ibii on 10/09/09.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "Game.h"
//declaired in game.h
//SPSprite *pivot;
//SPTween* tween;
//float rot;
//float rotTime;
@implementation Game
- (id)initWithWidth:(float)width height:(float)height
{
if (self = [super initWithWidth:width height:height])
{
rotTime = 0.3;
//------------------------
//create a pivot sprite
//------------------------
pivot = [SPSprite sprite];
[self addChild:pivot];
pivot.width = 100;
pivot.height = 100;
//---------------------------------
//Create test content
//---------------------------------
SPQuad *quad = [SPQuad quadWithWidth:100 height:100];
quad.color = 0xff0000;
quad.x = 0;
quad.y = 0;
SPQuad *quad2 = [SPQuad quadWithWidth:250 height:20];
quad2.color = 0xffffff;
quad2.x = 60;
quad2.y = 60;
//--------------------------------
//Add test content to pivot
//--------------------------------
[pivot addChild:quad];
[pivot addChild:quad2];
//--------------------------------------------
//Add Orientation listener
//--------------------------------------------
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
return self;
}
-(void)landscapeLeft
{
rot = SP_D2R(-90);
if(pivot.rotation == rot) return;
pivot.y = 1024;
tween = [SPTween tweenWithTarget:pivot time:rotTime];
[tween animateProperty:@"rotation" targetValue:rot];
[self.stage.juggler addObject:tween];
}
-(void)landscapeRight
{
rot = SP_D2R(90);
if(pivot.rotation == rot) return;
pivot.x = 768;
pivot.y = 0;
tween = [SPTween tweenWithTarget:pivot time:rotTime];
[tween animateProperty:@"rotation" targetValue:rot];
[self.stage.juggler addObject:tween];
}
-(void)Portrait
{
rot = SP_D2R(0);
if(pivot.rotation == rot) return;
pivot.x = 0;
pivot.y = 0;
tween = [SPTween tweenWithTarget:pivot time:rotTime];
[tween animateProperty:@"rotation" targetValue:rot];
[self.stage.juggler addObject:tween];
}
-(void)onOrientationChange:(NSNotification*)notification{UIInterfaceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
//NSLog(@"stage_width %f stage_height %f ",stage_width,stage_height);
switch(deviceOrientation)
{
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"landscape left");
[self landscapeLeft];
//make sure the keyboard goes the right way around
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"landscape right");
[self landscapeRight];
//make sure the keyboard goes the right way around
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
break;
case UIInterfaceOrientationPortrait:
NSLog(@"Right way up");
[self Portrait];
//make sure the keyboard goes the right way around
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"upside down");
//make sure the keyboard goes the right way around
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
break;
}
}
@end
Edit:
The code that ibii posted works great. I'm having trouble integrating it with gamecenter.
@ipadfan -- What was your Game Center issue with the above code? I can't help as I haven't used Game Center yet nor do I know how it cares about orientation, but I'm curious and perhaps the description and bump will prompt someone else to respond.
And for everyone else -- below is a distilled version of the above code from Ibii (who also quotes others), to show only the minimum code to recognize the rotation (plus NSLogs to verify). This is useful to show how simple it is to detect the rotation. My current project will respond to the rotation in a very different way but this portion of the code was extremely helpful:
Add the following to the init of the Game.m (or another implementation where you want to detect the orientation):
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
And add the following method to the implementation:
-(void)onOrientationChange:(NSNotification*)notification { UIInterfaceOrientation deviceOrientation = [UIDevice currentDevice].orientation; switch(deviceOrientation) { case UIInterfaceOrientationLandscapeLeft: NSLog(@"landscape left"); break; case UIInterfaceOrientationLandscapeRight: NSLog(@"landscape right"); break; case UIInterfaceOrientationPortrait: NSLog(@"Right way up"); break; case UIInterfaceOrientationPortraitUpsideDown: NSLog(@"upside down"); break; } }
reglet,
Thanks for thinking of me. The issue was that the leaderboard would not respect the rotation and would only display in portrait mode no matter what the display was.
You must log in to post.