2

我正在为客户开发我的第一个移相器游戏。比赛是一辆前进的汽车,它有两分钟的时间来进球。

我想在按下向上键的同时逐渐增加车速,直到达到限速。

我正在通过以下方式移动赛道而不是汽车:

this.highway2 = game.add.tileSprite(game.world.centerX,game.world.height/2,   game.cache.getImage('highway').width, game.cache.getImage('highway').height, 'highway');
this.highway2.anchor.setTo(0.5,0.5);
this.highway2.autoScroll(0,1000);

所以,我的问题是:
如何控制 autoScroll 的速度来模拟加速?有没有办法知道按键按下了多少时间?这是完成这项工作的正确方法吗?

先谢谢了。

4

2 回答 2

3

Well, I don't know if this is the better way to do this, but it's work pretty well. Just set speed limit and track it in the update function.

var playState = {
create: function(){

    this.setInitialValues();


    game.physics.startSystem(Phaser.Physics.ARCADE);        
    this.cursor = game.input.keyboard.createCursorKeys();

    //highway       
    this.highway2 = game.add.tileSprite(game.world.centerX,game.world.height/2, game.cache.getImage('highway').width, game.cache.getImage('highway').height, 'highway');
    this.highway2.anchor.setTo(0.5,0.5);
    this.highway2.autoScroll(0,0);

    //car
    this.player = game.add.sprite(game.world.centerX+10, game.world.height-150, 'player');
    this.player.anchor.setTo(0.5,0.5);
    game.physics.arcade.enable(this.player);

    //other things

},
update: function(){
    this.movePlayer();  
},
movePlayer: function(){
    // move left and right
    // If the left arrow key is pressed 
    if (this.cursor.left.isDown) 
    {
        // Move the player to the left
        this.player.body.velocity.x = -200; 
    }
    // If the right arrow key is pressed
    else if (this.cursor.right.isDown) 
    { // Move the player to the right 
        this.player.body.velocity.x = 200;
    }
    // If neither the right or left arrow key is pressed
    else 
    {
        // Stop the player 
        this.player.body.velocity.x = 0;
    }

    //speed up and speed down
    if (this.cursor.up.isDown)
    {
        if(this.currentSpeed < this.maxSpeed )
        {
            this.currentSpeed+=10;
            this.highway2.autoScroll(0,this.currentSpeed);
        }

    }
    else{
            if(this.currentSpeed > 0 )
        {
            this.currentSpeed-=10;
            this.highway2.autoScroll(0,this.currentSpeed);
        }
    }

    if (this.cursor.down.isDown)
    {
        if(this.currentSpeed > 0 )
        {
            this.currentSpeed-=30;
            this.highway2.autoScroll(0,this.currentSpeed);
        }
    }
},
setInitialValues: function(){
    this.maxSpeed=1500;
    this.currentSpeed=0;
}
}   
于 2014-10-09T17:29:19.530 回答
1

您应该将关注点严格分离以 1)简化编码 2)简化微调,甚至使游戏更有趣 3)轻松地将您的逻辑“插入”另一个控制器(触摸事件而不是键盘)。

所以在这里你有两个不同的关注点:
* 衡量用户加速了多长时间。
*给定当前速度,最小/最大速度,推力时间,决定什么是当前加速度(==速度变化)。

对于 1) 它非常简单:记录输入开始的时间,现在持续时间是当前时间 - 开始时间。如果你有一个游戏时间,最好使用游戏时间(而不是 Date.now()),这样你就可以避免在游戏从长时间的抽签中恢复后出现意外。

2)你应该微调你的游戏的加速/减速,这会让它更有趣。
最明显的是没有恒定的加速度:达到最大速度的最后 % 必须越来越难。这样你就可以给予和奖励/奖励玩家不要碰到障碍物。
如果玩家没有推进,我不知道你应该怎么做:慢慢减速或快速恢复正常速度?
此外,您还必须决定提升是否是无限的,并且可能是冷却时间。

所以计算当前加速度的函数取决于推力(bool)、推力时间(double)regularSpeed、maxSpeed、minAcc、maxAcc。

这里有很多选项,但计算加速度的代码可能如下所示:

 if (thrusting) {
     // if we're not even at regular speed, max acceleration
     if (speed<regularSpeed) { acc = maxAcc; return; }
     // if we are above maxSpeed, no acceleration  (?? or friction ??)
     if (speed>maxSpeed) { acc=0; return; }
     // compute current speed ratio
     // a figure in [0;1] representing where is the speed in [minSpeed; maxSpeed]
     var speedRatio = (currSpeed-regularSpeed)/(maxSpeed-regularSpeed);
     // ease this ratio as you like
     speedRatio = Math.sqrt(speedRatio);
     // compute acceleration : the more speed, the less acceleration
     // you might want to put one/some threshold this formula.
     acc= minAcc + (1-speedRatio)*(maxAcc-minAcc);
     return;
 } else {
     // do nothing if <= regularSpeed.
     if (speed<=regularSpeed) { acc=0 ; return;}
     // reduce speed if above regular speed
     acc = breakAcc ; // or with friction =>  acc = - k * currSpeed;
     return;
 }
于 2014-10-10T14:18:19.970 回答