我有一个香椿(player.physicsbody),我想通过按两个按钮(如太空入侵者中的一艘船)左右移动。我编写了这个并且它工作正常,当我在设备中运行应用程序时出现问题,如果按下“左键”,按“右键”则什么都不做,反之亦然。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"rightbutton"]){
player.physicsBody.velocity=CGVectorMake(200, player.physicsBody.velocity.dy);
}
if ([node.name isEqualToString:@"leftbutton"]){
player.physicsBody.velocity=CGVectorMake(-200, player.physicsBody.velocity.dy);
}
}
答案 0 :(得分:0)
调用[event allTouches]会返回一组UITouch事件的NSSet。调用[[event allTouches] anyObject]只返回该组的一个成员。我修改了你的代码来迭代集合中的所有触摸。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"rightbutton"]){
player.physicsBody.velocity=CGVectorMake(200, player.physicsBody.velocity.dy);
}
else if ([node.name isEqualToString:@"leftbutton"]){
player.physicsBody.velocity=CGVectorMake(-200, player.physicsBody.velocity.dy);
}
}
}