【问题标题】:How to change the hitbox's shape of a image in Phaser 3?如何更改 Phaser 3 中图像的 hitbox 形状?
【发布时间】:2020-02-19 05:58:02
【问题描述】:

我正在使用 Phaser 3 框架在 JavaScript 中创建一个滑板游戏。我已经将斜坡图像加载到屏幕上,并且我目前在我的文件中使用“街机”物理引擎。我知道我必须使用物质物理引擎来拥有一个非矩形的碰撞箱。如何用三角形的 hitbox 实现它?

我有斜坡的 .png 图像文件,以及它的 hitbox 的 .json 文件。

我尝试按照网站上的教程学习如何为物质物理引擎创建新的碰撞箱。最后一切都从屏幕上掉下来了,我不知道如何将 .json 文件用于坡道。

//Configurations for the physics engine
var physicsConfig = {
    default: 'arcade',
    arcade : {
        debug : true  //CHANGE THIS TO TRUE TO SEE LINES
    }
}

//Game configurations
var config = {
    type: Phaser.AUTO,
    width: 1200 ,
    height: 600,
    physics: physicsConfig,
    scene: {
        preload: preload,
        create: create,
        update: update
    }   
}

//Start the game
var game = new Phaser.Game(config);

function preload() {
    //Images
    this.load.image('sky', 'archery_assets/images/sky.png');
    this.load.image('ground', 'skate_assets/images/ground.png');
    this.load.image('up_ramp', 'skate_assets/images/up_ramp.png')

    //Spritesheets
    this.load.spritesheet('player', 'skate_assets/spritesheets/skater.png', {frameWidth: 160, frameHeight: 160});

}

function create() {
    //Background
    skyImg = this.add.image(600, 300, 'sky');
    //Scale the images
    skyImg.setDisplaySize(1200, 600);
    groundImg = this.add.image(600, 600, 'ground');
    groundImg.setDisplaySize(1200, 250);

    //Create the player
    this.player = this.physics.add.sprite(100, 410, 'player');
    this.player.setCollideWorldBounds(true);

    //Rolling animation 
    this.anims.create({
        key: 'move',
        frames: this.anims.generateFrameNumbers('player', {start: 0, end: 3}),
        frameRate: 16,
        repeat: -1 // <-- keeps the rolling animation going
    });
    //Pushing animation
    this.anims.create({
        key: 'push',
        frames: this.anims.generateFrameNumbers('player', {start: 4, end: 8}),
        frameRate: 16
    });
    //Start and keep the rolling animation going
    this.player.anims.play('move', true);

    //Up ramp (1st ramp)
    this.upRamp = this.physics.add.sprite(700, 330, 'up_ramp');
    this.upRamp.setSize(320, 150).setOffset(0, 175);
    this.upRamp.enableBody = true;
    this.upRamp.setImmovable();
    this.upRamp.body.angle = 150;

    //Input
    this.cursors = this.input.keyboard.createCursorKeys();

    //Spacebar
    this.spacebar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);

    this.physics.add.collider(this.player, this.upRamp);
}


function update() {

    //Set variable for push speed
    var playerPushSpeed = 0;

    //If the spacebar is pressed 
    if (this.spacebar.isDown) {
        //Play the push animation
        this.player.anims.play('push', true);

        //Push speed
        playerPushSpeed += 175;

        //Move player
        this.player.setVelocityX(playerPushSpeed);   
    }

    if (this.upRamp.body.touching.left) {
        this.player.setVelocityY(-200);
    }
}

我需要知道如何实现斜坡的 .png 图像及其 .json hitbox 文件,以便玩家可以正确“骑”上斜坡。

【问题讨论】:

  • 您能否将图像托管在某个地方,以便我可以访问它们并重新创建此问题?
  • @ManuelAbascal github.com/roberto257/Olympic-Games。图片和json在skate_assets下
  • @ManuelAbascal 链接正常吗?您需要回答以提供帮助的任何其他问题,请询问
  • 嗨@Robert Smith,我现在正在工作。但我肯定会在今天晚些时候看看
  • @ManuelAbascal 不用担心,谢谢

标签: javascript collision-detection phaser-framework matter.js


【解决方案1】:

您必须使用 physics: { default: 'matter' } 来更改 hitbox 的形状。使用此代码 sn-p 供参考:

var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    backgroundColor: '#000000',
    parent: 'phaser-example',
    physics: {
        default: 'matter',
        matter: {
            gravity: {
                y: 0
            },
            debug: true
        }
    },
    scene: {
        create: create
    }
};

var game = new Phaser.Game(config);

function create ()
{
    this.matter.world.setBounds();

    this.matter.add.rectangle(200, 200, 200, 200, { 
        chamfer: { radius: [230, 0, 200, 0 ] }
    });

    this.matter.add.mouseSpring();
}

您也可以使用 PhysicsEditor,您可以check this tutorial了解如何实现不同的形状。

编辑:

您可以使用 console.log(this.matter.bodies) 来检查要实现的其他可用形状。

【讨论】:

  • 谢谢。我一直在看那个试图实现它的教程。 “mouseSpring()”是做什么的?除了矩形,你还能实现形状吗?
  • 我现在可以使用它了。唯一的问题是,我如何设置精灵使它们不能移动?例如,在街机物理引擎中,它是“setImmovable()”
  • @RobertSmith 你删除了this.matter.add.mouseSpring();吗?
  • 会的,我将不得不重新创建它,因为我报废了它
  • 我在你发布的聊天室里给你发了一条消息
猜你喜欢
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多