繁体   English   中英

放开屏幕时如何释放节点? 雪碧套件

[英]How To Release Node When You Let Go Ot Screen? Sprite Kit

我想在屏幕上按下,然后一个节点将出现在屏幕上,当我释放它时,它将从父级中删除。 有谁知道如何做到这一点?

非常感谢你

我的代码很差

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"wall"];

        sprite.scale = 0.5;

        sprite.position = location;

        [self addChild:sprite];
    }


}

当您的手指不再触摸屏幕时,将调用touchesEnded方法。 您可以实现它并添加代码以删除您的精灵。 这是如何执行此操作的示例:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch ends */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *sprite = (SKSpriteNode *)[self nodeAtPoint:location];
        if ([sprite.name isEqualToString:@"wall"]) {
            [sprite removeFromParent];
        }
    }
}

编辑:将此语句添加到您的touchesBegan方法

    sprite.name = @"wall";

一种通用方法,它删除屏幕外的所有节点:

- (void)update:(CFTimeInterval)currentTime {
    for (SKNode *node in self.children) {
        if (node.position.x < 0 || node.position.y < 0 ||
             node.position.x > self.size.width || node.position.y > self.size.height) {
            [node removeFromParent];
        }
    }
}

您可能希望通过每秒仅执行一次代码来增强代码,因为您不需要每次都执行update:方法时执行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM