Can collisions have specific outcomes depending on the type of object your player is colliding with? My collision function is applied to my player, so I don’t know how you can differentiate what type of enemy is hitting it:
local function onPlayerCollision()print("collided with enemy")endplayer:addEventListener("collision",onPlayerCollision)[/code] [import]uid: 79394 topic_id: 16793 reply_id: 316793[/import]
you can attach event listener to specific object and make it respond to specific other object
if you want different things for different “other objects” just stack some elseif in there
something like that, i used it in my code and it works
[lua]local function onCollision(event)
if event.other.name == “enemy” then
event.other:removeSelf()
highScore = highScore + event.other.health
scoreField.text = highScore
event.target.isVisible = false
event.target:removeEventListener(“collision”,onCollision)
end
end[/lua] [import]uid: 16142 topic_id: 16793 reply_id: 62865[/import]
Hi dark consoles,
Thanks so much for the suggestion. I’m sure it works.
In my case though, I’m having trouble, I’m guessing because my “enemy” is hidden within a function:
local function scooterSpawn (event) local scooter = display.newRect(20,20,20,20) scooter.x = -100 scooter.y = math.random(120,924) transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end}) spawnedGroup:insert(scooter)end[/code]Do you (or anyone else) mind helping me out with this problem? I've put your code into effect like so:local function onPlayerCollision(event) if event.other.name == "scooter" then print ("collided with scooter") else print ("collided with some enemy other than scooter") endendplayer:addEventListener("collision",onPlayerCollision)[/code] [import]uid: 79394 topic_id: 16793 reply_id: 62871[/import]
you have to declare scooter.name == “scooter” first, then attach listener to scooter inside spawning function [import]uid: 16142 topic_id: 16793 reply_id: 62885[/import]
Awesome, that did the trick! You’re a star darkconsoles, thank you so much.
Cheers,
Steven [import]uid: 79394 topic_id: 16793 reply_id: 62893[/import]
transitions[#transitions + 1] = transition.to
i cant understand why did you use this line, i always store my objects in table and then issue transitions on it [import]uid: 16142 topic_id: 16793 reply_id: 62899[/import]
That’s a good question, I’ve been wondering that myself. That came from a code snippet put forward by another user in a previous thread. I’ll find it, and send you the link… [import]uid: 79394 topic_id: 16793 reply_id: 62902[/import]
This is it here: https://developer.anscamobile.com/forum/2011/09/19/how-do-you-manipulate-spawned-objects
It’s a long one, if you figure it out please let me know. Now that I’m taking a second look, I’m starting to think that line might not even be necessary.
Cheers,
Steven
[import]uid: 79394 topic_id: 16793 reply_id: 62906[/import]
Yup, it wasn’t necessary. I just chopped out the transitions[#transitions + 1] =
from my code and everything runs as normal.
I guess I should thank you one more time for helping me come to that realization (embarrassed face). Once again darkconsoles, a very sincere: “thank you”.
Cheers,
Steven [import]uid: 79394 topic_id: 16793 reply_id: 62912[/import]
any time, happy to help [import]uid: 16142 topic_id: 16793 reply_id: 62948[/import]