2015-02-24 45 views
1

我正在使用使用JS的Phaser框架製作自上而下的賽車遊戲。我在讓車減速時遇到了一些麻煩,此時它沒有按下任何按鈕就停止。我希望它放慢速度停下來。這是我到目前爲止的代碼: 創建:函數(){在Phaser中設置一輛汽車的最大速度和當前速度

//run in canvas mode 
    this.game.renderer.clearBeforeRender - false; 
    this.game.renderer.roundPixels = true; 

    //Add arcade physics 
    this.game.physics.startSystem(Phaser.Physics.ARCADE); 

    //Add background sprites 
    this.game.add.sprite(0, 0, 'background'); 
    this.game.add.sprite(0, 0, 'track'); 

    //Add car sprite 
    car = this.game.add.sprite(800, 135, 'car-concept'); 

    //Car physics settings 
    this.game.physics.enable(car, Phaser.Physics.ARCADE); 

    car.body.drag.set(100); 
    car.body.maxVelocity.set(200); 
    car.body.maxAngular = 500; 
    car.body.angularDrag = 500; 
    car.body.collideWorldBounds = true; 

    //Game input 
    cursors = this.game.input.keyboard.createCursorKeys(); 
    this.game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR]); 

    //pivot point 
    car.pivot.x = car.width * .3; 
    car.pivot.y = car.height * .5; 
    car.anchor.setTo(0.3, 0.5); 

    //scale the car 
    car.scale.setTo(0.3, 0.3); 
}, 

update: function() { 

    car.body.velocity.x = 0; 
    car.body.velocity.y = 0; 
    car.body.angularVelocity = 0; 

    if(cursors.left.isDown) 
    { 
     car.body.angularVelocity = -200; 
    } 
    else if(cursors.right.isDown) 
    { 
     car.body.angularVelocity = 200; 
    } 
    if(cursors.up.isDown) 
    { 
     this.game.physics.arcade.velocityFromAngle(car.angle, -200, car.body.velocity) 
     if(this.currentSpeed < this.maxSpeed) 
     { 
     this.currentSpeed += 10; 
     } 
    } 
    else 
    { 
     if(this.currentSpeed > 0) 
     { 
     this.currentSpeed -= 10; 
     } 
    } 
    if(cursors.down.isDown) 
    { 
     this.game.physics.arcade.velocityFromAngle(car.angle, 200, car.body.velocity) 
     if(this.currentSpeed > 0) 
     { 
     this.currentSpeed -= 30; 
     } 
    } 

}, 

speed: function() 
{ 
    this.maxSpeed = 100; 
    this.currentSpeed = 0; 
}, 

我已經看着這裏大約同樣的問題幾個問題,這就是我得到了我現在所在的位置。我設置了maxSpeed和currentSpeed,但由於某種原因,它不允許我實際使用它。在瀏覽器中的控制檯不會給我任何錯誤,所以如果任何人都可以指導我這一點,那會很棒!

謝謝。

回答

3

它停止死亡的原因是因爲你正在用速度而不是加速度來移動它 - 並且在更新循環中將速度設置爲零(這實際上表示「停止,現在!」)。

刪除這些行,並在您致電velocityFromAngle你應該餵養body.acceleration而不是body.velocity

這裏是一個更新循環,你可以使用,不過你需要在你的currentSpeed設置混合等類

function update() { 

    if (cursors.up.isDown) 
    { 
     game.physics.arcade.accelerationFromRotation(sprite.rotation, 200, sprite.body.acceleration); 
    } 
    else 
    { 
     sprite.body.acceleration.set(0); 
    } 

    if (cursors.left.isDown) 
    { 
     sprite.body.angularVelocity = -300; 
    } 
    else if (cursors.right.isDown) 
    { 
     sprite.body.angularVelocity = 300; 
    } 
    else 
    { 
     sprite.body.angularVelocity = 0; 
    } 

} 
+0

我已經使用該解決方案,但無濟於事。它似乎沒有什麼區別,接受前向運動需要很高,大約9000而不是200.我非常肯定,它是由當前速度和maxSpeed沒有被正確引用。 – 2015-02-25 09:47:37

+0

與引用currentSpeed無關我擔心 - 您使用的實際核心循環已損壞。 在開始引入任何類型的速度限制之前,您需要首先開始工作。正如我所說的,你需要在你的代碼中使用速度來移除所有的東西,並且除去限制(maxVelocity等),並且在你添加速度限制之前把它回到上面的基本結構。 – PhotonStorm 2015-02-25 13:33:15

0

你從未使用過currentSpeed。你的速度設置爲-200200velocityFromAngle

+0

所以你的意思是取代200和-200與currentSpeed? – 2015-02-24 13:52:42