Solved - Challenge Destroying an Object

0 favourites
  • 5 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I'm working on a tower defense game and it has an object "Bullet" that is fired from a turret object "Tower".

    I want to destroy the Bullet if it collides with another Tower (instead of the enemy).

    So, I wrote this:

    Bullet On Collision with Tower -> Bullet Destroy

    But, since the Bullet is being created on top of a Tower, it is destroying the Bullet before it can even move away from the tower that created it.

    Is there a simple way to prevent this from happening?

    Thanks in advance,

    Chris

    p.s. In my game, these bullets bounce, so it's possible the bullet would hit the original tower and that should destroy it as well (just not as soon as it appears).

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • There are different approaches. You could add an instance variable on the bullet to track where its spawned from so in your spawn bullet event directly under the spawn action you can set the bullet.var to tower.UID (the tower picked will be the one that is spawning the bullet)

    Now that the bullet has the UID of the spawning tower attached, on the collision with tower event you can add another condition bullet.var does not equal tower.UID, meaning the bullet will not be destroyed by the tower spawning it.

    As for being destroyed by the spawning tower later there are couple of ways. I used bouncing bullets in my game and I used another variable that tracks the number of bounces, so again on that same collision with tower event, with an action you can add 1 to this new numBounces var. Then you could add an event like if bounce > 0 set bullet.var (the UID check) to 9999 means it will now be destroyed when colliding with the spawning tower.

    There may be a simple way to do this with parent/child hierarchy but I've not used that functionality.

  • Thank you so much for your excellent suggestion. I'm sure that would work and it actually gave me some ideas for how I could solve some other upcoming issues.

    I've been trying to find a solution for the past hour and just came up with this solution below (I just had to add an Instance Variable on Bullet called "Live" by default set to False).

  • That's not bad either! You just need to remove the Else and bring the second event out of a sub event on its own. With the two separate events I think you should see it working.

  • That's not bad either! You just need to remove the Else and bring the second event out of a sub event on its own. With the two separate events I think you should see it working.

    Thanks for that suggestion, it tightened the code and works perfectly.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)