我正在尝试通过精灵套件框架制作一款游戏,但是在尝试让角色移动时我陷入困境。当我按下键(左/右)时,我想让角色移动(左/右)。按下时,角色应该加速(带速度限制),当我松开按键时,角色应该减速到完全停止。我无法通过SKAction
找到实现这一目标的方法......任何人都可以解释一下吗?谢谢。
这是我到目前为止所尝试的。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
if (touchLocation.x > [[UIScreen mainScreen]applicationFrame].size.width / 2) {
_rightPressed = YES;
} else {
_leftPressed = YES;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
_rightPressed = NO;
_leftPressed = NO;
_speedDelimiter = 0;
}
-(void)update:(CFTimeInterval)currentTime {
// ... code ...
if (_rightPressed && !_leftPressed && _speedDelimiter < 10) {
SKAction * moveRight = [SKAction moveBy:CGVectorMake(10, 0) duration:5];
[self.player runAction:moveRight];
_speedDelimiter++;
} else if (_leftPressed && !_rightPressed && _speedDelimiter < 10){
SKAction * moveLeft = [SKAction moveBy:CGVectorMake(-10, 0) duration:5];
[self.player runAction:moveLeft];
_speedDelimiter++;
}
}
我认为我应该使用的是我角色的velocity
属性中的SKPhysicsBody
属性。但我不确定。