gpt4 book ai didi

javascript - Phaser.js - 将游戏变量传递给自定义对象的更新函数

转载 作者:行者123 更新时间:2023-12-03 04:52:05 25 4
gpt4 key购买 nike

所以我遇到的问题是我正在尝试创建一个 Player 对象并将游戏变量传递给它,除非我尝试访问游戏变量(特别是 game.time) 在播放器的更新函数中。我正在使用 Phaser 版本 6.2.6,并使用此示例来创建自定义播放器对象:https://phaser.io/examples/v2/sprites/extending-sprite-demo-2

我确实删除了不相关的代码,但请随时询问您是否需要更多(例如加载状态,我加载播放器资源的位置)。但事不宜迟,这是代码。

index.html(头):

<script type="text/javascript" src="asset/js/phaser.min.js"></script>
<script type="text/javascript" src="asset/js/Player.js"></script>
<script type="text/javascript" src="asset/js/Level1.js"></script>

index.html(正文):

<script type="text/javascript">

window.onload = function() {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '');

game.state.add('Level1', Game.Level1);

game.state.start('Level1');
};
</script>

Level1.js:

var Game = {};  // This line is actually only in my Boot.js file but it for this question I put it here

Game.Level1 = function(game) {};

Game.Level1.prototype = {
preload : function() {

},

create : function(game) {

game.stage.backgroundColor = '#42f4d9';

this.physics.arcade.gravity.y = 1400;

game.player = new Player(game, 'henri', 100, 100);
},

update : function(game) {
console.log(game); // here the game object gets printed like it should
},
};

Player.js:

Player = function (game, name, x, y) {

// accessing 'game' in here works fine.

Phaser.Sprite.call(this, game, x, y, 'player');
this.anchor.setTo(0.5, 0.5);

this.speed = 100;
this.jumpVelocity = -600;
this.jumpTimer = 0;

this.animations.add('idle', [0], 1, false);
this.animations.add('jump', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 20, true);

game.physics.arcade.enable(this);
this.body.collideWorldBounds = true;

this.controls = {
jump: game.input.keyboard.addKey(Phaser.Keyboard.W)
};

game.add.existing(this);
};

Player.prototype = Object.create(Phaser.Sprite.prototype);
Player.prototype.constructor = Player;

Player.prototype.update = function(game) {

console.log(game) // here it prints 'undefined'

this.body.velocity.x = 0;

// this is where I actually get the problem, trying to do game.time.now throws the error "Uncaught TypeError: Cannot read property 'time' of undefined"
if(this.controls.jump.isDown && (this.body.onFloor() || this.body.touching.down) && game.time.now > this.jumpTimer) {
this.body.velocity.y = this.jumpVelocity;
this.jumpTimer = this.time.now +750;
this.animations.play('jump');
}

if(this.body.velocity.x == 0 && this.body.velocity.y == 0) {
this.animations.play('idle');
}
};

最佳答案

在Player.js中我不知道什么时候使用参数名称,但我假设它是通过Preload加载的资源的键:

Phaser.Sprite.call(this, game, x, y, name);

据我了解,Update 不接收参数,要从 Update 函数引用“游戏”对象,将在 Player.js 中使用“this”:

Player.prototype.update = function() {

console.log(this.game);

this.body.velocity.x = 0;

if (this.controls.jump.isDown && (this.body.onFloor() || this.body.touching.down) && this.game.time.now > this.jumpTimer) {
//Code
}
}

关于javascript - Phaser.js - 将游戏变量传递给自定义对象的更新函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608563/

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