【问题标题】:Phaser moving spritesPhaser 移动精灵
【发布时间】:2017-05-27 16:27:03
【问题描述】:

我有几个水平排列的精灵。使用的物理引擎是P2。在更新循环中,我为每个精灵设置速度:

sprite.body.velocity.x = 150;

然后我清除每个精灵的形状并加载我的自定义多边形:

sprite.body.clearShapes();
sprite.body.loadPolygon('physicsData', 'sprite1');

加载多边形后,精灵开始以不同的速度(不同的图像)移动。为什么会这样?当我不加载多边形时 - 一切正常,精灵以相同的速度移动。

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

    我想它与多边形有关,也就是说,在创建它们时。但是我不明白,你说它们都以不同的速度移动?他们有没有联系?因为我已经从 Phaser 示例中实现了一个简单的代码,并且两个多边形以相同的速度移动而没有逻辑上的接触。

    var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
    
    function preload() {
    
       game.load.image('contra2', 'contra2.png');
       game.load.image('bunny', 'bunny.png');
    
       //   Load our physics data exported from PhysicsEditor
       game.load.physics('physicsData', 'sprites.json');
    
    }
    
    var contra, bunny;
    
    function create() {
    
       //   Enable p2 physics
       game.physics.startSystem(Phaser.Physics.P2JS);
    
       contra = game.add.sprite(400, 300, 'contra2');
       bunny = game.add.sprite(100, 300, 'bunny');
    
       //   Enable the physics body on this sprite and turn on the visual debugger
       game.physics.p2.enable(contra, true);
       game.physics.p2.enable(bunny, true);
    
       //   Clear the shapes and load the 'contra2' polygon from the physicsData JSON file in the cache
       contra.body.clearShapes();
       contra.body.loadPolygon('physicsData', 'contra2');
    
       bunny.body.clearShapes();
       bunny.body.loadPolygon('physicsData', 'bunny');
    
       //   Just starts it rotating
       game.input.onDown.add(function() { start = true; }, this);
    
       cursors = game.input.keyboard.createCursorKeys();
    
    }
    
    function update() {
    
       contra.body.setZeroVelocity();
       bunny.body.setZeroVelocity();
    
       if (cursors.left.isDown)
       {
           contra.body.moveLeft(200);
           bunny.body.moveLeft(200);
       }
       else if (cursors.right.isDown)
       {
           contra.body.moveRight(200);
           bunny.body.moveRight(200);
       }
    
       if (cursors.up.isDown)
       {
           contra.body.moveUp(200);
           bunny.body.moveUp(200);
       }
       else if (cursors.down.isDown)
       {
           contra.body.moveDown(200);
           bunny.body.moveDown(200);
       }
    
    }
    

    Examples of P2 Polygons

    Examples of P2 Movements

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 2018-03-21
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2020-06-01
      • 1970-01-01
      相关资源
      最近更新 更多