Corona SDK,LUA:旋转和运动组以及显示对象

时间:2013-06-02 00:45:11

标签: lua corona

我有一个问题(显然)。实际上我不知道为什么这个解决方案不起作用。

我的背景是每帧速率都在移动。我屏幕上还有2个按钮。当我按住左按钮背景旋转到左边时,右边 - 背景旋转到右边。在第(1)点我正在计算这个背景应该如何在当前帧中移动。后来我在第(2)点分配了这个计算的结果。一切正常 - 让我们称之为情况A。

现在我想添加一些对象的组,这些对象将在与背景相同的方向移动。这里出现问题。当我将point(3)eventListener添加到此组(称为myGroup)时,background和myGroup的移动方式与单独的背景不同(来自情境A)。

以下是我的问题:

  1. 我可以将小组加入另一组吗?
  2. 我可以在组中添加事件监听器吗?
  3. 或任何其他想法为什么在将监听器添加到myGroup后,后台和myGroup不会单独作为backround移动(没有myGroup with listener)?

    我希望我能清楚地解释我的问题。 Thx提前寻求帮助!

    function createGame()
    
        background = display.newImage("background.jpg", 0, 0, true);
        background.x = _W/2; background.y = _H/2;       
        background.enterFrame = onFrame;
        Runtime:addEventListener("enterFrame", background);
        group:insert(background);
    
        myGroup = display.newGroup();
        myGroup.xReference = _W/2; myGroup.yReference = _H/2;
        myGroup.enterFrame = onFrame;
        Runtime:addEventListener("enterFrame", myGroup); -- (3)
        group:insert(myGroup); -- this group called "group" comes from storyboard
    
        myGroup:insert(some other objects);
    end
    
    -- Move background:
    function onFrame(self)
    
        -- (1) Calculate next move of background:
        -- (I'm making some calculation here how background should move. Calculation returns X and Y)
    
        -- (2) Move background and group:
        self.y = self.y + Y; 
        self.x = self.x + X;
        self.yReference = self.yReference - Y;
        self.xReference = self.xReference - X;
    end
    

3 个答案:

答案 0 :(得分:3)

  1. 是的,您可以将群组放到另一个群组中,就像这样

    local group1 = display.newGroup()
    local group2 = display.newGroup()
    group2:insert(group1);
    
  2. 是的,您可以将事件监听器添加到组

    group2:addEventListener("touch", function)
    
  3. 你使用物理来旋转你的物体吗?

答案 1 :(得分:2)

我找到了解决方案。实际上,将2个不同的运行时监听器放入2个不同的组中是个坏主意。这种态度造成了问题。它应该如下所示:

function createGame()

    gameGroup = display.newGroup();
    gameGroup.xReference = _W/2; myGroup.yReference = _H/2;
    gameGroup.enterFrame = onFrame;
    Runtime:addEventListener("enterFrame", gameGroup);
    group:insert(myGroup); -- this group called "group" comes from storyboard

    background = display.newImage("background.jpg", 0, 0, true);
    background.x = _W/2; background.y = _H/2;       
    gameGroup:insert(background);

    myGroup = display.newGroup();
    myGroup:insert(some other objects);
    gameGroup:insert(myGroup);  

end

现在就像一个魅力!

感谢krs和DevfaR提供答案和提示:)

答案 2 :(得分:1)

这里你正在使用

self.x = self.x + X;

只需在background函数之外声明myGroupcreateGame(这将使这些对象在特定类中具有全局可用性),如下所示:

local background
local myGroup

然后你可以在函数内移动它们:

background.x = background.x + X;
or
myGroup.x = myGroup.x + X;             
--[[ instead of moving self. ]]--

继续编码...............:)