Request: When sprite is x distance from another

0 favourites
  • 13 posts
From the Asset Store
A set of 10 pixel art animated magic effects with icons. 2 Fire, 2 Earth, 2 Wind, 2 Water, Portal, Explosion.
  • I love Tower Defense games. They are what keeps me sane.

    What I would really love is if the lads would code some sort of function that is essentially:

    When Sprite1 is <= x distance from Sprite2 ... fire event

    That way when coding a tower defense you could easily have a tower fire at the enemy mob.

    My aim is to say something like: When an instance of the players units are within an instance of the enemy's units - fire a bullet.

    I have read the forums and I'm aware of the distance() function, but it doesn't really cope when there are multiple instances of both the player and the enemy's units.

    If this could be coded into the engine, I would buy the boy's a beer or two. If I have missed something fundamental in coding events please learn me good.

    The beer is still offered*.

    * You will need to come to Adelaide to claim said beer.

    Cheers guys!

  • There is actually a distance expression: Distance(x1,y1,x2,y2)

    I made an example: dl.dropbox.com/u/6660860/distance.capx

    Have fun!

    Wait doh, i didn't realize you already mentioned the Distance() expression in your post! My bad....

    Edit again, maybe this will help? scirra.com/forum/nearest-instance_topic44692.html

  • Hello!

    I think that you are talking about Construct 2 but do you really mean C2 or probably CC?

  • Spades I am talking about Construct 2 - the HTML most recent version that exports to HTML5.

    Citnarf I read that forum post but the trick is having any instance of sprite 1 within any instance of sprite 2. I think that is the magic right there.

    (I meessed around with that today but couldn't get it to work with any instance of 1 vs any instance f 2)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • By default the compare values condition just picks the first instance of an object, so it doesn't work with multiple instances. That's why distance() doesn't appear to work with multiple instances.

    However, you can fix it with for-each:

    + For each A

    + For each B

    + distance(A.x, A.y, B.x, B.y) < 100

    -> A: look at B

    This repeats the distance() comparison for every combination of A and B, so every A looks at a B inside its range. (Not the nearest one - that's a fair bit more complicated - but it works OK this way.)

  • Ahh thanks mate. I'll give it a go and let you know how I go.

    Kudos.

  • It worked perfectly!

    Here is the final code.

    <img src="http://i.imgur.com/FHUZf.gif" border="0" />

    Obviously I wouldn't want too many of these running for performance reasons but it does solve the problem of having things like Tureets autofire at mobs.

    thanks Ashley!

  • You should be able to have all four conditions in one event and have it still work the same! No need for three subevents.

  • Can I just confirm the difference from when I indent the events?

    Also, if all these events are at the same 'level' of indentation is that correct?

    Is there a performance difference?

  • i think ash is telling you that you can create 'one' event that includes all the conditions you have listed, because they all create one desired effect.

  • Ahh nice.

    I put them all into one condition as it worked exactly the same.

    awesome.

  • What about checking distance between 2 or more instances of the same object.

    I have 3 instances of the same object moving around the screen with foreach.

    How can I check distance between 2 of them?

    I want to make them change direction if they're going to bump to each other.

  • Hi there, If you want a tower defense game then I guess all you need is the combination of these plugins:

    -Turret Behavior

    -Pathfinder

    Use the Turret behavior for your towers and the pathfinder behavior for your creeps/monsters.

    The plugins are easy to implement, you'll just have to read to their documentation/tutorial to get it going :)

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