我目前正在使用Corona SDK制作塔防游戏.然而,当我正在制作游戏场景时,背景场景总是覆盖怪物产卵,我已经尝试过background:toBack(),但它不起作用.这是我的代码:
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
Run Code Online (Sandbox Code Playgroud)
而怪物只是停留在产卵点并留在那里.

但是,当我评论这三行代码时:
--local bg = display.newImage ("image/levels/1/bg.png");
--bg.x = _W/2;bg.y = _H/2;
--bg:toBack();
Run Code Online (Sandbox Code Playgroud)
问题消失了

任何想法??感谢您的帮助
由于您使用的是director,因此应将所有显示对象插入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();
Run Code Online (Sandbox Code Playgroud)
或添加代码
localGroup:insert(bg)
Run Code Online (Sandbox Code Playgroud)