2014-05-16 104 views
1

我有一個小雪碧套件遊戲,你有屏幕上的螞蟻,當你觸摸它們時,它們應該消失。觸摸節點是精靈套件

這是我加入螞蟻代碼:

-(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]; 
} 

然後,當屏幕被觸摸,我想刪除被觸摸的螞蟻。 (ant%d)。 我怎樣才能遍歷所有的螞蟻,並刪除觸摸的?

+0

什麼是您的觸摸響應代碼? – connor

回答

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