gpt4 book ai didi

javascript - 如何在phaser框架中实现按键长按加速

转载 作者:行者123 更新时间:2023-11-29 18:12:17 25 4
gpt4 key购买 nike

我正在为客户开发我的第一个移相器游戏。比赛是一辆向前行驶的汽车,它有两分钟的时间到达目标。

我想在按下向上键的同时逐步提高车速,直到达到限速。

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

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

关于javascript - 如何在phaser框架中实现按键长按加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com