我正在尝试使用SKSpriteNode在sprite工具包中创建一个按钮。我希望按下按钮图像在按下时更改,并在按下结束后立即恢复为旧图像。到目前为止我所做的是: -
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
self.startTouch = [[touches allObjects][0] locationInNode:self ];
for (UITouch *touch in touches){
CGPoint position = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:position];
if ([node.name isEqualToString:@"missileButton"]) {
TEMissileButtonNode *button = (TEMissileButtonNode*) node;
button.isPressed = YES;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches){
CGPoint position = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:position];
if ([node.name isEqualToString:@"missileButton"]) {
TEMissileButtonNode *button = (TEMissileButtonNode*) node;
button.isPressed = NO;
}
}
}
在更新方法中我调用此方法来检查触摸是否已结束
-(void)changeMissileButton{
if (self.missileButton.isPressed) {
[self.missileButton addMoreMissileButtons];
[self.missileButton setTexture:[SKTexture textureWithImageNamed:@"missileButtonPressed"]];
}else{
[self.missileButton setTexture:[SKTexture textureWithImageNamed:@"missileButtonDeselected"]];
[self.missileButton hideMissileButtons];
}
}
问题是触摸有时无法注册。有时它按我想要的方式工作。当我触摸它时,它的纹理会发生变化,当我移开手指时,纹理会恢复到旧纹理。但大多数时候,按钮对我的触摸没有反应。我错过了什么吗?
答案 0 :(得分:0)
使用touchesBegan,touchesMoved,touchesEnded。
touchesBegan - 检查按钮(SKSpriteNode)是否包含触摸。如果是这样,请更改按钮纹理。
touchesMoved-如果按钮不包含触摸,则更改纹理。
touchesEnded-如果在touchesMoved期间触摸保持在按钮内,则更改纹理。
以上是如何有效地完成你想要做的事情的逻辑。是的,令人讨厌的Spritekit决定放弃按钮。