Javascript 移相器:';无法读取属性';车身';未定义的';
我是新做相位器游戏的,我不太明白如何在更新功能中“调用”玩家,以便能够重新启动他们的位置x和y 错误在第59行:Javascript 移相器:';无法读取属性';车身';未定义的';,javascript,html,phaser-framework,Javascript,Html,Phaser Framework,我是新做相位器游戏的,我不太明白如何在更新功能中“调用”玩家,以便能够重新启动他们的位置x和y 错误在第59行:player.body.velocity.y=0可能这也会失败player.body.velocity.x=0 我认为这是一个全局变量的问题,有什么解决办法吗 谢谢大家! var Game = {}; Game.init = function(){ game.stage.disableVisibilityChange = true; }; G
player.body.velocity.y=0代码>可能这也会失败player.body.velocity.x=0代码>
我认为这是一个全局变量的问题,有什么解决办法吗
谢谢大家!
var Game = {};
Game.init = function(){
game.stage.disableVisibilityChange = true;
};
Game.preload = function() {
game.load.tilemap('map', 'assets/map/ejemplo4.json', null, Phaser.Tilemap.TILED_JSON);
game.load.spritesheet('tileset', 'assets/map/tilesheet.png',32,32);
game.load.image('sprite','assets/sprites/sprite.png');
};
var player;
var layer;
var map;
var blockedLayer;
Game.create = function(){
Game.playerMap = {};
game.physics.startSystem(Phaser.Physics.ARCADE);
var testKey = game.input.keyboard.addKey(Phaser.Keyboard.ENTER);
testKey.onDown.add(Client.sendTest, this);
map = game.add.tilemap('map');
map.addTilesetImage('tilesheet', 'tileset'); // tilesheet is the key of the tileset in map's JSON file
map.setCollisionBetween(1, 100);
layer = map.createLayer('backgroundLayer');
blockedLayer = map.createLayer('blockedLayer');
map.setCollisionBetween(1, 100000, true, 'blockedLayer');
layer.inputEnabled = true; // Allows clicking on the map ; it's enough to do it on the last layer
layer.events.onInputUp.add(Game.getCoordinates, this);
Client.askNewPlayer();
};
Game.getCoordinates = function(layer,pointer){
Client.sendClick(pointer.worldX,pointer.worldY);
};
Game.addNewPlayer = function(id,x,y){
player = game.add.sprite(x,y,'sprite');
//player.anchor.setTo(0.5, 0.5);
//player.scale.setTo(2, 2);
game.physics.arcade.enable(player);
player.body.collideWorldBounds = true;
Game.playerMap[id] = player;
};
Game.update = function(){
game.physics.arcade.collide(player, blockedLayer);
player.body.velocity.y = 0;
player.body.velocity.x = 0;
if(this.cursors.up.isDown) {
player.body.velocity.y -= 50;
}
else if(this.cursors.down.isDown) {
player.body.velocity.y += 50;
}
if(cursors.left.isDown) {
player.body.velocity.x -= 50;
}
else if(cursors.right.isDown) {
player.body.velocity.x += 50;
}
};
Game.movePlayer = function(id,x,y){
var player = Game.playerMap[id];
var distance = Phaser.Math.distance(player.x,player.y,x,y);
var tween = game.add.tween(player);
var duration = distance*10;
tween.to({x:x,y:y}, duration);
tween.start();
};
Game.removePlayer = function(id){
Game.playerMap[id].destroy();
delete Game.playerMap[id];
};
解决了
在更新功能中,我检查游戏中是否存在精灵,因此代码为:
if (typeof player !== "undefined"){
player.body.velocity.y = 0;
player.body.velocity.x = 0;
if (cursors.left.isDown) {
player.body.velocity.x = -200;
} else if (cursors.right.isDown) {
player.body.velocity.x = 200;
} else if (cursors.up.isDown) {
player.body.velocity.y = -200;
} else if (cursors.down.isDown) {
player.body.velocity.y = 200;
}
}
谢谢你的帮助 变量player
超出了您的范围,您正在preload
上定义它,并试图在update
上使用它,这就是为什么它没有定义。也许你可以把它们放在你的文件上,而不是放在移相器专家的上面。如果我没弄错的话,它是在“预加载”之外定义的。即便如此,我还是试着把它放在文档顶部,但它仍然不起作用:你是对的,也许从来没有调用过addNewPlayer
?addNewPlayer在另一个类中被调用(我使用多人游戏的套接字),玩家被添加,它移动,但我无法重新启动“body.velocity”