【问题标题】:Corona SDK, LUA: rotation and motion groups and display objectsCorona SDK、LUA:旋转和运动组以及显示对象
【发布时间】:2013-06-02 00:45:10
【问题描述】:

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

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

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

这是我的问题:

  1. 我可以将组放入另一个组吗?
  2. 我可以将事件侦听器添加到组中吗?

或任何其他想法为什么在将侦听器添加到 myGroup 后,背景和 myGroup 不会单独作为背景移动(没有带有侦听器的 myGroup)?

我希望我清楚地解释了我的问题。提前谢谢帮助!

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

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:
    1. 是的,您可以像这样将组放入另一个组

      local group1 = display.newGroup()
      local group2 = display.newGroup()
      group2:insert(group1);
      
    2. 是的,您可以将事件侦听器放入组中

      group2:addEventListener("touch", function)
      

    您是否使用物理学来旋转您的对象?

    【讨论】:

    • 其实我是否使用物理并不重要。打开/关闭物理不会改变旋转/运动行为。此外,我发现如果 myGroup 为空(刚刚声明),问题仍然存在。
    • 您提出的答案是否解决了问题?很难理解你真正想要实现的目标:)
    • 我上面的回答没有解决问题。我想了解带有事件侦听器的显示组如何改变我的背景运动..这让我感到困惑@.@
    • 您能否提供更多代码以查看问题所在
    【解决方案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 的回答和提示 :)

    【讨论】:

      【解决方案3】:

      你在这里使用

      self.x = self.x + X;
      

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

      local background
      local myGroup
      

      然后你可以将它们移动到函数中:

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

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

      【讨论】:

      • 其实我已经在createGame函数之外声明了backgroumd和myGroup。我只是没有在上面的代码中编写它。顺便说一句,当我将背景添加为侦听器时,self.x = self.x + X 和 background.x = background.x + X 有什么区别?意思是“自我”不等于任何带有监听器的对象? (在 c 的指定功能范围内)。谢谢回答krs,但我的问题仍未解决:-(
      • 好的,正如 DevfaR 所说,您能否提供更多代码以查看确切问题所在?
      猜你喜欢
      • 2016-08-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多