Cocos2D CCNode position in absolute screen coordinates
I’ve been looking around for a while, and I have not been able to find an answer to this for some reason. It seems simple enough, but maybe I just can’t find the right function in the library.
I have a scene with a layer that contains a bunch of CCNodes with each one CCSprite in them.
During the application, I move around the position of the main layer, so that I “pan” around a camera in a way. (i.e. I translate the entire layer so that the viewport changes).
Now I want to determine the absolute position of a CCNode in screen coordinates. The position property return the position relative to the parent node, but I really would like this transformed to its actual position on screen.
Also, as an added bonus, it would be awesome if I could express this position as coordinate system where 0,0 maps to the upper left of the screen, and 1,1 maps to the lower right of the screen. (So I stay compatible with all devices)
Edit: Note that the solution should work for any hierarchy of CCNodes preferably.
- UITableView & UIScrollview stop cocos2d animations
- Adding Colours (Colors) Together like Paint (Blue + Yellow = Green, etc)
- Is it possible to use UIPickerView in cocos2d?
- How to get objects to react to touches in Cocos2D?
- how to get Distance of Object from iPhone camera using image Exif meta data?
- Sprite Kit and bitmap fonts
Solutions Collect From Internet About “Cocos2D CCNode position in absolute screen coordinates”
Every CCNode, and descendants thereof, has a method named convertToWorldSpace:(CGPoint)p
This returns the coordinates relative to your scene.
When you have this coordinate, flip your Y-axis, as you want 0,0 to be in the top left.
CCNode * myNode = [[CCNode alloc] init]; myNode.position = CGPointMake(150.0f, 100.0f); CCSprite * mySprite = [[CCSprite alloc] init]; // CCSprite is a descendant of CCNode [myNode addChild: mySprite]; mySprite.position = CGPointMake(-50.0f, 50.0f); // we now have a node with a sprite in in. On this sprite (well, maybe // it you should attack the node to a scene) you can call convertToWorldSpace: CGPoint worldCoord = [mySprite convertToWorldSpace: mySprite.position]; // to convert it to Y0 = top we should take the Y of the screen, and subtract the worldCoord Y worldCoord = CGPointMake(worldCoord.x, ((CGSize)[[CCDirector sharedDirector] displaySizeInPixels]).height - worldCoord.y); // This is dry coded so may contain an error here or there. // Let me know if this doesn't work. [mySprite release]; [myNode release];
- Is there a constant for the maximum CGFloat value?
- Open a different UIViewController when app is launched via SiriKit
- SpriteKiit Swift: touch a moving object
- Xcode Project Icon files and Organiser Archives artwork
- How to post images with text in facebook integration in Iphone sdk
- Node movement lag in Spritekit, Xcode
- Use UIDocumentInteractionController in Delphi XE
- Playing YouTubePlayer Sound in Background Swift
- Swift 2.1 OSx shell commands using NSTask work when run from xcode, but not when exported
- In CBCentralManager delegate method didDiscoverPeripheral: never called
- Do I have to re-compile everytime I add a new device for ad-hoc installation?
- iOS: how to query WiFi state
- NSUrlRequest: where an app can find the default headers for HTTP request?
- Saving 2 UIImages to one while saving rotation, resize info and its quality
- iPhone Brainstorm – CLLocation in Background – Polling every 15 minutes