向后移动平台的代码

时间:2019-07-25 19:54:57

标签: phaser-framework

我正在尝试制作一个游戏,角色可以在水平移动的平台上跳跃,一旦平台到达特定点,它就会返回并重复。但是我在寻找正确的方法来编写代码时遇到了一些麻烦,我尝试使用setVelocityX()看起来像这样

/etc

所做的所有事情就是当平台到达“ platformMaxX”点时,它刚刚在该区域中来回移动,因此仍在调用“ movingPlatform.moveRight()”

2 个答案:

答案 0 :(得分:0)

var config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,    loader: {
      baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
      crossOrigin: 'anonymous'
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    },
    physics: {
        default: 'arcade'
    }
};

var game = new Phaser.Game(config);
var dude;
var alien1, alien2;
var direction = 1

function preload ()
{
  this.load.image('dude', 'sprites/phaser-dude.png');
  this.load.image('alien1', 'sprites/phaser-alien.png');
  this.load.image('alien2', 'sprites/alien2.png');
}

function create ()
{ dude = this.physics.add.sprite(300, 100, 'dude');
  alien1 = this.physics.add.sprite(400, 100, 'alien1');
  alien1.body.immovable = true;
  alien2 = this.physics.add.sprite(100, 100, 'alien2');
  alien2.body.immovable = true;
}

function update ()
{
  this.physics.add.collider(dude, alien1, flipX, null, this);
  this.physics.add.collider(dude, alien2, flipX, null, this);
  dude.setVelocityX(direction * 100)
}

function flipX ()
{
  direction = - direction
}
<script src="//cdn.jsdelivr.net/npm/phaser@3.18.1/dist/phaser.min.js"></script>

答案 1 :(得分:0)

var config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,    loader: {
      baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
      crossOrigin: 'anonymous'
    },
    scene: {
        preload: preload,
        create: create
    },
    physics: {
        default: 'arcade'
    }
};

var game = new Phaser.Game(config);

function preload ()
{
  this.load.image('dude', 'sprites/phaser-dude.png');
}

function create ()
{ 
  this.dude = this.physics.add.sprite(100, 100, 'dude');

  const tween = this.tweens.add({
    targets: this.dude,
    x: 300,
    ease: 'Power0',
    duration: 3000,
    flipX: true,
    yoyo: true,
    repeat: -1,
  });
}
<script src="//cdn.jsdelivr.net/npm/phaser@3.19.0/dist/phaser.js"></script>