Corona SDK,LUA: 旋转和移动组和显示对象

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

我有一个背景,它在每一帧都在移动。屏幕上还有2个按钮。当我按住左边的按钮时,背景向左旋转;当我按住右边的按钮时,背景向右旋转。在点(1)中,我做了一些计算,计算出当前帧中背景应该如何移动。稍后我在点(2)中分配了这个计算的结果。一切都运行良好——让我们称之为情况A。

现在我想添加一组对象,这些对象将沿着与背景相同的方向移动。这里出现了问题。当我在点(3)中为这个组(称为myGroup)添加eventListener时,背景和myGroup沿着不同于仅有背景的方式移动(从情况A)。

以下是我的问题:

  1. 我可以把组放在另一个组里吗?
  2. 我可以在组中添加事件侦听器吗?

或者任何其他的想法,为什么在将listener添加到myGroup之后,背景和myGroup不像仅有背景一样移动(不包括带有listener的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); --这个名为“ group”的组来自storyboard

     myGroup:insert(一些其他对象); 结束

--移动背景: function onFrame(self)

     - (1)计算背景的下一个移动:      - (我在这里做了一些计算,说明背景应该如何移动。计算返回X和Y)

     - (2)移动背景和组:      self.y = self.y + Y;      self.x = self.x + X;      self.yReference = self.yReference-Y;      self.xReference = self.xReference-X; end `` `

点赞
用户1682268
用户1682268
  1. 是的,你可以像这样将一个组放置于另一个组中

    ``` local group1 = display.newGroup() local group2 = display.newGroup() group2:insert(group1);


2. 是的,你可以在组中添加事件监听器

group2:addEventListener("touch", function)

```

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

2013-06-02 03:20:41
用户1979583
用户1979583

下面是翻译结果,并且保留原本的 markdown 格式:

在这里使用:

self.x = self.x + X;

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

local background
local myGroup

然后,您可以在函数内将它们移动,如:

background.x = background.x + X;
或
myGroup.x = myGroup.x + X;
--[[不再移动 self。]]--

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

2013-06-02 17:26:55
用户1009767
用户1009767

我已经找到解决方案。将两个不同的运行时监听器放在两个不同的组中是一个不好的想法。这种做法会导致问题。应该像下面这样:

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-06 20:57:08