2016-10-22 38 views
0

我開發了一個Cordova Phaser遊戲。它運行在Android和iOS設備上。如何避免在使用Cordova的Phaser遊戲中使用高內存?

遊戲有七個等級,每個等級都有一些精靈(背景,玩家)和羣體(子彈,敵人)。

preload功能,我已經加載的所有圖像和atlasJSONHash

function preload(){ 
    game.load.atlasJSONHash('anim', 'anim.png', 'anim.json'); 
    //and so on 
} 

function create(){ 
    var star = game.add.sprite(160, 32, 'level1bg'); 
    star.x = 0; 
    star.y = 0; 
    star.height = game.height; 
    star.width = game.width; 

    bullets = game.add.group(); 
    bullets.enableBody = true; 
    bullets.physicsBodyType = Phaser.Physics.ARCADE; 

    bullets.createMultiple(30, 'bullet'); 
    bullets.setAll('anchor.x', 0.5); 
    bullets.setAll('anchor.y', 1); 
    bullets.setAll('outOfBoundsKill', true); 
    bullets.setAll('checkWorldBounds', true); 
    //and so on 
} 
function startlevel(level) 
{ 
    var star = game.add.sprite(160, 32, 'level1bg'); 
    star.x = 0; 
    star.y = 0; 
    star.height = game.height; 
    star.width = game.width; 

    bullets = game.add.group(); 
    bullets.enableBody = true; 
    bullets.physicsBodyType = Phaser.Physics.ARCADE; 

    bullets.createMultiple(30, 'bullet'); 
    bullets.setAll('anchor.x', 0.5); 
    bullets.setAll('anchor.y', 1); 
    bullets.setAll('outOfBoundsKill', true); 
    bullets.setAll('checkWorldBounds', true); 
    //and so on 
} 

當關卡結束我叫startlevel(2)等。

在瀏覽器中運行良好,但在移動內存中每個級別都會翻倍,應用程序最終會崩潰。我如何避免這種內存問題?

回答

1

我希望這會有所幫助。在分配一個新組之前,爲了確保舊組被銷燬,我總是調用destroy來重新分配一個新組。 移動我想你會想爲你的子彈組創建一個新的函數並重新使用它。

function createBulletGroup(){ 

    if(bullets!=null) 
    { 
     bullets.destroy(true); 
     bullets = null; 
    } 
    //.. The rest of add group 
} 

更多了,因爲你使用

var star = game.add.sprite(160, 32, 'level1bg'); 

你STARTLEVEL每一次,一個新的明星將放在老明星,因爲他們的立場是相同的。當明星創建時,它將被分配給世界。而且很多時候重新創建星星會造成眼睛看不到的內存泄漏。

當你開始新的關卡時,你可以嘗試隨機放置你的星星,看它是否與其他人重疊或將其設置爲0.5;