I’m working on a grid-based top-down movement game. I create a new sprite, called ball, at a random grid position every 5 seconds. The ball object has a variable, called dormant. When first created, the variable dormant is set to 1 and if another ball collides with it, nothing should happen. (It is a different color while dormant.) After a certain time dormant is set to 0, meaning it is no longer dormant. At that time it turns darker and starts moving around the screen. When 2 balls that are moving (i.e., not dormant) collide, they should both be deleted, and this works. However, when a moving ball runs into a dormant ball, the moving ball should continue on its way. In other words, there should be no collision. But instead, the moving ball is deleted.
In the collision condition I specify ball in collision with ball since, well, they’re both balls. I have a second condition in the event, which is the Variable dormant of ball must = 0. However, I don’t know how to specify that the ball with dormant = 0 should only be deleted if the ball it collides with also has dormant = 0.
Is there some standard way in GDev to only affect one of two of the same object type upon collision?
If in the conditions you put object variable dormant = 0, only the ones with variable dormant = 0 will affected by the actions.
To continue to arthoro’s answer, it will be a sub-event after the collision event, put the condition there.
If that doesn’t work I’d try a For Each event (although not 100% sure how to use it)
Thanks for the reply. Using a sub-event didn’t work (if I’m doing it correctly). My problem is that if a ball that’s active collides with a ball that is dormant, I don’t want either ball to be deleted, not even the active (dormant = 0) one. I only want to delete the balls when both of them are active, i.e, dormant = 0.
I don’t know how to specify that the active ball should not die if the ball it hits is dormant. One of the balls has dormant = 0, so that one still gets deleted even though the other ball is dormant.
I have thought of another way to accomplish what I want, although I haven’t tried it yet. I’m going to create another animation for the ball and set it to that animation when the ball is dormant. That animation will have no hit area…so there should be no collision. When it becomes active I will switch the ball to its other animation, and that animation will have a hit area. So there will only be collisions between balls that have a hit area, i.e., active balls. I hope…
But I’m still curious about the collision event in GDev. It would be nice if there were functionality built in to deal with 2 (or more) of the same objects colliding, based on some properties of each object.
I think I see the problem. You could solve it by making the dormant ball a separate object, and to make it no dormant delete it and create an active one in it’s place.
I’m interested into the topic, it’s a question i have for a quite long time now, and the only workaround i have ever found is the solution given by @arthuro555. Just make a second fake object to test collision properly.
I’m glad to know that others have had this same issue, so my question wasn’t dumb. But FYI, the work-around I described in my previous reply was successful! The sprite ball now has 2 animations, called active and dormant. When I create a ball, I set it to display the animation dormant. That animation has no hit area. When it becomes active, I switch its animation to active, which does have a hit area. So only when 2 active balls collide is a collision detected. (Each animation only has one frame.) I’m sure that arthuro555’s method would work, but this way seems simpler.