Cocos2D CCNode position in absolute screen coordinates

后端 未结 1 1639
Happy的楠姐
Happy的楠姐 2021-01-12 18:14

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 fun

1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 18:52

    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];
    
    

    0 讨论(0)
提交回复
热议问题