所以我在精灵套件中为角色实现了一些运动,向前运动工作正常、漂亮且流畅,但是当我应用相反的 (-) 力时,该部分用于向后移动,但它有效,但看起来仍然有一个小的向前力推动它,使其抖动,所以看起来很丑且不流畅,
这是我用于所有力的代码,包括重力和跳跃(以便更好地理解)
- (void)update:(NSTimeInterval)delta
{
CGPoint gravity = CGPointMake(0.0, -450.0);
CGPoint gravityStep = CGPointMultiplyScalar(gravity, delta);
//FORWARD MOVE
CGPoint forwardMove = CGPointMake(800.0, 0.0);
CGPoint forwardMoveStep = CGPointMultiplyScalar(forwardMove, delta);
//BACKMOVE
CGPoint BackwardsMove = CGPointMake(-800.0, 0.0);
CGPoint BackMoveStep = CGPointMultiplyScalar(BackwardsMove, delta);
self.velocity = CGPointAdd(self.velocity, gravityStep);
self.velocity = CGPointMake(self.velocity.x * 0.9, self.velocity.y);
CGPoint jumpForce = CGPointMake(0.0, 310.0);
float jumpCutoff = 150.0;
if (self.mightAsWellJump && self.onGround) {
self.velocity = CGPointAdd(self.velocity, jumpForce);
[self runAction:[SKAction playSoundFileNamed:@"jump.wav" waitForCompletion:NO]];
} else if (!self.mightAsWellJump && self.velocity.y > jumpCutoff) {
self.velocity = CGPointMake(self.velocity.x, jumpCutoff);
}
if (self.forwardMarch) {
self.velocity = CGPointAdd(self.velocity, forwardMoveStep);
CGPoint minMovement = CGPointMake(0.0, -450);
CGPoint maxMovement = CGPointMake(120.0, 250.0);
self.velocity = CGPointMake(Clamp(self.velocity.x, minMovement.x, maxMovement.x), Clamp(self.velocity.y, minMovement.y, maxMovement.y));
}
if (self.BackwardsMarch) {
self.velocity = CGPointSubtract(self.velocity, BackMoveStep);
//self.velocity = CGPointAdd(self.velocity, BackMoveStep);
CGPoint minMovement = CGPointMake(0.0, -450);
CGPoint maxMovement = CGPointMake(-120.0, 250.0);
//self.velocity = CGPointMake(-16.0, self.velocity.y);
self.velocity = CGPointMake(Clamp(self.velocity.x, minMovement.x, maxMovement.x), Clamp(self.velocity.y, minMovement.y, maxMovement.y));
}
//4
CGPoint velocityStep = CGPointMultiplyScalar(self.velocity, delta);
self.desiredPosition = CGPointAdd(self.position, velocityStep);
}
如果有人可以弄清楚正在发生的事情以及如何解决这个问题,那就太好了!
我的猜测是,当你应用向后速度时,仍然有向前速度,这会导致抖动。当您要应用向后速度时,首先将 self.velocity 设置为零。然后向后移动。
所以,我想通了,@Knight0fDragon走在正确的轨道上,
我修改了下面的代码。一切都在正常进行。
if (self.velocity.x >0) {
self.velocity = CGPointMake(self.velocity.x * 0.9, self.velocity.y);
}
else
{
self.velocity = CGPointMake(0.0, self.velocity.y);
self.velocity = CGPointMake(self.velocity.x * 0.9, self.velocity.y);
}