我正在尝试使用精灵套件制作游戏,我需要角色移动。我希望用户按住按钮,让角色在x轴上直线移动。现在你必须多次点击它才能让它移动
我建议在touchBegan事件上设置x方向速度,在touchUp事件上设置速度为0。
- 点击节点(左或右)
- 检查方向节点是否被触摸
- 根据被触摸的节点设置速度(+值向右移动,-值向左移动)
- 当Touch被释放时,角色的速度重置为0
这是我从游戏中剥离的一些代码:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if([self.moveRightNode containsPoint: location]) {
NSInteger xSpeed = yourSpeed;
character.physicsBody.velocity = CGVectorMake(yourspeed, 0);
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
character.physicsBody.velocity = CGVectorMake(0, 0);
}
设置物理世界时,将重力设置为0或将character.physicsworld.dynamic=NO