Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript SetCollizeWorldBounds(true)不是一个函数,使用的是Phaser 3_Javascript_Phaser Framework - Fatal编程技术网

Javascript SetCollizeWorldBounds(true)不是一个函数,使用的是Phaser 3

Javascript SetCollizeWorldBounds(true)不是一个函数,使用的是Phaser 3,javascript,phaser-framework,Javascript,Phaser Framework,使用Phaser 3,我试图使用setCollizeWorldBounds,但它总是告诉我这不是一个函数。我已经在游戏中加入了物理元素,但仍然不起作用 window.onload = function() { config = { type: Phaser.AUTO, width: 800, height: 500, scene: [Scene, Scene2], physics: { default: 'arcade',

使用Phaser 3,我试图使用
setCollizeWorldBounds
,但它总是告诉我这不是一个函数。我已经在游戏中加入了物理元素,但仍然不起作用

window.onload = function() {
config = {
    type: Phaser.AUTO,
    width: 800,
    height: 500,
    scene: [Scene, Scene2],
    physics: {
        default: 'arcade',
        arcade: {
          gravity: { y: 200 },
          enableBody: true,
        }
    }
}
var game = new Phaser.Game(config);
}

create() {
    this.background = this.add.tileSprite(0,0,800,500,"space");
    this.background.setOrigin(0,0);
    this.spaceship = this.add.sprite(0,Phaser.Math.Between(1,width),"spaceship");
    this.player_spaceship = this.add.sprite(400,250,"player_spaceship");
    this.scoreText = this.add.text(16,16, 'score: 0', { fontSize: '32px', fill: 'red' });
    this.keys = this.input.keyboard.addKeys("W,A,S,D");
    this.anims.create ({
        key: "player_spaceship", //name of the animation
        frames: this.anims.generateFrameNumbers("player_spaceship"), //takes the spritesheet
        frameRate:20, // frames per seconds
        repeat: -1 // repeat infinite
    });
    this.player_spaceship.setCollideWorldBounds(true);
    this.spaceship.play("player_spaceship");
    this.player_spaceship.play("player_spaceship");
}

SetCollizeWorldBounds是Arcade主体和(为方便起见)Arcade Sprite类上的一个方法。不幸的是,它不是sprite类本身的一个方法,而这正是您的代码所使用的

1.香草雪碧 您可以使用“香草”精灵,然后将物理添加到其中:

// Assumes the `create` method was snipped from a Scene class
this.player_spaceship = this.add.sprite(400, 250, "player_spaceship");
this.physics.world.enable(this.player_spaceship);
this.player_spaceship.body.setCollideWorldBounds(true);
// nit: don't add me to any physics groups below this line, or I'll forget you want me to collide with the world bounds! Weird but true.
2.街机精灵 或者,您也可以使用物理类和工厂来隐藏一点这种复杂性:

this.player_spaceship = this.physics.add.sprite(400, 250, "player_spaceship");
this.player_spaceship.setCollideWorldBounds(true);
// You *could* use the body here instead if you wanted! But since you have the convenience method, might as well use it.
哪条路更好? 嗯。可能是第二条路。取决于你的比赛