Side Scroller Game - Problem with Collision Event in Multiple Display Groups???

I couldn’t find a resolution in the forum so I’m starting a new thread regarding my issue.

I’m trying to make a side scroller game similar to mario brothers however, the difference is that the background and game objects are moving towards the character while the character is static in one location. I used the same code example found in the HorseAnimation game in Corona. I’m also grouping a bunch of coins in a display group and moving the coins towards the character because I don’t want to code each coin individually to move to the character. The display group is like a container where I can just move the group instead of each coin manually.

My problem is that when the character jumps towards the coins, the collision event does not register when the coins are inserted in a display group. However, if I don’t insert the coins in a display group and the character jumps into them, the collision event works correctly.

Now, you might be thinking, “well, don’t insert the coins in the display group and you’re all good”. It’s not that easy. I tried not inserting the coins in a display group and coded logic to move each coin individually towards the character, however, since I’m moving around 20 coins, it started causing issues with the framerate and the performance of my game started slowing down. It’s pretty bad where my character does not jump smoothly and its pretty much unplayable until the coins get removed off the screen.

Oh yeah, I’m testing my game on a 3rd gen IPod Touch with IOS 5 operating system. The game can perform fine on my IPad 2, but the game is slow on the IPod Touch.

If I place all 20 coins in a display group and just move the group in the “enterFrame” event, the performance does not degrade and my game runs fine. However, I lose my collision event because my coins are in a group. I’ve been beating my head on this and coding/re-coding just to get this working but no luck. Can someone help me out? Is it possible to have collision events work across multiple display groups?

Code example below:

[code]
local characterGroup = display.newGroup()

local character = display.newImage(“character.png”, 0, 0)
physics.addBody(character, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.2})
characterGroup:insert(character)

local coinGroup = display.newGroup()

local coin1 = display.newImage(“coin.png”, 0, 0)
local coin2 = display.newImage(“coin.png”, 0, 0)
local coin3 = display.newImage(“coin.png”, 0, 0)

physics.addBody(coin1, “dynamic”, {density = 1.0, friction=0.3, bounce = 0.2})
physics.addBody(coin2, “dynamic”, {density = 1.0, friction=0.3, bounce = 0.2})
physics.addBody(coin3, “dynamic”, {density = 1.0, friction=0.3, bounce = 0.2})

coinGroup:insert(coin1)
coinGroup:insert(coin2)
coinGroup:insert(coin3)

function MakeCoinSparkle(event)
if (event.other.Name = “character”) then
local coin = event.target

coin.isVisible = false
coinSparkle.isVisible = true
end
end

coin1:addEventListener(“collision”, MakeCoinSparkle)
coin2:addEventListener(“collision”, MakeCoinSparkle)
coin3:addEventListener(“collision”, MakeCoinSparkle)

local tPrevious = system.getTimer()

function MoveCoins(event)
local tDelta = event.time - tPrevious
local xOffset = (.20 * tDelta)

coinGroup.x = coinGroup.x - xOffSet

return true
end

Runtime:addEventListener(“enterFrame”, MoveCoins)

[/code] [import]uid: 87837 topic_id: 17035 reply_id: 317035[/import]

I’m not sure why collision listeners are not firing when the objects are placed in the group. Have you tried inserting print statements in the collision listener to see if they are firing off at all? Perhaps it’s just a phase-issue.

Also, future reference, if you paste all of your code in <code></code> tags, you can preserve tabs, etc. Example:

<code>
– Your code here
</code> [import]uid: 52430 topic_id: 17035 reply_id: 63929[/import]

Thank you for the tip on placing my code in <code> tags. It helped.

About your question on putting print statements, yes, i’ve placed print statements and the collision does work if the coins are not inserted in the group. I’ve been putting them in the group and removing them from the group to test the issue.

I’ve read in an older thread that you should not place display objects in separate groups if you want them to collide. I thought that was kinda stupid because in my case, I have a bunch a coins that need to be group together so it would make sense to place them in a “display group” =P

I’ve tried just moving my coins outside of a group and move the coins manually one-by-one using enterFrame event listener, my game slows down on my ITouch 3G. If I place the same coins in a display group and move the group using the same enterFrame event listener, the game runs fine on my ITouch 3G and it does not slow down and behaves like how I want it =)

I guess I’ll have to figure out how to detect collision without using the Physics body and Collision event. Really stupid though. Check out my code again and you’ll see that all I am doing is placing Character and Coins in separate groups. I would think that the Corona SDK would be smart to know that I want them to collide if i have event listeners attached to the objects that need collision. [import]uid: 87837 topic_id: 17035 reply_id: 63976[/import]

I did some research on the forum and someone suggested that you use Physics Joints to group objects together. Instead of using a display group to group my coins together, I’m going to use Physics Joints. Maybe the “weld” or “friction” joint options.

I’ll try it out and post back if I was successful. [import]uid: 87837 topic_id: 17035 reply_id: 64071[/import]