0

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.

4

1 回答 1

0

你只做一个Enemy,因为代码

local Obj = {};

仅在加载 Enemy 模块时执行一次。

将该行移到顶部

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

return Obj在我上面指出的最后添加。

此外,而不是

    array[i] = Enemy;
    array[i].new({x=StartX,y=StartY});

你必须

    array[i] = Enemy.new({x=StartX,y=StartY});

否则array[i]设置为类,而不是实例。

现在,在你的版本中

function enemy.show()
  Obj.sprite.alpha = 1;
end

你正在设置唯一的敌人的阿尔法。您需要更改它以使用您要更改的实例的参数。

看起来您正在尝试创建类和实例。也许您应该阅读有关 SimpleLuaClasses 的网页

以这种方法的风格,您可以这样做:

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

然后你可以制作show一个方法,而不是:

function enemy:show()
  self.sprite.alpha = 1;
end

要调用该方法,请使用

array[i]:show(); -- note the `:` to pass the instance to the method
于 2013-05-04T03:07:02.407 回答