javascript - 需要帮助使用 phaser.js 让 Sprite 从屏幕右侧进入

标签 javascript phaser-framework

我正在学习游戏的移相器框架:http://phaser.io

这个例子演示了从屏幕左侧进入的多个 Sprite 。我只是想弄清楚如何让他们从右边进来。我缺少一些东西。

可以在这里看到工作示例:http://examples.phaser.io/_site/view_full.html?d=sprites&f=add+several+sprites.js&t=add%20several%20sprites

代码:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload:preload, create: create, update: update });

var timer = 0;
var total = 0;

function preload() {

//  37x45 is the size of each frame
//  There are 18 frames in the PNG - you can leave this value blank if the frames     fill up the entire PNG, but in this case there are some
//  blank frames at the end, so we tell the loader how many to load
game.load.spritesheet('mummy', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18);

}

function create() {

releaseMummy();

}

function releaseMummy() {

var mummy = game.add.sprite(-(Math.random() * 800), game.world.randomY, 'mummy');

mummy.scale.setTo(2, 2);

//  If you prefer to work in degrees rather than radians then you can use Phaser.Sprite.angle
//  otherwise use Phaser.Sprite.rotation
mummy.angle = game.rnd.angle();

mummy.animations.add('walk');
mummy.animations.play('walk', 20, true);

game.add.tween(mummy).to({ x: game.width + (1600 + mummy.x) }, 20000, Phaser.Easing.Linear.None, true);

total++;
timer = game.time.now + 100;

}

function update() {

if (total < 200 && game.time.now > timer)
{
    releaseMummy();
}

} 

最佳答案

您可以将 Sprite 位置设置在游戏边界后的最右边。

var mummy = game.add.sprite(800 + (Math.random() * 800), game.world.randomY, 'mummy');

并且在游戏边界后向左远的 tween。

game.add.tween(mummy).to({ x: -(1600 + mummy.x) }, 20000, Phaser.Easing.Linear.None, true);

FIDDLE DEMO

关于javascript - 需要帮助使用 phaser.js 让 Sprite 从屏幕右侧进入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22307697/

相关文章:

javascript - 动态设置TextArea宽度

javascript - Promise 和 for 循环 - 尝试验证多个元素文本

javascript - 操作系统和操作系统版本之间的浏览器是否存在差异?

javascript - 如何测试在 componentDidMount 中异步调用创建的组件?

sprite - 在移相器中使用几乎相同的 Sprite 的良好做法。使用更多 Sprite 的替代方法

javascript - 取消选择 Select2 jquery 插件中的选择选项

javascript - Phaser.io 中的 setSize

javascript - Google Closure Compiler 错误地删除了函数调用

javascript - 在 Phaser 中从位图数据加载 spritesheet

javascript - Phaser 悬停在事件上方