我最近從Gamemaker:Studio轉移到Java和Slick2D。我不確定如何描述我的問題,但我會盡我所能。在Gamemaker中,有一個稱爲motion_add的功能,它將運動添加到具有給定參數(速度和方向)的對象。假設我們有一個對象以20像素/幀的速度向下移動。如果要將對象的角度反轉180度(因此它現在指向上方)並運行motion_add代碼,它將一直減慢到0,然後再次加速。這對浮動空間運動非常有效(這正是我想要做的)。Java/Slick2D運動的困境(添加運動而不是設置它?)
我的問題是,我該如何重新創建?這是我目前的移動代碼:
private void doPlayerMovement(Input input, int delta) {
// Calculate rotation speed
rotateSpeed = rotateSpeedBase - (speed/5);
if (rotateSpeed < 0.1f) { rotateSpeed = 0.1f; }
// Get input
if (input.isKeyDown(Input.KEY_A)) {
// Rotate left
if (input.isKeyDown(Input.KEY_W)) { rotation -= rotateSpeed * delta; }
sprite.rotate(-rotateSpeed * delta);
}
if (input.isKeyDown(Input.KEY_D)) {
// Rotate right
if (input.isKeyDown(Input.KEY_W)) { rotation += rotateSpeed * delta; }
sprite.rotate(rotateSpeed * delta);
}
if (input.isKeyDown(Input.KEY_W)) {
// Accelerate
speed += acceleration * delta;
} if (input.isKeyDown(Input.KEY_S)) {
// Decelerate
speed -= acceleration * delta;
} else {
if (speed > 0) { speed -= friction; }
}
// Clamping
if (speed > maxSpeed) { speed = maxSpeed; } else if (speed < minSpeed) { speed = minSpeed; }
if (rotation > 360 || rotation < -360) { rotation = 0; }
// Make sure rotation catches up with image angle, if necessairy
if (rotation != sprite.getRotation() && input.isKeyDown(Input.KEY_W)) {
rotation = sprite.getRotation();
}
// Update position
position.x += speed * Math.sin(Math.toRadians(rotation)) * delta;
position.y -= speed * Math.cos(Math.toRadians(rotation)) * delta;
會發生什麼: A和d旋轉精靈。如果W被按下(加速對象),它將操縱對象。如果對象的旋轉與精靈的旋轉不同,對象的旋轉將被設置爲精靈的旋轉,並且即我的問題在哪裏。這樣做會導致對象立即以新的方向拍攝,速度不會減慢。我如何讓它放慢速度?
編輯(在某種程度上) 我敢肯定要有這個是一個名字,如果你開車在100英里,使其轉180度莫名其妙,而它的移動,並擊中天然氣,它會放慢速度,然後再次加速。這正是我想要實現的。感謝您的任何建議。