Display objects in a group revert back to old settings

Im trying to write a bejewled like game
I have a function which takes a click point and gets display objects near that point. I create a group in that function and add theobjects to the group. I rotate the group and everthing works fine. Later i click same point and same objects ( which are now rotated) go into new group. This time when i rotate the group the objects seem to jump back to old posirion and rotate to same position instead of rotating an additional amount.
I am using transition to do the rotation and i use the delta is true to specify that the angle is a delta angle.

Do i somehow have to reset objects state after rotation to make them know they’ve been rotated?

This is driving me crazy. It should work.
[import]uid: 56054 topic_id: 9603 reply_id: 309603[/import]

Any chance of posting the source of your function?
Are you using a local or global group to transition them?
[import]uid: 34945 topic_id: 9603 reply_id: 35035[/import]

g = display.newRect(100,100,20,20)
h = display.newRect(180,100,20,20)
ll = display.newLine(100,100,180,100)
x = display.newGroup()

x:insert(g)
x:insert(ll)
x:insert(h)

x:setReferencePoint(display.CenterReferencePoint)

x.rotation=0
transition.to(x,{ time=1900,rotation=45,delta = true})
transition.to(x,{ time=1900,rotation=45,delta = true})

Okay this is the same thing.
THis should rotate the object to 90. First by 45 then by another 45.
Instead it only rotates to 45 degrees. Because the second time you do the transtion the rotation is back to 0.

I actually printed out the rotation of the group after the first transition and sure enough it was still 0.
So rotating with transition doesn’t actuallly change the rotation setting of an object but it LOOKS like the rotation is changed because the object really looks rotated.

What the heck is going on? The documentation doesn’t seem to address this issue.

I wish there was a doc of “GOTCHA’S” in CORONA.
I would bet that it would be quite a big document.
[import]uid: 56054 topic_id: 9603 reply_id: 35037[/import]

i’d say you are rotating the group, and the objects relative to the group remain unchanged.
its all working logically, unfortunately. think of the group as a filter applied to the objects, when you remove the filter, you remove the rotation.
probably a terrible resource waste to create new groups on the fly for each click tho.
also, you need a delay on the second transition, because the script runs at the end of the file, all transitions at once. {time=1900,delay=1900…
[import]uid: 34945 topic_id: 9603 reply_id: 35059[/import]

I guess that wasn’t a good example. Here’s a better example. And this isn’t real code. It’s just an example to show the problem.

r = display.newRect(100,100,20,20)
r:setFillColor(255,0,0)

g = display.newRect(180,100,20,20)
g:setFillColor(0,255,0)

b = display.newRect(100,200,20,20)
b:setFillColor(0,255,255)
local function makegroup(a,b)
local x = display.newGroup()
x:insert(a)
x:insert(b)
x:setReferencePoint(display.CenterReferencePoint)
x.rotation=0
return x
end

local function doGroupRot(obj1,obj2, time,delay,angle)
local grp= makegroup(obj1,obj2)
transition.to(grp,{ time=time,delay =delay,rotation=angle,delta = true})
end

doGroupRot(r,g,1000,500,45)
doGroupRot(g,b,1000,1500,45)
Now I want this to first rotate objects r and g and then rotate objects g and b.

Instead what happens is that g remains fixed and r rotates and then g and b rotate.
Why isn’t g rotating after the first doGroupRot?

Now if you comment out the second doGroupRot and run program then r AND g do rotate together.

[import]uid: 56054 topic_id: 9603 reply_id: 35067[/import]

the second shouldn’t interfere… if you make the second angle 46 does it work? [import]uid: 34945 topic_id: 9603 reply_id: 35180[/import]

Ok, I’ve run your code, and played with it a lot.
After the first run, I assumed that the second group add removed the common object from the first group
But it doesn’t seem to be the case.

I’d say this is a bug, but I’ll do a bit more digging before I file a report.
[import]uid: 34945 topic_id: 9603 reply_id: 35191[/import]

its not reusing the grp var… changing
[lua]local function doGroupRot(obj1,obj2, time,delay,angle)
local grp= makegroup(obj1,obj2)
transition.to(grp,{ time=time,delay =delay,rotation=angle,delta = true})
end

doGroupRot(r,g,1000,500,45)
doGroupRot(g,b,1000,1500,45)[/lua]
to
[lua]local grp= makegroup(r,g)

transition.to(grp,{ time=1000,delay =500,rotation=45,delta = true})
local grt= makegroup(b,g)

transition.to(grt,{ time=1000,delay =1500,rotation=45,delta = true})[/lua]

doesn’t make any difference :slight_smile: [import]uid: 34945 topic_id: 9603 reply_id: 35193[/import]

http://developer.anscamobile.com/content/display-objects#Groups


Note

That if you insert a display object into one group and then later insert the same object into another group, the display object is removed the original group into which it was inserted. An object can only exist in one group at a time.
[import]uid: 34945 topic_id: 9603 reply_id: 35194[/import]

Alright, I have made it work, whether this solution is suitable is up to you and your games requirements, but I hope it helps!

[lua]function changeGroup(obj,newGroup) --using group:remove() deletes
– the object. We want to keep the object, so we need to be a
– bit more creative.

–Set transform params we want to preserve
local oldGroup = obj.parent
local x,y = obj:localToContent(0, 0)

obj.setReferencePoint = display.CenterReferencePoint
–if you are using non-center reference points, remember to reset
–them after using this function!

newGroup:insert(obj)

–Object needs to inherit oldGroup’s transform, replace
–with saved ones!
obj.x = x
obj.y = y
obj.rotation = oldGroup.rotation
end

local function main()
r = display.newRect(100,100,20,20)
r:setFillColor(255,0,0)

g = display.newRect(180,100,20,20)
g:setFillColor(0,255,0)

b = display.newRect(100,200,20,20)
b:setFillColor(0,255,255)

local function makegroup(a,b)
local x = display.newGroup()
changeGroup(a,x) --See function above, using insert
changeGroup(b,x) --doesn’t preserve transformations!
x:setReferencePoint(display.CenterReferencePoint)
x.rotation=0
return x
end

local function doGroupRot(obj1,obj2, time,angle)
–removed transition delay, see below

local grp= makegroup(obj1,obj2)
transition.to(grp,{ time=time,rotation=angle,delta = true})
–removed transition delay, see below

end

doGroupRot(r,g,1000,45)
timer.performWithDelay(1000,function() doGroupRot(g,b,1000,45)end,1)
–Unfortunately, using the transition delay parses at the same
–time as the previous transition, where x,y, and rotation are
–still original :confused:
–Because LUA is a scripting language, the interpreter parses the
–entire file before doing everything at once.
–I’ve used a timer with an anonymous function to force the
–corona engine to parse the transition after the previous one
–is complete.
–If anyone knows a better way, PLEASE tell me :smiley:
end
main()[/lua] [import]uid: 34945 topic_id: 9603 reply_id: 35204[/import]

Thanks.That worked. Just what I needed to know. [import]uid: 56054 topic_id: 9603 reply_id: 35244[/import]

You’re welcome. :slight_smile: [import]uid: 34945 topic_id: 9603 reply_id: 35305[/import]

On more thing. It appears that you don’t have to set the object’s rotation to oldgroup’s rotation. Even if I comment out the lines:

local oldGroup = obj.parent and

 obj.rotation = oldGroup.rotation   

it still works. What’s important is setting the x,y of the object to content coordinates. If you don’t do this then the objects jump around with each rotation. Seems strange that you have to set some properties but not others.

[code]

function changeGroup(obj,newGroup)
–using group:remove() deletes
– the object. We want to keep the object, so we need to be a
– bit more creative.
–Set transform params we want to preserve
–local oldGroup = obj.parent
local x,y = obj:localToContent(0, 0)
obj.setReferencePoint = display.CenterReferencePoint
–if you are using non-center reference points, remember to reset
–them after using this function!
newGroup:insert(obj)
–Object needs to inherit oldGroup’s transform, replace
–with saved ones!
obj.x = x
obj.y = y
–obj.rotation = oldGroup.rotation
end

[/code] [import]uid: 56054 topic_id: 9603 reply_id: 35326[/import]

Very strange :slight_smile:
In my testing I was checking the childs rotation and it was always zero. Glad I could help :slight_smile: [import]uid: 34945 topic_id: 9603 reply_id: 35578[/import]