I’ve got a function that continually spawns scooters zipping across the screen. I now need to compare the x and y coordinates of these scooters to those of a draggable object (my player) to check for collision detection. Only problem is, the x and y coordinates of the scooters are locked inside this function and I don’t know how to access them. I’m only a few weeks into Corona development, so there isn’t an obvious solution in front of me. My scooter spawning function is as follows:
local function scooterFunction() timer.performWithDelay(math.random(20,35), scooterFunction) local scooter = scooterFactory:newSpriteGroup() scooter.x = -100 scooter.y = math.random(120,924) transition.to(scooter, {time = math.random(1300,1400), delay = 0, x = scooter.x + 968}) endscooterFunction()[/code]and my draggable object is controlled by your basic "touch" event. I need to create some sort of checkForCollision function that will compare the coordinates of the scooters with that of my player, but I'm not sure what the "best practice" method would be. Any suggestions? [import]uid: 79394 topic_id: 14118 reply_id: 314118[/import]
Hey there,
When we talk about collisions, we’re talking about physics.
Are you using physics at all? It’s a lot simpler than trying to detect a “collision” without it
[import]uid: 52491 topic_id: 14118 reply_id: 51973[/import]
Hi Peach, no I’m not using physics:) My previous post was all about finding a non-physics based approach to collision detection: https://developer.anscamobile.com/forum/2011/08/20/does-corona-have-collision-detection-thats-not-physics-based [import]uid: 79394 topic_id: 14118 reply_id: 51977[/import]
I see - I’m not sure without seeing how you are detecting your collision how you’d access the x and y - you have multiple scooters with the same name and therefore scooter.x will return the x of the last scooter spawned.
You’d likely check in the “collision” function for an x and y value. [import]uid: 52491 topic_id: 14118 reply_id: 52084[/import]
Thanks for getting back to me, Peach. I actually haven’t even been able to come up with a viable collision function, as I’m stuck on how to access the many scooters’ x coordinates (there’s at least 30 onscreen at any given time). On top of dealing with multiple objects with the same name, I also have the problem of not having their x coordinates as global variables (if they were, I’d have no problem doing something like:
local objectdistancex = scooter.x - player.xlocal objectdistancey = scooter.y - player.yif objectdistancex <10 thenif objectdistancey <10 thenprint("collision")endend[/code]I've tried changing my scooter function into an event, like so:local function scooterSpawn (event) timer.performWithDelay(math.random(20,35),scooterSpawn) local scooter = scooterFactory:newSpriteGroup() scooter.x = -100 scooter.y = math.random(120,924) transition.to(scooter, {time = math.random(1300,1400), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end}) endtimer.performWithDelay(0,scooterSpawn,1)[/code]but I don't think this makes any difference. I'm pretty lost at this point. [import]uid: 79394 topic_id: 14118 reply_id: 52091[/import]
Would you consider using physics? If you were and put a collision listener on your player you could easily print out the coords for any scooter hit.
Without using physics and using a workaround to detect “collision” (which I gather isn’t working properly?) it’s going to be pretty tricky. [import]uid: 52491 topic_id: 14118 reply_id: 52260[/import]
Hi Peach,
You’re absolutely right. I just learned exactly how the code should work with the help of Renjith in another thread. Of course you already knew that, as I see you’ve just posted on that same thread:) Thanks so much for the hint, Peach!
Steven [import]uid: 79394 topic_id: 14118 reply_id: 52271[/import]
edaabs,
assuming that you are still pursuing your original chain of thought of making a non-physics collision detection routine,
- The thought behind your function to spawn scooters is correct, but the implementation is wrong.
If you write a factory function to create any object, then you *must* return the created object. so you need to have that function return a scooter instance.
- If you have your scooters created in an array, you know the one that has collided and you can then remove that scooter from the display and memory.
Hope this helps you back on track from being pretty lost
And yes, before Physics (Box2D) existed, there were quite a large number of games that defined the game industry and they handled collisions quite well, see R-Type, Xenon, Tiger Shark, Contra, etc to name a few.
cheers,
?
[import]uid: 3826 topic_id: 14118 reply_id: 52285[/import]
I’m wondering the same thing.
I got a function that gets called quite often and every time it loads a different image. I thought it would be easy to just declare a variable outside the function and then inside the function do:
variableOutside = variableInside
but nope.
my code looks like this:
local variableOutside
local function spawnImages()
local ent = math.random(1,7)
local houseImage = display.newImage("house" .. ent .. ".png")
variableOutside = houseImage
end
but all I get is "attempt to index upvalue ‘variableOutside’ “a nil value” when I try to use it.
Why isn’t variableOutside.x the same as houseImage.x? I can print(variableOutside.y) just fine inside the function spawnImages but not outside.
Any ideas?
[import]uid: 21652 topic_id: 14118 reply_id: 52346[/import]