Phaser-js与世界边界冲突



有人能解释一下我的错误在哪里吗?我试图让我的玩家与世界的边界碰撞,而不仅仅是扔墙。我尝试了自定义方法,但这不是我想要的效果。当我将"game.world.setBounds(0,0,x,y);"添加到创建函数时,我的游戏不会启动。我是相位器js的工程师,所以也许我做错了什么。这是我的代码:

"use strict";
var game = new Phaser.Game(1000, 800, Phaser.CANVAS, "game_div");
var spaceField,
    backgroundSpeed,
    player,
    cursors,
    bullets,
    bulletsTime = 0,
    fireButton,
    bullet,
    bulletSound;
var mainState = {
    preload: function () {
        //id
        game.load.image("starfield", "images/space.png");
        game.load.image("player", "images/playerSmall.png");
        game.load.image("bullet", "images/fire.png");
        // audio
        game.load.audio("bulletSound", "sounds/blaster.mp3");
    },
    create: function () {
        // Full screen when clicking with the mouse on the screen
        game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT;
        game.input.onDown.add(goFull, this);
        // background
        spaceField = game.add.tileSprite(0, 0, 1000, 800, "starfield");
        backgroundSpeed = 2;
        game.physics.setBoundsToWorld();
        // player spaceship + adding physics + player movement
        player = game.add.sprite(game.world.centerX, game.world.centerY + 300, "player");
        game.physics.enable(player, Phaser.Physics.ARCADE);
        cursors = game.input.keyboard.createCursorKeys();
        // Fire bullets
        bullets = game.add.group();
        bullets.enableBody = true;
        bullets.physicsBodyType = Phaser.Physics.ARCADE; // Enabling physics for bullets
        bullets.createMultiple(30, "bullet");
        bullets.setAll("anchor.x", 0.5);
        bullets.setAll("anchor.y", 1);
        bullets.setAll("outOfBoundsKill", true); // Checks if the bullet is off screen so we can reuse it
        bullets.setAll("checkWorldBounds", true);
        fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
        bulletSound = game.add.audio("bulletSound");
    },
    update: function () {
        // Making scrolling background
        spaceField.tilePosition.y += backgroundSpeed;
        player.body.velocity.x = 0; // Everytime when key is not pressed the player does not move
        player.body.velocity.y = 0;
        // Checking which key is pressed
        if (cursors.up.isDown) {
            player.checkWorldBounds = true;
            player.events.onOutOfBounds.add(playerOutOfBoundsTop, this);
            player.body.velocity.y = -350;
        }
        if (cursors.down.isDown) {
            player.checkWorldBounds = true;
            // player.events.onOutOfBounds.add(playerOutOfBoundsBottom, this);
            player.body.velocity.y = 350;
        }
        if (cursors.left.isDown) {
            player.body.velocity.x = -350;
        }
        if (cursors.right.isDown) {
            player.body.velocity.x = 350;
        }
        if (fireButton.isDown) {
            fireBullet();
        }
    }
};
function fireBullet() {
    if (game.time.now > bulletsTime) {
        bullet = bullets.getFirstExists(false);
        if (bullet) {
            bullet.reset(player.x + 28, player.y);
            bullet.bulletAngleOffset = 90;
            bullet.bulletAngleVariance = 30;
            bullet.body.velocity.y = -400;
            bulletsTime = game.time.now + 200;
            bulletSound.play();
        }
    }
}
function playerOutOfBoundsTop(player) {
    //  Move the Spaceship to the top of the screen again
    player.reset(player.x, 60);
}
/*function playerOutOfBoundsBottom(player) {
 // Move the spaceship to the bottom of the screen again
 player.reset(60, player.x);
 }
 */
function goFull() {
    if (game.scale.isFullScreen) {
        game.scale.stopFullScreen();
    } else {
        game.scale.startFullScreen(false);
    }
}
//id
game.state.add('mainState', mainState);
game.state.start("mainState");

如果你想让你的玩家不超出游戏边界,你可以在create函数中将属性collapseWorldBounds设置为true:

player.body.collideWorldBounds=true;

看看这个相位器的例子和文档

但是,如果你想在玩家出界时做一些不同的事情,你可以在更新循环中添加一个函数(比如按下UP键时的函数)。

例如,使用此代码,当您的玩家出界时,它将再次出现在游戏中间:

var game = new Phaser.Game(1000, 800, Phaser.CANVAS, "game_div");
var spaceField,
    backgroundSpeed,
    player,
    cursors,
    bullets,
    bulletsTime = 0,
    fireButton,
    bullet,
    bulletSound;
var mainState = {
    preload: function () {
        //id
        game.load.image("starfield", "images/space.png");
        game.load.image("player", "images/playerSmall.png");
        game.load.image("bullet", "images/fire.png");
        // audio
        game.load.audio("bulletSound", "sounds/blaster.mp3");
    },
    create: function () {
        // Full screen when clicking with the mouse on the screen
        game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT;
        game.input.onDown.add(goFull, this);
        // background
        spaceField = game.add.tileSprite(0, 0, 1000, 800, "starfield");
        backgroundSpeed = 2;
        game.physics.setBoundsToWorld();
        // player spaceship + adding physics + player movement
        player = game.add.sprite(game.world.centerX, game.world.centerY + 300, "player");
        game.physics.enable(player, Phaser.Physics.ARCADE);
        cursors = game.input.keyboard.createCursorKeys();
        //player.body.collideWorldBounds=true;
        // Fire bullets
        bullets = game.add.group();
        bullets.enableBody = true;
        bullets.physicsBodyType = Phaser.Physics.ARCADE; // Enabling physics for bullets
        bullets.createMultiple(30, "bullet");
        bullets.setAll("anchor.x", 0.5);
        bullets.setAll("anchor.y", 1);
        bullets.setAll("outOfBoundsKill", true); // Checks if the bullet is off screen so we can reuse it
        bullets.setAll("checkWorldBounds", true);
        fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
        bulletSound = game.add.audio("bulletSound");
    },
    update: function () {
        // Making scrolling background
        spaceField.tilePosition.y += backgroundSpeed;
        player.body.velocity.x = 0; // Everytime when key is not pressed the player does not move
        player.body.velocity.y = 0;

        player.events.onOutOfBounds.add(playerOutOfBounds, this);
        // Checking which key is pressed
        if (cursors.up.isDown) {
            player.checkWorldBounds = true;
            player.body.velocity.y = -350;
        }
        if (cursors.down.isDown) {
            player.checkWorldBounds = true;
            // player.events.onOutOfBounds.add(playerOutOfBoundsBottom, this);
            player.body.velocity.y = 350;
        }
        if (cursors.left.isDown) {
            player.body.velocity.x = -350;
        }
        if (cursors.right.isDown) {
            player.body.velocity.x = 350;
        }
        if (fireButton.isDown) {
            fireBullet();
        }
    }
};
function fireBullet() {
    if (game.time.now > bulletsTime) {
        bullet = bullets.getFirstExists(false);
        if (bullet) {
            bullet.reset(player.x + 28, player.y);
            bullet.bulletAngleOffset = 90;
            bullet.bulletAngleVariance = 30;
            bullet.body.velocity.y = -400;
            bulletsTime = game.time.now + 200;
            bulletSound.play();
        }
    }
}
function playerOutOfBounds(player) {
    //  Move the Spaceship to the top of the screen again
    player.reset(player.x, game.world.centerX);
    player.reset(player.y, game.world.centerY);
}
/*function playerOutOfBoundsBottom(player) {
 // Move the spaceship to the bottom of the screen again
 player.reset(60, player.x);
 }
 */
function goFull() {
    if (game.scale.isFullScreen) {
        game.scale.stopFullScreen();
    } else {
        game.scale.startFullScreen(false);
    }
}
//id
game.state.add('mainState', mainState);
game.state.start("mainState");

最新更新