我需要知道用户是否正在触摸精灵。我可以很容易地判断他是否举起手指或是否伸出手指:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"button"]) {
// Sprite stopped being touched
return;
}
}
然而,我对如何知道触摸是否结束感到无能为力,因为当用户将手指放在适当位置时精灵会移出。我想我应该以某种方式检查我的更新方法,但我不知道如何:
// Called before each frame is rendered
-(void)update:(CFTimeInterval)currentTime {
// Check if the user is holding the sprite
}
如何做到这一点?
答案 0 :(得分:0)
Apple提供有关他们提供的每个触摸事件的文档。您正在寻找的方法是touchesEnded
处理它与处理touchesMoved的方式相同,并调用您需要的任何方法,以通知节点它不再被触摸。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"button"]) {
// Notify the node that the touch has stopped
}
}
答案 1 :(得分:0)
我认为你要求下面这样的事情。有很多方法可以解决这个问题,但这是思考问题的基本方法。根据游戏的实际复杂程度,您可能需要进行一些状态检查以保护性能,但是您可以获得要点:
//in touchesBegan and touchesMoved update a property:
_lastTouchPoint = //the touch point in scene, etc, blah blah
//if you care about a specific sprite you could also store it:
_someMovingSprite = theSpriteIJustTouchedOrCareAbout;
//in update
if (CGRectContainsPoint(_someMovingSprite.frame, _lastTouchPoint)) {
//the touch is inside the rect of the sprite
}
//OR
NSArray *spritesContainingTheTouchPoint = [self nodesAtPoint:_lastTouchPoint];
if (spritesContainingTheTouchPoint.count == 0) {
//no sprites are being touched
}else{
for (SKSpriteNode *spriteContainingTouch in spritesContainingTheTouchPoint) {
//do something to sprite
}
}
-(void)update:(CFTimeInterval)currentTime {
CGPoint location = [_lastTouchPoint locationInNode:self];
if (!CGRectContainsPoint(_sprite.frame, location)) {
@NSLog(@"Sprite Released");
}
}