1
votes

I have a problem (obviously). Actually I don't know why this solution doesnt work.

I have background which is moving on every frame rate. I have also 2 buttons on screen. When I push and hold left button background rotate to left, when right - background rotate to right. In point (1) I'm making some calculation how this background should move in current frame. Later I assign result of this calculation in point (2). Everything works fine -- lets call it situation A.

Now I want to add group of some objects which will be move in the same direction as background..and here appear problem. When I add in point (3) eventListener to this group (called myGroup), background and myGroup are moving in different way than background alone (from situation A).

Here are my questions:

  1. Can I put group into another group?
  2. Can I add event listener into group?

or any other idea why after adding listener into myGroup, background and myGroup don't move as backround alone (without myGroup with listener)?

I hope I explained my problem clearly. Thx in advance for help!

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

3 Answers

3
votes
  1. Yes you can put group to another group just like this

    local group1 = display.newGroup()
    local group2 = display.newGroup()
    group2:insert(group1);
    
  2. Yes you can put event Listener to group

    group2:addEventListener("touch", function)
    

are you using physics to rotate your object?

2
votes

I found solution. Actually it is bad idea to put 2 different runtime listeners into 2 different groups. This attitude caused a problem. It should looks like below:

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

and now works like a charm!

Thanks krs and DevfaR for answers and hints :)

1
votes

here you are using

self.x = self.x + X;

Just declare the background and myGroup outside your createGame function(this will give those objects a global posibility in the particular class) as below:

local background
local myGroup

and then you can move them inside the function as:

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

Keep coding............... :)