ios如何限制节点外观上触摸

发布时间: 2017/4/3 15:37:51
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有节点命名为"Sprite",当我触摸它显示为在特定的 x 轴上,1 秒后消失了,"Sprite"节点,都要与那屏幕上的屏幕可以在同一时间只有两个"Sprite"节点和他们无法在相同的位置上。因为现在我可以添加"Sprite"节点,就像我的手指可以点击屏幕。在这里我给带了什么现在︰

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {

    CGPoint touchlocation = [touch locationInNode:self];
    CGPoint location = CGPointMake(touchlocation.x, 60);

    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];

    sprite.xScale = 0.4;
    sprite.yScale = 0.6;
    sprite.position = location;
    sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
    sprite.physicsBody.dynamic = NO;
    sprite.name = boardCategoryName;

    SKAction *delay = [SKAction waitForDuration:1];
    SKAction *remove = [SKAction removeFromParent];
    SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
    [sprite runAction:actionSequence];

    [self addChild:sprite];

}
}

编辑

@implementation GameScene
SKNode *groupSprite;

-(void)didMoveToView:(SKView *)view{
    groupSprite  = [[SKNode alloc]init];
    [self addChild:groupSprite];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {


    if (groupSprite.children.count < 2) {



        CGPoint touchlocation = [touch locationInNode:self];
        CGPoint location = CGPointMake(touchlocation.x, 75);

        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];

            sprite.xScale = 0.4;
            sprite.yScale = 0.6;

        sprite.position = location;
        sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
        sprite.physicsBody.dynamic = NO;
        sprite.physicsBody.categoryBitMask = boardCategory;
        sprite.physicsBody.contactTestBitMask = ballCategory;
        sprite.name = boardCategoryName;


        SKAction *delay = [SKAction waitForDuration:1];
        SKAction *remove = [SKAction removeFromParent];
        SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
        [sprite runAction:actionSequence];

        [groupSprite addChild:sprite];


    };

解决方法 1:

您可以添加类似的 @kaizoku 数组已指出,但这是只浮在那里的变量代码,使其可能难以管理。 更好的方法就是使用 SKNode 来创建一个组,然后检查组数。

(我客观的 c 是有点生疏,所以请熊与我)

...

SKNode *groupSprite = [[SKNode alloc] init];

...

-(void)didMoveToView:(SKView *)view 
{
    self.addChild(groupSprite);
}

...

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) 
    {
        if(groupSprite.children.count < 2)
        {
            CGPoint touchlocation = [touch locationInNode:self];
            CGPoint location = CGPointMake(touchlocation.x, 60);
            SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];

            sprite.xScale = 0.4;
            sprite.yScale = 0.6;
            sprite.position = location;
            sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
            sprite.physicsBody.dynamic = NO;
            sprite.name = boardCategoryName;

            SKAction *delay = [SKAction waitForDuration:1];
            SKAction *remove = [SKAction removeFromParent];
            SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
            [sprite runAction:actionSequence];

            [groupSprite addChild:sprite];

        }
    }
}

你想要这样,做的原因是因为它使您的代码更具可读性。 你有一个场景,和一群精灵,包含动画层。 现在,如果出于某种原因,你想要从屏幕中移除所有精灵。 你只是从组中删除它,你不需要担心从现场中移除并从数组中移除它。

赞助商