2015-03-02 25 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

相關問題