首页 文章

如何阻止精灵尺寸变化停止Phaser中的碰撞检测?

提问于
浏览
2

我正在使用Phaser开发一个小型JavaScript游戏,我有一个精灵在某些点改变它的大小 . 它使用sprite.body.setSize方法执行此操作 . 但是,它看起来像精灵在更改大小时会停止与应该碰撞的对象发生碰撞 . 我理解为什么会这样做,因为精灵's boundaries are in a state of flux during a change of size, but I'害怕我的用户可以利用这个问题并穿过墙壁 . 我在改变大小的过程中 . 有办法防止这种情况吗?

Edit at Supamiu's request:

Here is a quick example我正在尝试在游戏中做什么 . 此外,这是该示例的源代码 .

// Global constants
var GAME_WIDTH = 800;
var GAME_HEIGHT = 600;

var TEXT_X_POS = 150;
var TEXT_Y_POS = 50;

var PHASER_DUDE_WIDTH = 27;
var PHASER_DUDE_HEIGHT = 40;

var MASTER_WIDTH = 57;
var MASTER_HEIGHT = 77;

var SPRITE_X_POS = 200;
var SPRITE_Y_POS = 400;

var SPRITE_GRAVITY = 300;

var LEFT_VELOCITY = -300;
var RIGHT_VELOCITY = 300;
var JUMP_VELOCITY = -300;
var STOPPED = 0;

var WALL_X_POS = 500;
var WALL_Y_POS = 300;

// Global variables
var sprite;
var wall;
var cursors;

var game = new Phaser.Game(GAME_WIDTH, GAME_HEIGHT, Phaser.AUTO, "game", {preload: preload, create: create, update: update});

function preload () {
    game.load.image("master", "sprites/master.png");
    game.load.image("phaser dude", "sprites/phaser_dude.png");
    game.load.image("wall", "sprites/wall.png");
}

function create () {
    game.add.text(TEXT_X_POS, TEXT_Y_POS, "Use the cursor keys to jump and move", {fontSize: "16px", fill: "white"});

    sprite = game.add.sprite(SPRITE_X_POS, SPRITE_Y_POS, "master");
    game.physics.arcade.enable(sprite);
    sprite.body.collideWorldBounds = true;
    sprite.body.gravity.y = SPRITE_GRAVITY;

    wall = game.add.sprite(WALL_X_POS, WALL_Y_POS, "wall");
    game.physics.arcade.enable(wall);
    wall.body.immovable = true;

    cursors = game.input.keyboard.createCursorKeys();
}

function update () {
    game.physics.arcade.collide(sprite, wall);

    sprite.body.velocity.x = STOPPED;

    cursors.up.onDown.add(jump);
    if (cursors.left.isDown) {
        sprite.body.velocity.x = LEFT_VELOCITY
    }
    if (cursors.right.isDown) {
        sprite.body.velocity.x = RIGHT_VELOCITY;
    }

    if (sprite.isJumping && sprite.body.onFloor()) {
        sprite.isJumping = false;
        sprite.loadTexture("master");
        sprite.body.setSize(MASTER_WIDTH, MASTER_HEIGHT);
    }
}

function jump () {
    sprite.isJumping = true;
    sprite.body.velocity.y = JUMP_VELOCITY;

    sprite.loadTexture("phaser dude");
    sprite.body.setSize(PHASER_DUDE_WIDTH, PHASER_DUDE_HEIGHT);
}

您可以看到,如果您在精灵更改大小(和纹理)时按下墙的左侧,它可以穿过墙壁 .

1 回答

  • 1

    如果你不想让用户使用它,如果他们试图通过它,让他们回到墙的开头 . 像这样:

    U是用户||是一堵墙

    ||
    U    ||
         ||
         ||
    

    开始

    ||
    |U|
    ||
    ||
    

    哦,你正在穿墙!

    ||
    U ||
      ||
      ||
    

    你走到了墙的起点 .

    或者您可以在更改尺寸时让用户无法移动

    编辑:

    还有另外两个解决方案,使用带有0值的边界系统设置墙壁以进行弹跳:

    function create () {
        game.add.text(TEXT_X_POS, TEXT_Y_POS, "Use the cursor keys to jump and move", {fontSize: "16px", fill: "white"});
    
        sprite = game.add.sprite(SPRITE_X_POS, SPRITE_Y_POS, "master");
        game.physics.arcade.enable(sprite);
        sprite.body.collideWorldBounds = true;
        sprite.body.gravity.y = SPRITE_GRAVITY;
    
        wall = game.add.sprite(WALL_X_POS, WALL_Y_POS, "wall");
        game.physics.arcade.enable(wall);
        wall.body.immovable = true;
        //here is what i added
        wall.body.collideWorldBounds = true;
        wall.body.bounce.set(0);
    
        cursors = game.input.keyboard.createCursorKeys();
    }
    

    或者在改变大小时使精灵体变得不动:

    function update () {
        game.physics.arcade.collide(sprite, wall);
    
        sprite.body.velocity.x = STOPPED;
    
        cursors.up.onDown.add(jump);
        if (cursors.left.isDown) {
            sprite.body.velocity.x = LEFT_VELOCITY
        }
        if (cursors.right.isDown) {
            sprite.body.velocity.x = RIGHT_VELOCITY;
        }
        if (sprite.isJumping && sprite.body.onFloor()) {
            sprite.isJumping = false;
            sprite.loadTexture("master");
            //Here is what i added
            sprite.body.velocity.x = STOPPED;
            sprite.body.setSize(MASTER_WIDTH, MASTER_HEIGHT);
        }
    }
    

相关问题