2015-03-02 93 views
0
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create}); 

function preload() { 

    // You can fill the preloader with as many assets as your game requires 

    // Here we are loading an image. The first parameter is the unique 
    // string by which we'll identify the image later in our code. 

    // The second parameter is the URL of the image (relative) 
} 

function create() { 

    // This creates a simple sprite that is using our loaded image and 
    // displays it on-screen 
    // and assign it to a variable 

    var image = game.add.sprite(0, 0, 'einstein'); 
    game.physics.enable(image, Phaser.Physics.ARCADE); 
    image.body.velocity.x=150; 

    //my attempt below 
    if(image.body.coordinate.x > 1){ 
    var image = game.add.sprite(0, 0, 'einstein'); 
    } 
} 

我目前正尝试将图像精灵重置为坐标0,0,当它离开屏幕时完全离开屏幕,所以基本上一旦它离开画布就会重置并重置几个小时尝试了很多东西任何提示将不胜感激。正如我一直试图主动获取坐标,以便它知道何时在第一个图像消失时加载图像。如何在到达框架结尾时将其位置重置回原点? javascript

感谢

回答

1

我认为创建()方法,当你创建它只被调用。这样if语句只被调用一次。你需要多次检查(例如每一帧)。

它显示阶段有一个类,检查你是否超出界限。 (onOutOfBounds)尝试:

function create() { 

    // This creates a simple sprite that is using our loaded image and 
    // displays it on-screen 
    // and assign it to a variable 

    var image = game.add.sprite(0, 0, 'einstein'); 
    game.physics.enable(image, Phaser.Physics.ARCADE); 
    image.body.velocity.x=150; 

    image.checkWorldBounds= true; 
    image.events.onOutOfBounds.add(resetimage, this); 

    } 
    function resetimage(image) { 

    image.reset(image.x, image.y); 
    } 

编辑:前面的例子不适用于精灵。改用onOutOfBounds。

编辑:链接到一个工作示例:Phaser out of bounds

+0

如何做任何提示? – 2015-03-02 20:33:54

+0

它似乎只检查一次,它总是输出越界 – 2015-03-02 21:15:04

+0

雅它仍然只是越界并不会重置 – 2015-03-02 21:21:16

相关问题