cocos2d : why convertToGL, and why addChild CCSprite before its body? - cocos2d-iphone

i'm starting to learn cocos2d, and i was wondering why actually we use convertToGL from CCDirector, [[CCDirector sharedDirector] convertToGL: touchLoc]; when we already have the [touch view] from [touch locationInView: [touch view]];
?
Also, we first "addChild" a CCSprite, containing an image, and then we set its body, and the ccsprite becomes the data user of this body. Would'nt it better to "addChild" the body instead? or is it for any particular purpose?
Thanks

This is because the OpenGL View (EAGLView class) uses a different coordinate system than Cocoa Touch does.
For example, the 0,0 position for Cocoa Touch is on the upper left corner, whereas the 0,0 position for OpenGL is in the lower left corner. That's why you need to "convert to GL" all UIView coordinates.
The conversion also takes into account the current device orientation.

Related

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?

coordinates CCTouch Obj-C

How do I get the coordinates of an touch event in objective-c using cocos2d?
- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event
{
CCLOG(#"Received a touchBegan");
}
CCTouch (currently missing in the class reference but will be added soon) has methods to get the touch locations:
// position in scene coordinates
CGPoint touchPos = [touch locationInWorld];
// touch position relative to anyNode's position
CGPoint touchPosNode = [touch locationInNode:anyNode];
Tip: In Xcode right-click a keyword such as CCTouch and select Jump to Definition and it will bring you to the class interface which at least shows you the available properties/methods and for most of them you'll also find associated reference documentation as comments.

CCDirector setDeviceOrientation into cocos2d v2.0rc1

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.

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

How to restart a level in an iPhone game

I want to restart the current scene i am on. I thought of using replaceScene and replace it with itself. Is that ok to do ?
Level2Scene *scene = [Level2Scene node];
[[CCDirector sharedDirector] replaceScene:scene];
Level2Scene is the current scene I am in.
Thanks
Abhinav
Using replaceScene: is a good way to restart the level, if levels are represented by scenes. Just make sure that if you have any global state (stored outside of the scene) that you reset that, too.