0
我正在尝试使用SKSpriteNode在sprite工具包中创建一个按钮。我想让按钮图像在按下时更改,并在按下按钮后立即恢复为旧图像。我所做的到现在是以下几点: -如何在sprite工具包中创建按钮iOS(类似于切换按钮)
-(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;
}
}
}
update方法里面我打电话此方法来检查,如果触摸已经结束
-(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];
}
}
的问题是,触摸没有得到有时登记。有时它按我想要的方式工作。当我触摸它时,其纹理会发生变化,当我移开手指时,纹理会恢复到旧纹理。但大多数时候,按钮对我的触摸没有反应。我错过了什么吗?
你在哪里调用'changeMissileButton'方法? – ZeMoon 2014-08-31 16:05:34
主场景的更新方法 – 2014-09-01 07:05:39
这可能是问题...你每秒设置纹理60次!尝试在每个案例的changeMissileButton方法中使用NSLog,并检查方法如何从更新中调用:方法 – ZeMoon 2014-09-01 07:11:59