CCDirector setDeviceOrientation into cocos2d v2.0rc1 - cocos2d-iphone

before to update my cocos2d 1.0.1 to 2.0rc1 in my app, I was calling [[CCDirector sharedDirector] setDeviceOrientation:(ccDeviceOrientation)currentOrientation]; into some scenes when detected one change on orientation and then load other scene in portrait or lanscape depending the currentOrientation.
But now I don't have any idea about how I can do it.

Orientation setOrientation ( Orientation orientation )
Callback by CCDirector for change device orientation.
The defination of orientation which CCDirector want change to.
Returns
The actual orientation of the application.

Open AppDelegate.m and locate this method:
// Supported orientations: Landscape. Customize it for your own needs
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Return YES for all the orientations your app supports.

Related

Qt C++ / objective-C application, lock iPad screen orientation programmatically

I have a Qt / QML application on iOS. It is coded in C++ but I can include objective-c code in it.
I can change programmatically the orientation of the screen, which changes the orientation of the application, without changing the orienation of the iPad, and the user has to turn it when he notices the change of orientation of the screen.
I do that as I need to force the orientation depending on what I display on the application.
What I need to do now is to lock the automatic change of orientation (due to physical change) as I need it to be landscape sometimes and portrait other times.
To change the orientation programmatically, I found the above solution which works:
in my plist file, orientations must be allowed:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
in my C++ code where I can insert objective-c:
void setScreenOrientation(bool inIsPortrait)
{
if(#available(iOS 16, *))
{
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = (UIWindowScene *)array[0];
UIInterfaceOrientationMask newOrientation;
if(inIsPortrait)
newOrientation = UIInterfaceOrientationMaskPortrait;
else
newOrientation = UIInterfaceOrientationMaskLandscapeRight;
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:newOrientation];
[scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) { }];
}
else
{
//does not work on iOS 16, outputs following error:
//[Orientation] BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)
NSNumber *newOrientation;
if(inIsPortrait)
newOrientation = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
else
newOrientation = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:newOrientation forKey:#"orientation"];
}
}
}
So with that, when I want to change the orientation from portrait to landscape or vice versa, I just call the function with the correct parameter.
The problem is that when I physically turn the iPad, the screen automatically changes its orientation due to the plist file. So when I force to landscape, the user sees the change so turns its iPad, but if he turns back to portrait, the automatic change will put back the screen to portrait, without calling my function of course.
I can't remove the orientations from the plist file as they are required if I want to change it programmatically.
But I would like to lock the screen in the selected orientation at the end of the function, and of course, unlock it at the beginning, so that physically turn the iPad won't change the screen orientation.
I found solutions with the function "shouldAutorotate" but it has to be overwritten and called in viewDidLoad or supportedInterfaceOrientationsForWindow which are specific to an objective-c implementation and architecture.
I found : Lock orientation with native Objective-C code from Qt
but I can't do that in my code as I don't have any ViewController, ... it's a Qt / QML application.
I would like to have something that I can call in my function, something to change the supported orientations, so modify "supportedInterfaceOrientationsForWindow" by calling it directly in a function. Something like:
[[UIApplication ???] supportedInterfaceOrientationsForWindow] = UIInterfaceOrientationMaskPortrait; //or UIInterfaceOrientationMaskLandscapeRight depending on what I need
or any other instructions but without having to override supportedInterfaceOrientationsForWindow.
Thanks

CCSprite detection via touch without transparent pixel

I am trying to detect a touch on a CCSprite using:
#implementation MainScene{
CCPhysicsNode *_levelView;
}
- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
CGPoint location = touch.locationInWorld;
CCSprite *clickedSprite;
for (CCSprite *obj in _levelView.children)
{
if (CGRectContainsPoint(obj.boundingBox, location))
clickedSprite = obj;
}
}
which works perfectly fine.
So I can detect clicked Sprites already.
Since I am using sprites which are transparent at some parts, they are also detected when the transparent part of the sprite is clicked.
But I want to exclude the transparent part from the detection…
For the physics I am using physics shape polygon. Is there a way to use this polygon for something like:
polygonContainsPoint(obj.physicsPolygon, location)
? Or maybe even a way saying all pixels but those with transparency 100? Or an even easier solution?

How to show particle from a thread in Cocos2d

I am trying to take a particle effect in my Cocos2d project. Particle effect shows good. But I am confused when I put the particle showing function into a thread as follows, it only shows tiny dark squares instead of the right texture. thanx in advance.
// thread caller
[self performSelectorInBackground:#selector(showParticleThrd:) withObject:nil];
// it works good when i call it directly
-(void) showParticleThrd{
CCParticleSystem * system = [[ParticleEffectSelfMade alloc] initWithTotalParticles:1];
[aLayer removeChildByTag:R_TAG cleanup:YES];
system.position = ccp(self.position.x,self.position.y);
[self.parent addChild:system z:R_ORDER tag:R_TAG];
[system release];
}
You can not modify anything cocos2d related in a background thread. Cocos2d requires you to make changes to nodes in the main thread, where the OpenGL context was created.

Cocos2d - a way to get the current scene?

I'm looking for a way to get the current scene so that I'll be able to tell which scene is running at any time.
Thanks!
Check out CCDirector. You can get the running scene like this:
[[CCDirector sharedDirector] runningScene];
From the documentation of cocos2D:
-(CCScene*) runningScene [read, assign]
The current running Scene. Director can only run one Scene at the time
Sandro Meier

setting proper cocos2d orientation

In my cocos2d application, inside the applicationDidFinishLaunching method for my app delegate, I set the orientation via [director setDeviceOrientation:kCCDeviceOrientationPortrait] because I really only want portrait. However, Apple rejected my app saying it must support upside down portrait as well.
I'm not certain how I detect this, though. Reading the currentDevice orientation seems to return an unknown orientation, so my questions are twofold:
1) How am I supposed to detect the orientation so I can properly set it to either portrait or upsidedown portrait (where it will stay for good).
2) I suspect I'll have an issue with the splash screen because it's loaded before I reach this point in the delegate. How can I properly detect the orientation so I can set the right splash screen?
I can only edit the codes to fix your first question.. i hope you are using .99.5..
in RootViewController.h, in the function
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
look for this line:
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
{
return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
}
change to
return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );