2012-07-08 117 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()方法来控制此订购。