Sprite Kit - Notifier that a node is leaving the screen

末鹿安然 提交于 2019-12-11 11:36:14

问题


Is there a event/notification in Sprite Kit that tells me when a node is leaving the screen? Lets say I want a coloured circle to appear on top when it left the screen at the bottom. Which means I need to know when it is leaving the screen.


回答1:


Sprite kit does not generate a notification when a sprite leaves screen. You will need to add your own test. Here's an example...

- (void) update:(NSTimerInterval)currentTime
{
    CGPoint newPosition = CGPointMake(node.position.x, node.position.y);

    if (node.position.y > maxY+node.size.y/2) {
        newPosition.y = minY;
    }
    else if (node.position.y < minX-node.size.y/2) {
        newPosition.y = maxY;
    }

    if (node.position.x > maxX+node.size.x/2) {
        newPosition.x = minX;
    }
    else if (node.position.x < minX-node.size.x/2) {
        newPosition.x = maxX;
    }
    node.position = newPosition;



回答2:


You will need to check for yourself I think,

- (void)update:(NSTimeInterval)currentTime {

    if (node.position.y > screenHeight+nodeSize){ // need to define first, of course
          // do something like NSLog(); or [removeFromParent] or whatever =)
    }
}


来源:https://stackoverflow.com/questions/25033279/sprite-kit-notifier-that-a-node-is-leaving-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!