Duplicating and colliding multiple objects

Hi guys

I’m new to corona and am really enjoying it :slight_smile: I come from an AS2 background but lets face it… im not a strong coder :stuck_out_tongue: fun hobby though.

im trying to make an avoidance game which is kinda 3d - the view is from behind the players shoulders, whos running forwards into the screen. obstacles come flying out of the distance toward you which you have to avoid. So far ive got the player moving how i want, and ive made the obstacles (baddies) work in the 3d space.

What I wanna do now is set something up where the baddies are randomly generated - and the randomly generated baddies can detect collision with the player (if theyre in front of the player and their zpos is the same). Where do i even start to make this happen :stuck_out_tongue: ?

heres my code concerning baddy creation -

[lua]Originx = display.contentWidth / 2
Originy = display.contentHeight / 3
FocalLength = 2000
scaleRatio = 0

col1 = display.contentWidth/4 - display.contentWidth/8
col2 = col1 + display.contentWidth/4
col3 = col2 + display.contentWidth/4
col4 = col3 + display.contentWidth/4

marker1 = display.newRect(0,0,1,1)
marker1xpos = (display.contentWidth/4 - display.contentWidth/8) - Originx
marker1ypos = display.contentHeight/1.3 - Originy
marker1zpos = 0
marker1ScaleRatio = FocalLength/(FocalLength+marker1zpos)
marker1.x = Originx + marker1xpos * marker1ScaleRatio
marker1.y = Originy + marker1ypos * marker1ScaleRatio
marker1.yScale = 10 * marker1ScaleRatio
marker1.xScale = marker1.yScale

marker2 = display.newRect(0,0,1,1)
marker2xpos = (display.contentWidth/4 - display.contentWidth/8) + (display.contentWidth/4) - Originx
marker2ypos = display.contentHeight/1.3 - Originy
marker2zpos = 0
marker2ScaleRatio = FocalLength/(FocalLength+marker2zpos)
marker2.x = Originx + marker2xpos * marker2ScaleRatio
marker2.y = Originy + marker2ypos * marker2ScaleRatio
marker2.yScale = 10 * marker2ScaleRatio
marker2.xScale = marker2.yScale

marker3 = display.newRect(0,0,1,1)
marker3xpos = (display.contentWidth/4 - display.contentWidth/8) + (display.contentWidth/4)+ (display.contentWidth/4) - Originx
marker3ypos = display.contentHeight/1.3 - Originy
marker3zpos = 0
marker3ScaleRatio = FocalLength/(FocalLength+marker2zpos)
marker3.x = Originx + marker3xpos * marker3ScaleRatio
marker3.y = Originy + marker3ypos * marker3ScaleRatio
marker3.yScale = 10 * marker2ScaleRatio
marker3.xScale = marker3.yScale

marker4 = display.newRect(0,0,1,1)
marker4xpos = (display.contentWidth/4 - display.contentWidth/8) + (display.contentWidth/4)+ (display.contentWidth/4) + (display.contentWidth/4) - Originx
marker4ypos = display.contentHeight/1.3 - Originy
marker4zpos = 0
marker4ScaleRatio = FocalLength/(FocalLength+marker4zpos)
marker4.x = Originx + marker4xpos * marker4ScaleRatio
marker4.y = Originy + marker4ypos * marker4ScaleRatio
marker4.yScale = 10 * marker4ScaleRatio
marker4.xScale = marker4.yScale

–create baddy

baddy = display.newRect(0,0,12,12)
baddy.xpos = marker1xpos
baddy.ypos = marker1ypos
baddy.zpos = 10000

baddy2 = display.newRect(0,0,12,12)
baddy2.xpos = marker2xpos
baddy2.ypos = marker2ypos
baddy2.zpos = 10000

baddy3 = display.newRect(0,0,12,12)
baddy3.xpos = marker3xpos
baddy3.ypos = marker3ypos
baddy3.zpos = 10000

baddy4 = display.newRect(0,0,12,12)
baddy4.xpos = marker4xpos
baddy4.ypos = marker4ypos
baddy4.zpos = 10000

–move the baddy

function moveBaddy(event)

baddy.zpos = baddy.zpos - 50
scaleRatio = FocalLength/(FocalLength+(baddy.zpos*2))
baddy.x = Originx + baddy.xpos * scaleRatio
baddy.y = Originy + baddy.ypos * scaleRatio
baddy.yScale = 10 * scaleRatio
baddy.xScale = baddy.yScale

baddy2.zpos = baddy2.zpos - 50
scaleRatio2 = FocalLength/(FocalLength+(baddy2.zpos*2))
baddy2.x = Originx + baddy2.xpos * scaleRatio
baddy2.y = Originy + baddy2.ypos * scaleRatio
baddy2.yScale = 10 * scaleRatio
baddy2.xScale = baddy2.yScale

baddy3.zpos = baddy3.zpos - 50
scaleRatio3 = FocalLength/(FocalLength+(baddy3.zpos*2))
baddy3.x = Originx + baddy3.xpos * scaleRatio
baddy3.y = Originy + baddy3.ypos * scaleRatio
baddy3.yScale = 10 * scaleRatio
baddy3.xScale = baddy3.yScale

baddy4.zpos = baddy4.zpos - 50
scaleRatio4 = FocalLength/(FocalLength+(baddy4.zpos*2))
baddy4.x = Originx + baddy4.xpos * scaleRatio
baddy4.y = Originy + baddy4.ypos * scaleRatio
baddy4.yScale = 10 * scaleRatio
baddy4.xScale = baddy4.yScale

end

[import]uid: 88604 topic_id: 15792 reply_id: 315792[/import]

Oh and when i say randomly generated, i mean generated at random intervals. theyre moving along the paths that I want them to already.
[import]uid: 88604 topic_id: 15792 reply_id: 58301[/import]

Do you need to know which of the “baddies” (good word) hit the player?

Using a baddy1.myName = “baddy1” would let you know which enemy was involved in the collision. You could use baddy1.kind = “bad” on each enemy if you don’t need to know which one was hit.

The collision listener would be on the player weather than all the enemies. (This way, there is only one.)

If you do a forum search you’ll find people talking about randomizing timer.performWithDelay which should help with your other question :slight_smile:

Peach [import]uid: 52491 topic_id: 15792 reply_id: 58344[/import]

i thought the collision listener only worked with physics objects?

What I really want is that I want some code thats gonna say, at a random interval, create a baddy, put that baddy in one of the 4 columns at random, and check to see if the players hits it.

What i dont understand is how to give all the baddies unique names when i create them, and how to check collisions between a potentially infinite number of baddies and the player.

Am I thinking about this all wrong? is there a simpler way or something? [import]uid: 88604 topic_id: 15792 reply_id: 58488[/import]

Collisions only occur with physics objects, so you’d make them physics objects - sensors, or the like. They could be static and gravity could be set at (0,0) so they wouldn’t behave “badly”.

You could check the position of objects to avoid using physics if you wish, that would be a suitable alternative :slight_smile:

For giving them unique names, you could do that with something like this;

[lua]local baddy = {}
local baddies = 0

local function printName (event)
print (event.target.myName)
end

local function spawnBaddy ()
baddies = baddies + 1
baddy[baddies]=display.newImage(“m.png”)
baddy[baddies].x = math.random(50,270)
baddy[baddies].y = math.random(50,430)
baddy[baddies].myName = “baddy” … (baddies)
baddy[baddies]:addEventListener(“tap”, printName)
end
timer.performWithDelay(1000, spawnBaddy, 10)[/lua]

That’s just an example but it should help - that can be run on it’s own, just replace “m.png” with another image name :slight_smile:

See how you go with it.

Peach :slight_smile: [import]uid: 52491 topic_id: 15792 reply_id: 58555[/import]