【问题标题】:Touching nodes is sprite kit触摸节点是精灵套件
【发布时间】:2014-05-16 20:42:45
【问题描述】:

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

这是我添加蚂蚁的代码:

-(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)。 我怎样才能遍历所有的蚂蚁并删除触摸过的蚂蚁?

【问题讨论】:

  • 你对触摸做出反应的代码是什么?

标签: ios objective-c sprite-kit


【解决方案1】:

遍历接触点的节点。

- (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.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多