1

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

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

现在我想添加一组对象,这些对象将在与背景相同的方向上移动......这里出现了问题。当我将第 (3) 点 eventListener 添加到该组(称为 myGroup)时,背景和 myGroup 的移动方式与单独的背景不同(来自情况 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
4

3 回答 3

3
  1. 是的,您可以像这样将组放入另一个组

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

    group2:addEventListener("touch", function)
    

你在用物理学来旋转你的物体吗?

于 2013-06-02T03:20:41.117 回答
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 的回答和提示 :)

于 2013-06-06T20:57:08.593 回答
1

你在这里使用

self.x = self.x + X;

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

local background
local myGroup

然后您可以将它们移动到函数中,如下所示:

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

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

于 2013-06-02T17:26:55.597 回答