Search code examples
coronasdk

In Corona SDK the background image always cover other images


I'm currently making a tower defense game with Corona SDK. However, while I'm making the gaming scene, The background scene always cover the monster spawn, I've tried background:toBack() ,however it's doesn't work.Here is my code:

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

and the monster just stuck at the spawn point and stay there. enter image description here

but, When i comment these 3 lines of code:

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

The problem disappear enter image description here

Any ideas??Thanks for helping


Solution

  • Since you are using director, you should insert all your display objects into the localGroup. You haven't inserted bg into localGroup.

    SO director class inserts bg finally after inserting localGroup.

    Modify your code as

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

    or add the code

     localGroup:insert(bg)