Test how far an object is from another?

0 favourites
  • 15 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Hi, I'm working on a tower defense game(Just for fun, i'm not particularly knowledgeable or in love with the genre but it seemed like a managable task for a change) and i'm trying to make it so that when an enemy get's to my main tower(The one you are protecting) , the tower's health(An instance variable) will go down over a period of X seconds.

    Now i know how to do the whole remove X damage every X seconds thing, but i can't think of a way of testing if the enemy is within say 2 pixels of my tower.

    Another question i have is how can i easily control how many turrets my player can create? I have it so they appear when the

    left mouse is clicked but as it stands you could create a

    thousand if you like and the on screen counter just keeps going up.....

    This is pretty inconvenient as is , especially when i consider adding more than one type of turret and some kind of points system to allow you to spend points to create them in the first place.

    If anybody out there has made a tower defense game(and i'm sure some of you must have at some point or another) , i would really like your help,

    Thanks, Paul.

  • Perhaps the expression: distance(x1, y1, x2, y2)

    enter the X,Y of the tower, and the x, y of the enemy, and it will return the number of pixels between.

    Edit: and do a PickedCount (with all picked) to see how many towers there are, and don't create one if it's more than your maximum.

    Tower.PickedCount < 20

  • To check the distance use distance(player.x,player.y,tower.x,tower.y)

    So you should just add another condition to let the system check if this distance is smaller than 2. (I guess you should use another number, but you said 2)

  • Thanks for such a fast reply, i'm a beginner so i guess i'm just not following too well at this point. I think i understand how such a condition would actually work, but i'm not at all sure how to add this condition.

    What i mean is i know to click add condition, then a list of possible conditions to add will appear , but which of these should i choose in order to enter the enemy.x/y coordinates and the tower ones. I thought it would have something to do with the whole compare x and compare y thing, but i can't seem to use either that or the compare width and height. It simply won't take those values, i assume i'm doing it wrong?

    Thanks for your patience.

  • Is it something to do with variables? Also if anybody can think of any particular tutorials on this subject, not to mention any decent general help websites for this kind of mathematics, As i'm not particularly well-versed in it(although eager to learn)

  • pmurph, don't worry it can take a while to get used to this stuff. I'm still learning new things, and asking silly questions all of the time :) I've added some steps below.

    1. Add a 'Compare two values' event (found in the general section of the system events).

    2. Click on the first value, and in the Objects expressions window select system. This will give you a whole bunch of options. Scroll down to the maths section, and you'll find the distance option.

    3. Click on the distance expression (this will add it to your comparison equation).

    4. To add the enemy & turret X & Y values, use the object expressions window again. click on either object (enemy or turret), and scroll down until you find the expressions for each items x&y. Add x&y values to the equation (for both objects).

    5. Once you've added the distance + x&y values, click on the second value box. Here you'll need to add a comparison value for your equation. So for example, if you want the turret to recognise an enemy when it gets within 50px you'd add 50 in this box.

    Hope that helps to get you going in the right direction.

    Good luck :)

  • Hey blacksmith, thanks that cleared things up a hell of a lot. When you say to add the player and enemy values , do you mean to add them as parameters for the distance part of the equation? In the parenthesis i mean?

    I tried to add them after but it didn't work, so i added them in place of the (presumably) stockholder x1/y1 parts. I managed to get that part working and hooked up to a variable in text form on the screen just to test it, but the number shoots up at lightning speed so i still need to work that out. I figured a wait action would work but i think i'm placing it somewhere where it's doing nothing.

    Thanks again for your help.


  • pmurph,

    Yes you got it, add the relevant x,y values (for both objects) into the parenthesis for the distance expression.

    Not sure why you're still having problems, did you also add a comparison value into the 'second value' box?

    If you don't have any luck yourself, you can always post your .capx on the forum and maybe someone could take a look at it for you.

    If you haven't done this before, it's pretty straight forward. Just save your project as a single file (option in file menu). Then place it somewhere like Dropbox or Google Drive. Then you can post the http link on the forum, so that others can access the file.

    Also worth pointing out that using before a user name (e.g. pmurph) will alert the user that you're contacting them.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Blacksmith , Thanks, I was never entirely sure how the whole username thing actually worked. And it's not so much that i'm having problems with what i originally said i wanted to do, it's adding a value to my variable and working perfectly in that regard.

    It's just that it seems to be constantly adding 1 to it as though it were every tick, which i'm fairly sure isn't one of my conditions here.


  • Ah, i figured out a workaround. Just adding the wait 1.0 seconds as a condition instead of an action directly after the number was added to the variable(What i was trying to do before)

  • pmurph, good to hear you got it working the way you wanted!

  • dl.dropboxusercontent.com/u/151998804/TowerDefense/index.html

    Seem to be having a problem with this wee prototype where the pathfinding isn't working at all like i thought. The path is at the start of the layout set to Tower.X and Tower.Y ,

    the enemys all move differently despite being symetrical and being the same object.

    Not to mention they group to the left hand side of the tower instead of gathering around it, The image point in my tower is set perfectly in the middle of the square too. I feel like i'm missing something glaringly obvious here.


  • whoooops forgot the capx ,https://dl.dropboxusercontent.com/u/151998804/TowerDefense/RandomTowerDefense.capx , Thanks Blacksmith! Couldn't have got it working without your help. :)

  • pmurph,

    No Problem, glad I could help out!

    I haven't used pathfinder yet, so I'm sorry I can't be much help on this one (as mentioned, I still have loads to learn myself!).

    But its a really friendly forum, and C2'er are a very helpful bunch. So I'm sure someone will be able to assist you.

    Edit: as this is a separate issue it might benefit you to start a new thread about it. Otherwise people may not pick up on your pathfinder problem ;)

  • Blacksmith , Probably a good call. I'll mess with it a little and see if i can coax it into working. If i can't be later on today i'll post it, wouldn't wan't to flood up the forums!


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