没有在Lua中调用数组中的所有函数

时间:2013-05-04 02:26:00

标签: lua corona

所以我使用Corona SDK,在我的Lua代码中我试图从一个屏幕转换到下一个屏幕并显示隐藏的对象,alpha = 0,只需将其alpha调整为等于1.当我运行我的代码时它只有为数组中的最后一个元素正常工作,但其余部分都没有发生。我的代码在下面,非常感谢任何帮助,我仍然习惯了Lua,所以请光临我:)。

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;

更新

当测试它时,我注意到它每次都在表中处理相同的地址。一世 打印出表号,注意到地址是一样的,因此为什么只有一个对象 在改变。然而,我仍然不知道为什么会发生这种情况

1 个答案:

答案 0 :(得分: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

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

看起来您正在尝试创建类和实例。也许你应该阅读this web page on 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