I'm using Corona SDK in my Lua code I'm trying to transition from one screen to the next and show objects that are hidden, alpha = 0, by simply adjusting their alpha to equal 1. When I ran my code its only working properly for the very last element in the array, but nothing is happening to the rest. My code is below.
Main:
--------------------------------------
local function test2ScreenTransition()
--Gets the files level1 & Menu for use
require "Menu"
require "level1"
--Creates a menu screen, First Screen seen
local menu = Menu:new();
--Creates level 1 screen, Creates Enemies hidden at this point
local lvl1 = level1;
lvl1.new();
--Event when the user pushes the Play button on the menu, Transitions to level 1
local onPlayButTouched = function(e)
--Hides Menu Screen
menu:hide();
--HERE IS THE ISSUE FUNCTION, Suppose to call this function and change alphas of the objects
--to 1
lvl1.show();
end
local onMenuHidded = function(e)
lvl1.isVisible = true;
end
Runtime:addEventListener ( "playButTouched", onPlayButTouched )
Runtime:addEventListener ( "menuHidden", onMenuHidded )
end
.
level1
------------------------------------------
--New Level 1 table
level1 = {};
--Array Holding Objects Needings Alpha changed
local array = {};
function level1.new()
end
--Filling Array with Objects
function level1.addEnemies()
local Enemy = require "Enemy"
local StartX = 50;
local StartY = 20;
local counter = 0;
for i=0, 17 do
array[i] = Enemy;
array[i].new({x=StartX,y=StartY});
--print(i);
if counter > 4 then
StartX = 50;
StartY = StartY+35;
counter=0;
else
StartX =StartX+35;
counter = counter+1;
end
end
end
--FUNCTION HAVING ISSUES WITH
function level1.show()
for i = 0, 17 do
array[i].show();
end
end
level1.addEnemies();
return level1;
.
Enemy
---------------------------------
enemy = {};
--Table that holds properties of Object
local Obj = {};
function enemy.new(args)
Obj.x = args.x;
Obj.y = args.y;
Obj.spriteTexFile= "MYIMG.png";
Obj.sprite = display.newImage( Obj.spriteTexFile);
Obj.sprite:setReferencePoint ( display.TopLeftReferencePoint );
Obj.sprite.x = Obj.x;
Obj.sprite.y = Obj.y;
Obj.sprite.alpha = 0;
end
--FUNCTION HAVING ISSUES WITH
function enemy.show()
Obj.sprite.alpha = 1;
end
return enemy;
I noticed that its processing the same address every time in the table. I printed out the table numbers and am noticing the addresses are the same, hence why is only one object is changing. Yet I still don't why this is happening.
You are only making one Enemy
since the code
local Obj = {};
only executes once when the Enemy module is loaded.
Move that line into the top of
function enemy.new(args)
local Obj = {};
Obj.x = args.x;
Obj.y = args.y;
Obj.spriteTexFile= "MYIMG.png";
Obj.sprite = display.newImage( Obj.spriteTexFile);
Obj.sprite:setReferencePoint ( display.TopLeftReferencePoint );
Obj.sprite.x = Obj.x;
Obj.sprite.y = Obj.y;
Obj.sprite.alpha = 0;
return Obj
end
and add return Obj
at the end as I've indicated above.
Also, instead of
array[i] = Enemy;
array[i].new({x=StartX,y=StartY});
you must
array[i] = Enemy.new({x=StartX,y=StartY});
Otherwise the array[i]
is set to the class, not the instance.
Now, in your version
function enemy.show()
Obj.sprite.alpha = 1;
end
you are setting the one and only Enemy's alpha. You need to change this to take an argument with the instance you want to change.
It looks like you are trying to create classes and instances. Perhaps you should read this web page on SimpleLuaClasses.
In the style of that approach, you could do this:
enemy.__index = enemy
function enemy.new(args)
local Obj = {};
setmetatable(Obj,enemy) -- make Obj an instance of Enemy
Obj.x = args.x;
Obj.y = args.y;
Obj.spriteTexFile= "MYIMG.png";
Obj.sprite = display.newImage( Obj.spriteTexFile);
Obj.sprite:setReferencePoint ( display.TopLeftReferencePoint );
Obj.sprite.x = Obj.x;
Obj.sprite.y = Obj.y;
Obj.sprite.alpha = 0;
return Obj
end
Then you can make show
a method, not the :
function enemy:show()
self.sprite.alpha = 1;
end
To call the method, use
array[i]:show(); -- note the `:` to pass the instance to the method