【问题标题】:how can I change velocity of a child of a physics.group in phaser.js?如何在 phaser.js 中更改物理组的孩子的速度?
【发布时间】:2022-08-03 12:10:40
【问题描述】:

我正在使用 Phaser 制作一个游戏,看起来像这样:

玩家必须接住鸡蛋,所以鸡蛋(由gameState.eggs = this.physics.add.group();制成)在坡道上具有一定的velocity,但是一旦它们离开坡道,我想自动将setVelocity()变为一个x 坐标为 0,而不是只在屏幕上拍摄。

这是我的鸡蛋生成功能:

function eggGen() {
let num = Math.random();

let xCoord, yCoord, eggDirection, eggAnimation, velocityX

if (num < .5) {
    xCoord = 100;

    eggDirection = \'eggLeft\';
    eggAnimation = \'rollingLeft\'
    
    velocityX = this.velocityX;
    
    if (num < .25) {
        yCoord = 232;

    } else {
        yCoord = 382;
    }
} else {
    xCoord = 700;

    eggDirection = \'eggRight\';
    eggAnimation = \'rollingRight\';

    velocityX = -(this.velocityX)

    if (num < .75) {
        yCoord = 232;

    } else {
        yCoord = 382;
    }
}

let egg = gameState.eggs.create(xCoord, yCoord, eggDirection).setVelocity(velocityX, this.velocityY).setScale(.6);

if (egg.x > 220 && egg.x < 580) {
    egg.setVelocity(0, this.velocityY);

}

egg.anims.play(eggAnimation);
}

最后一个条件是我希望做的魔术,但它没有做任何事情。澄清一下,eggGen 函数在 this.time.addEvent(); 内部调用

    标签: javascript phaser-framework


    【解决方案1】:

    在不知道你的代码的情况下(并假设使用街机物理), 我会:

    只需签入update 函数,现场的,如果一个鸡蛋“在斜坡上”并且有一个x-速度0

      function update(){
           // ...
           gameState.eggs.getChildren().forEach(egg => {
               if(egg.velocity.x > 0 && (egg.x > 220 || egg.x < 580)) {
                   // ... stop velocity.x or set the whole velocity new
                   egg.velocity.x = 0;
               }
           });
           // ...
      }
    

    这是一个迷你演示:
    它只涵盖了基础知识

    document.body.style = 'margin:0;';
    
    var config = {
        type: Phaser.AUTO,
        width: 300,
        height: 183,
        physics: {
            default: 'arcade',
            arcade: { 
                debug: true,
            }
        },
        scene: {
            create,
            update
        },
        banner: false
    }; 
    
    let objectGroup;
    
    function create () {
        objectGroup = this.physics.add.group();
        this.time.addEvent({ delay: 500, callback: createObject, callbackScope: this, loop: true });
    }
    
    function createObject(){
        let spawnLeft = Phaser.Math.Between(0, 1);
        let obj = this.add.rectangle(spawnLeft ? 0 : config.width, 0,  10, 10, 0xff0000);
        this.physics.add.existing(obj);
        
        objectGroup.add(obj);
        obj.body.setVelocity((spawnLeft ? 1 : -1) * 75, 30);   
    
    }
    
    function update(){
         if(!objectGroup)
             return;
       
         objectGroup.getChildren().forEach(obj =>{
             if(obj.body.velocity.x > 0 && (obj.x > 100 && obj.x < 150) || (obj.x > config.width - 150 && obj.x < config.width - 100) ){
                 // Just to keep the same speed, even after changing direction
                 let speed = obj.body.velocity.length();
                 obj.body.velocity.x = 0;
                 obj.body.velocity.y = speed;
             }
         });
    }
    
    new Phaser.Game(config);
    &lt;script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"&gt;&lt;/script&gt;

    【讨论】:

    • 尽管您的示例表明它有效,但它不适合我。可能与eggGenthis.time.addEvent(); 中被调用的事实有关
    • @chylinski,我想帮忙,但如果没有看到你的代码,很难说。浏览器控制台中可能有错误吗?
    • @chylinski 使用 addEvent 时,您是否将上下文传递给 callbackScope: this,
    • 是的,我通过callbackScope: this,你第一次是对的,我按照你的建议将它固定在update,但解决方案略有不同。多谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2018-10-24
    • 2011-09-11
    • 2017-07-02
    相关资源
    最近更新 更多