如何通过移相框架中的长按键实现加速

时间:2014-10-09 15:34:32

标签: javascript phaser-framework

我正在为客户开发我的第一个移相器游戏。游戏是向前推进的汽车,它有两分钟的时间来达到目标​​。

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

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

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的速度来模拟加速? 有没有办法知道按键的时间是多少? 这是完成这项工作的正确方法吗?

先谢谢了。

2 个答案:

答案 0 :(得分:3)

好吧,我不知道这是否是更好的方法,但它的工作非常好。 只需设置速度限制并在更新功能中跟踪它。

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;
}
}   

答案 1 :(得分: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;
 }