【问题标题】:How to use X and Y positions of a Sprite for "Fling" physics in Phaser 3?如何在 Phaser 3 中将 Sprite 的 X 和 Y 位置用于“Fling”物理?
【发布时间】:2020-03-07 17:43:01
【问题描述】:

我正在使用 Phaser 3,特别是 Matter 物理引擎制作游戏。我正在尝试按照这个关于投掷物理的教程https://www.emanueleferonato.com/2019/03/08/fling-unity-game-built-in-html5-with-phaser-and-matter-js-updated-with-the-latest-constraints-tweaks-fly-through-the-screen-with-your-ninja-rope/

let game;
let gameOptions = {
    gravity: 1,
    constraintSpeed: 2,
    hookSpeed: 20,
    ropeTolerance: 6
};
const BALL = 1;
const HOOK = 2;

window.onload = function() {

    let gameConfig = {
        type: Phaser.AUTO,
        scale: {
            mode: Phaser.Scale.FIT,
            autoCenter: Phaser.Scale.CENTER_BOTH,
            parent: "thegame",
            width: 1200,
            height: 750
        },
        scene: playGame,
        physics: {
            default: "matter",
            matter: {
                gravity: {
                    y: gameOptions.gravity
                },
                debug: true
            }
        }
    }
    game = new Phaser.Game(gameConfig);
    window.focus();
};
class playGame extends Phaser.Scene{
    constructor(){
        super("PlayGame");
    }
    preload() {
        //Load background image
        this.load.image('background', 'images/background.png');

        this.load.atlas('sheet', 'assets/spiderTP.png', 'assets/spiderTP.json');
        this.load.json('shapes', 'assets/spritesPE.json');
    }
    create() {
        //Place background image
        let background = this.add.image(600, 375, 'background');

        //Update the game at 30Hz
        this.matter.world.update30Hz();

        //Get hitboxes
        var shapes = this.cache.json.get('shapes');

        //Set boundaries for physics
        this.matter.world.setBounds(0, 0, game.config.width, 800);

        //Player
        this.hero = this.matter.add.sprite(600, 500, 'sheet', '0001', {shape: shapes.s0001})
        this.hero.label = BALL;
        this.hook = null;

        //Fire the hook
        this.input.on("pointerdown", this.fireHook, this);

        this.rope = null;

        //Listen for collisions with the hook
        this.matter.world.on("collisionstart", function(e, b1, b2){
            if((b1.label == HOOK || b2.label == HOOK) && !this.rope) {
                Phaser.Physics.Matter.Matter.Body.setStatic(this.hook, true);
                let distance = Phaser.Math.Distance.Between(this.hero.body.position.x, this.hero.body.position.y, this.hook.position.x, this.hook.position.y);

                if (distance > gameOptions.heroSize * 2) {
                    this.rope = this.matter.add.constraint(this.hero, this.hook, distance, 0.1);
                }
            }
        }, this);
    }

    fireHook(e) {
        if(this.hook) {
            this.releaseHook();
        }
        else {
            let angle = Phaser.Math.Angle.Between(this.hero.body.position.x, this.hero.body.position.y, e.position.x, e.position.y);

            this.hook = this.matter.add.rectangle(this.hero.body.position.x + 40 * Math.cos(angle), this.hero.body.position.y + 40 * Math.sin(angle), 10, 10);
            this.hook.label = HOOK;

            Phaser.Physics.Matter.Matter.Body.setVelocity(this.hook, {
                x: gameOptions.hookSpeed * Math.cos(angle),
                y: gameOptions.hookSpeed * Math.sin(angle)
            });
        }
    }

    releaseHook() {
        if(this.rope) {
            this.matter.world.removeConstraint(this.rope);
            this.rope = null;
        }
        if(this.hook){
            this.matter.world.remove(this.hook);
            this.hook = null;
        };
    }
    update() {
        // is there a constraint? Shrink it
        if(this.rope){
            this.rope.length -= gameOptions.constraintSpeed;
            let hookPosition = this.hook.position;
            let distance = Phaser.Math.Distance.Between(hookPosition.x, hookPosition.y, this.hero.body.position.x, this.hero.body.position.y);
            if(distance - this.rope.length > gameOptions.ropeTolerance){
                this.rope.length = distance;
            }
            this.rope.length = Math.max(this.rope.length, gameOptions.heroSize * 2);
        }
    }
}

编辑-我越来越近了。现在没有错误,但是没有射出绳子

【问题讨论】:

  • “没有绳索被击中”是什么意思?我需要更多上下文和解释来帮助您调试。
  • @ManuelAbascal 阻挡或“钩子”被击中并粘住,但是,应该在玩家和钩子之间攻击的绳索没有出现。这样更有意义吗
  • 我在聊天中回复了你

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


【解决方案1】:

您可以像这样获得sprite位置X和Y:

hero.body.position.x
hero.body.position.y

【讨论】:

  • 还是没有运气。我仍然收到game.js:88 Uncaught TypeError: Cannot read property 'x' of undefined at playGame.fireHook (game.js:88) at initialize.h.emit (phaser.min.js:1) at initialize.processDownEvents (phaser.min.js:1) at initialize.update (phaser.min.js:1) at initialize.updateInputPlugins (phaser.min.js:1) at initialize.queueMouseDown (phaser.min.js:1) at HTMLCanvasElement.onMouseDown (phaser.min.js:1)
  • @RobertSmith 我现在正在工作,如果你把它放在你的 github 仓库中,我今晚可以看看。让我知道...
  • 我能做到。我会在更新后立即通知您。谢谢
  • github.com/roberto257/Phaser-Spiderman/tree/master/… 这是仓库。 game.js 是我试图用精灵复制和实现的代码,而 spiderman_game.js 是当前代码。再次感谢
  • 我无意打扰,只是想让您了解最新情况。现在,我的精灵射球,但不射球后面的绳子。 repo 的当前代码中没有显示错误。
【解决方案2】:

让我们看看this.hero

"Cannot read property 'x' of undefined" 表示 this.hero.position.x 中 x 之前所述的属性未找到,并且可能找到了不存在于其父属性中。在这种情况下,this.hero 不包含 position 属性。

我们可以在代码运行时在控制台中输入this.hero的属性:

您可以看到 position 作为可访问属性不存在。

在您的情况下,您需要使用 body 属性。

此属性包括 position 属性,该属性包含 x 和 y 坐标。

因此,作为结论,使用以下行访问 this.hook 的 x 和 y 坐标:

this.hero.body.position.x
this.hero.body.position.y

(这个答案是对这个问题的first revision的回应)

【讨论】:

  • 还是不走运。我仍然收到game.js:88 Uncaught TypeError: Cannot read property 'x' of undefined at playGame.fireHook (game.js:88) at initialize.h.emit (phaser.min.js:1) at initialize.processDownEvents (phaser.min.js:1) at initialize.update (phaser.min.js:1) at initialize.updateInputPlugins (phaser.min.js:1) at initialize.queueMouseDown (phaser.min.js:1) at HTMLCanvasElement.onMouseDown (phaser.min.js:1)
  • 在某处添加console.log(this.hero),看看this.hero是否有body属性,以及其中的position属性。
猜你喜欢
  • 2022-08-18
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
相关资源
最近更新 更多