2012-07-08 118 views
2

我目前正在用Corona SDK製作塔防遊戲。然而,當我在做遊戲的場景,背景場景總是覆蓋怪物的刷新,我已經試過background:toBack(),但是它不work.Here是我的代碼:在Corona SDK中,背景圖片總是覆蓋其他圖片

module(..., package.seeall) 

function new() 
    local localGroup = display.newGroup(); 

    local level=require(data.levelSelected); 

    local currentDes = 1; 

    monsters_list = display.newGroup() 

    --The background 
    local bg = display.newImage ("image/levels/1/bg.png"); 
    bg.x = _W/2;bg.y = _H/2; 
    bg:toBack(); 

    --generate the monsters 
    function spawn_monster(kind) 
     local monster=require("monsters."..kind); 
     newMonster=monster.new() 
     --read the spawn(starting point) in level, and spawn the monster there 
     newMonster.x=level.route[1][1];newMonster.y=level.route[1][2]; 
     monsters_list:insert(newMonster); 
     localGroup:insert(monsters_list); 
     return monsters_list; 
    end 

    function move(monster,x,y) 
     -- Using pythagoras to calauate the moving distace, Hence calauate the time consumed according to speed 
     transition.to(monster,{time=math.sqrt(math.abs(monster.x-x)^2+math.abs(monster.y-y)^2)/(monster.speed/30),x=x, y=y, onComplete=newDes}) 
    end 

    function newDes() 
     currentDes=currentDes+1; 
    end 

    --moake monster move according to the route 
    function move_monster() 
     for i=1,monsters_list.numChildren do 
      move(monsters_list[i],200,200); 
      print (currentDes); 
     end 

    end 

    function agent() 
     spawn_monster("basic"); 
    end 

    --Excute function above. 
    timer2 = timer.performWithDelay(1000,agent,10); 
    timer.performWithDelay(100,move_monster,-1); 
     timer.performWithDelay(10,update,-1); 
    move_monster(); 

    return localGroup; 
end 

和怪物正好卡在重生點並留在那裏。 enter image description here

但是,當我評論這3行代碼:

--local bg = display.newImage ("image/levels/1/bg.png"); 
--bg.x = _W/2;bg.y = _H/2; 
--bg:toBack(); 

問題消失 enter image description here

??感謝任何想法,幫助

+0

也許你是多次產卵的背景?嘗試在產生背景檢查後立即添加'print(「衍生」)。只是一個想法,可能不是解決方案。 – Jutanium 2012-07-08 10:31:51

+0

我試過了,但它不起作用,無論如何,謝謝你的建議! – 2012-07-08 11:46:03

+0

這個問題仍然很好,每個人都會遇到這個問題。但接受的答案已過時。 Corona Labs在2014年推出了用於場景管理的Composer API(https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/)。[我的回答](http ://stackoverflow.com/a/42976328/6286781)更新了作曲家接受的答案。 – GoojajiGreg 2017-03-23 12:44:26

回答

8

由於您使用的主任,您應該將所有顯示對象插入到localGroup中。 您還沒有將bg插入localGroup。

SO director類插入localGroup後最終插入bg。

修改代碼爲

--The background 

    local bg = display.newImage (localGroup,"image/levels/1/bg.png"); 
    bg.x = _W/2;bg.y = _H/2; 
    bg:toBack(); 

或添加代碼

localGroup:insert(bg) 
+2

非常感謝您的建議!這是工作,我有15個聲望後,我會爲你投票! – 2012-07-08 11:43:25

+0

當然!別擔心。 您可以同時接受答案:) – SatheeshJM 2012-07-08 12:13:10

+0

Thank you.its工作.. :) – 2014-03-05 09:57:07

0

在較新版本科羅娜SDK的:

Composer是官方的場景(屏幕)的創建和Corona SDK中的管理庫.... Composer庫中的主要對象是scene對象...並且它包含au nique self.view ....這個self.view是你應該插入有關場景的視覺元素。

因此,現在在您的scene:create()方法中,您應該將所有DisplayObject插入到self.view中。它看起來像這樣:

local composer = require("composer") 
local scene = composer.newScene() 

function scene:create() 

    local sceneGroup = self.view 

    local bg = display.newImage(... 
    bg.x, bg.y = ... 

    local dude = display.newImage(... 
    dude.x, dude.y = .... 

    sceneGroup:insert(bg) 
    sceneGroup:insert(dude) 

end 

的DisplayObject在此sceneGroup的分層取決於它們被添加到組,與對象最後添加在上面的順序。您可以使用toBack()toFront()方法來控制此訂購。