My first Corona program - criticism please

Here's the main reason I switched to Corona from my previous software. I wanted more flexibility - and to control depth. I want to have two characters that moved vertically and swap depth as they crossed. Here's my horrendously verbose code to do that.

I have to say I really like Corona and will be subscribing. I started Corona this week and have been following Peach's superb tutorials.

Oh and if anyone has a few secs to tear my code apart I'd really appreciate it.

Here goes...

-- Hide status bar
display.setStatusBar(display.HiddenStatusBar);

-- set up variables
local zombie_speed = 1;
local zombie1_layer = "nothing";

-- Set up background
local background = display.newImage("background.png");

-- set up test text

local test_text = display.newText("Zombie 1 y: ", 0, 0, nil, 15);
test_text:setReferencePoint(display.TopLeftReferencePoint);
test_text.x = 10;
test_text.y = 10;

local test_text2 = display.newText("Zombie 1 Layer: ", 0, 0, nil, 15);
test_text2:setReferencePoint(display.TopLeftReferencePoint);
test_text2.x = 10;
test_text2.y = 30;

local test_text3 = display.newText("Zombie 2 Layer: ", 0, 0, nil, 15);
test_text3:setReferencePoint(display.TopLeftReferencePoint);
test_text3.x = 10;
test_text3.y = 50;

-- Set up images

local zombie1 = display.newImage("zombie.png");
zombie1:setReferencePoint(display.TopLeftReferencePoint);
zombie1.x = 240;

local zombie2 = display.newImage("zombie.png");
zombie2:setReferencePoint(display.TopLeftReferencePoint);

zombie2.x = 240;
zombie2.y = display.contentHeight - zombie2.height;

-- Create 10 groups - one for each layer
-- group 1 is the farthest back

local layer1 = display.newGroup();
local layer2 = display.newGroup();
local layer3 = display.newGroup();
local layer4 = display.newGroup();
local layer5 = display.newGroup();
local layer6 = display.newGroup();
local layer7 = display.newGroup();
local layer8 = display.newGroup();

-- create function to check zombie1s depth
-- and correct it if needs be

local function set_zombie1_depth()
--layer 1
if(zombie1.y > 0 and zombie1.y < 25) then
if(zombie1_layer ~= 1) then
layer1:insert(zombie1);
zombie_1_layer = 1;
end
end

--layer 2
if(zombie1.y > 25 and zombie1.y < 50) then
if(zombie1_layer ~= 2) then
layer2:insert(zombie1);
zombie_1_layer = 2;
end
end

--layer 3
if(zombie1.y > 50 and zombie1.y < 75) then
if(zombie1_layer ~= 3) then
layer3:insert(zombie1);
zombie_1_layer = 3;
end
end

--layer 4
if(zombie1.y > 75 and zombie1.y < 100) then
if(zombie1_layer ~= 4) then
layer4:insert(zombie1);
zombie_1_layer = 4;
end
end

--layer 5
if(zombie1.y > 100 and zombie1.y < 125) then
if(zombie1_layer ~= 5) then
layer5:insert(zombie1);
zombie_1_layer = 5;
end
end

--layer 6
if(zombie1.y > 125 and zombie1.y < 150) then
if(zombie1_layer ~= 6) then
layer6:insert(zombie1);
zombie_1_layer = 6;
end
end

--layer 7
if(zombie1.y > 150 and zombie1.y < 175) then
if(zombie1_layer ~= 7) then
layer7:insert(zombie1);
zombie_1_layer = 7;
end
end

--layer 8
if(zombie1.y > 150 and zombie1.y < 175) then
if(zombie1_layer ~= 8) then
layer8:insert(zombie1);
zombie_1_layer = 8;
end
end

end

-- create function to check zombie2s depth
-- and correct it if needs be

local function set_zombie2_depth()

--layer 1
if(zombie2.y > 0 and zombie2.y < 25) then
if(zombie2_layer ~= 1) then
layer1:insert(zombie2);
zombie_2_layer = 1;
end
end

--layer 2
if(zombie2.y > 25 and zombie2.y < 50) then
if(zombie2_layer ~= 2) then
layer2:insert(zombie2);
zombie_2_layer = 2;
end
end

--layer 3
if(zombie2.y > 50 and zombie2.y < 75) then
if(zombie2_layer ~= 3) then
layer3:insert(zombie2);
zombie_2_layer = 1;
end
end

--layer 4
if(zombie2.y > 75 and zombie2.y < 100) then
if(zombie2_layer ~= 4) then
layer4:insert(zombie2);
zombie_2_layer = 4;
end
end

--layer 5
if(zombie2.y > 100 and zombie2.y < 125) then
if(zombie2_layer ~= 5) then
layer5:insert(zombie2);
zombie_2_layer = 5;
end
end

--layer 6
if(zombie2.y > 125 and zombie2.y < 150) then
if(zombie2_layer ~= 6) then
layer6:insert(zombie2);
zombie_2_layer = 6;
end
end

--layer 7
if(zombie2.y > 150 and zombie2.y < 175) then
if(zombie2_layer ~= 7) then
layer7:insert(zombie2);
zombie_2_layer = 7;
end
end

--layer 8
if(zombie2.y > 175 and zombie2.y < 200) then
if(zombie2_layer ~= 8) then
layer8:insert(zombie2);
zombie_2_layer = 8;
end
end

end

-- Function to update test text each frame

local function update_test_text()

test_text.text = "Zombie 1 y: " .. zombie1.y;
test_text:setReferencePoint(display.TopLeftReferencePoint);
test_text.x = 10;

test_text2.text = "Zombie 1 layer: " .. zombie_1_layer;
test_text2:setReferencePoint(display.TopLeftReferencePoint);
test_text2.x = 10;
test_text2.y = 30;

test_text3.text = "Zombie 2 layer: " .. zombie_2_layer;
test_text3:setReferencePoint(display.TopLeftReferencePoint);
test_text3.x = 10;
test_text3.y = 50;

end

-- Create function to make zombies move

local function move_zombie1()
if((zombie1.y + zombie1.height) < 320) then
zombie1.y = zombie1.y +zombie_speed ;
end
end

local function move_zombie2()
if(zombie2.y > 0) then
zombie2.y = zombie2.y - zombie_speed ;
end
end

-- Now make event listeners for enterFrame

Runtime:addEventListener("enterFrame", move_zombie1);
Runtime:addEventListener("enterFrame", move_zombie2);

Runtime:addEventListener("enterFrame", set_zombie1_depth);
Runtime:addEventListener("enterFrame", set_zombie2_depth);
Runtime:addEventListener("enterFrame", update_test_text);

Have a read through this, might help with your logic :)
http://lua-users.org/wiki/SwitchStatement

Its a bit long for me to actually figure out what you're doing (I'm at work, and I really shouldn't even be looking at this), but you can tidy up your code a bit (functionally equivalent)...

test_text3.x = 10;
test_text3.y = 50;

can be written as

test_text3.x,test_text3.y = 10,50;

you could use a function to set them too, like this

1
2
3
4
5
6
7
8
function setPosition(object,x,y)
object:setReferencePoint(display.TopLeftReferencePoint);
object.x,object.y = x,y
end
 
setPosition(test_text1,20,30)
setPosition(test_text2,40,50)
setPosition(test_text3,10,50)

Hi innominata

Thanks for checking it out. That helps a lot - will read the link you sent too. Well, tomorrow when my eyes are working again. ;-)

Thanks

Tom

if you're only using it in that file, use

1
local function ...
views:1985 update:2011/9/30 9:15:39
corona forums © 2003-2011