触摸节点是精灵套件
Posted
技术标签:
【中文标题】触摸节点是精灵套件【英文标题】: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)。 我怎样才能遍历所有的蚂蚁并删除触摸过的蚂蚁?
【问题讨论】:
你对触摸做出反应的代码是什么? 【参考方案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.
【讨论】:
以上是关于触摸节点是精灵套件的主要内容,如果未能解决你的问题,请参考以下文章