Javascript 如何在移相器框架中实现长按键加速

Javascript 如何在移相器框架中实现长按键加速,javascript,phaser-framework,Javascript,Phaser Framework,我正在为客户开发我的第一款phaser游戏。这场比赛是一辆向前行驶的汽车,有两分钟的时间射门 我想在按下Up键的同时提高车速,直到达到限速 我通过以下方式移动赛马场而不是汽车: this.highway2 = game.add.tileSprite(game.world.centerX,game.world.height/2, game.cache.getImage('highway').width, game.cache.getImage('highway').height, 'highw

我正在为客户开发我的第一款phaser游戏。这场比赛是一辆向前行驶的汽车,有两分钟的时间射门

我想在按下Up键的同时提高车速,直到达到限速

我通过以下方式移动赛马场而不是汽车:

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的速度以模拟加速度? 有没有办法知道一个键被按下了多少时间? 这是实现这一目标的正确方法吗


提前谢谢

我不知道这是否是更好的方法,但效果相当不错。 只需设置速度限制,并在更新功能中跟踪它

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;
}
}   
var playState={
创建:函数(){
此参数为.setInitialValues();
游戏。物理。启动系统(相位器。物理。拱廊);
this.cursor=game.input.keyboard.createCursorWorkeys();
//公路
this.highway2=game.add.tileSprite(game.world.centerX,game.world.height/2,game.cache.getImage('highway')。宽度,game.cache.getImage('highway')。高度,'highway');
这条公路2.锚定.设置为(0.5,0.5);
这条高速公路2.autoScroll(0,0);
//汽车
this.player=game.add.sprite(game.world.centerX+10,game.world.height-150,'player');
this.player.anchor.setTo(0.5,0.5);
游戏。物理。街机。启用(this.player);
//其他事情
},
更新:函数(){
这个.movePlayer();
},
movePlayer:function(){
//左右移动
//如果按下左箭头键
if(this.cursor.left.isDown)
{
//把球员移到左边
this.player.body.velocity.x=-200;
}
//如果按下向右箭头键
else if(this.cursor.right.isDown)
{//将玩家移到右边
这个.player.body.velocity.x=200;
}
//如果未按下向右或向左箭头键
其他的
{
//阻止球员
这个.player.body.velocity.x=0;
}
//加速和减速
if(this.cursor.up.isDown)
{
if(this.currentSpeed0)
{
该电流速度-=10;
this.highway2.autoScroll(0,this.currentSpeed);
}
}
if(this.cursor.down.isDown)
{
如果(此.currentSpeed>0)
{
该电流速度-=30;
this.highway2.autoScroll(0,this.currentSpeed);
}
}
},
setInitialValues:函数(){
此参数为.maxSpeed=1500;
此参数。当前速度=0;
}
}   

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

因此,这里有两个不同的关注点:
*测量用户加速的时间。
*给定当前速度、最小/最大速度、推力时间,确定当前加速度(=速度变化)

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

对于2)你应该微调游戏的加速/减速,这将使游戏更有趣。
最明显的是不要有恒定的加速度:要达到最大速度的最后%必须越来越困难。通过这种方式,您可以奖励玩家不要碰障碍物。
如果玩家没有冲刺,你应该怎么做,我不知道:缓慢减速还是快速恢复正常速度?
此外,你还必须决定助推是否是无限的,也许还有一段冷却时间

因此,计算电流加速度的函数取决于推力(bool)、推力时间(double)规则速度、最大速度、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;
 }
if(推力){
//如果我们不是以正常速度,最大加速度
如果(speedmaxSpeed){acc=0;返回;}
//计算当前速度比
//[0;1]中的一个数字,表示[minSpeed;maxSpeed]中的速度
var速比=(当前速度调节速度)/(最大速度调节速度);
//根据您的喜好降低此比率
速比=数学sqrt(速比);
//计算加速度:速度越快,加速度越小
//您可能需要在该公式中设置一个或多个阈值。
acc=minAcc+(1-速比)*(maxAcc minAcc);
返回;
}否则{

//如果游戏炼金术士,你错了,什么都不要做。我只是需要一些指导。这是我第一次与phaser一起开发游戏。我不是一个天生的演说家,所以我不知道你的评论是否友好,但我认为最后一部分(“如果我猜对了,你的问题是‘你能为我写代码吗?’”)不会增加价值。谢谢。@gamealchest,再次感谢您的评论。让我们关注一些问题。您能帮助解决其中一些问题吗?谢谢您的方法。这比我的方法更好。我将尝试实施,并让您知道它是如何工作的。