触摸节点是精灵套件

时间:2014-05-16 20:42:45

标签: ios objective-c sprite-kit

我有一个小的Sprite Kit游戏,你在屏幕上有ser猫蚂蚁,当你触摸它们时,它们应该消失。

这是我添加蚂蚁的代码:

-(void)addAnt
{
    SKSpriteNode *ant = [SKSpriteNode spriteNodeWithImageNamed:@"ant-icon"];
    NSString *antName = [NSString stringWithFormat:@"ant %d",_antNumber];
    _antNumber++;
    ant.name = antName;
    ant.xScale = 0.5;
    ant.yScale = 0.5;
    int lowestPositionX = ant.size.width/2;
    int highestPositionX = self.size.width - ant.size.width/2;
    int lowestPositionY = ant.size.height/2;
    int highestPositionY = self.size.height - ant.size.height/2;
    int randomSpiderXValue = lowestPositionX + arc4random() % (highestPositionX - lowestPositionX);
    int randomSpiderYValue = lowestPositionY + arc4random() % (highestPositionY - lowestPositionY);
    int randomRotaionValue = -2*M_PI + arc4random() % (int)(2*M_PI - 2*-M_PI);
    ant.zRotation = randomRotaionValue;
    ant.position = CGPointMake(randomSpiderXValue, randomSpiderYValue);;
    [self addChild:ant];
}

然后,当触摸屏幕时,我想删除被触摸的蚂蚁。 (蚂蚁%d)。 如何通过所有蚂蚁进行迭代,只删除被触摸的蚂蚁?

1 个答案:

答案 0 :(得分:2)

遍历触摸点处的节点。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];

    for (SKNode *ant in nodes)
    {
        // Do something with touched ant.
    }
}