# Test how far an object is from another?

Get help using Construct 2

### » Wed Jul 10, 2013 9:21 am

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.
B
5
S
1
Posts: 47
Reputation: 542

### » Wed Jul 10, 2013 9:57 am

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.

B
252
S
65
G
34
Posts: 907
Reputation: 41,526

### » Wed Jul 10, 2013 10:01 am

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)

I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

### » Wed Jul 10, 2013 10:20 am

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?

B
5
S
1
Posts: 47
Reputation: 542

### » Wed Jul 10, 2013 10:21 am

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)
B
5
S
1
Posts: 47
Reputation: 542

### » Wed Jul 10, 2013 10:43 am

@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 :)
B
34
S
6
G
7
Posts: 494
Reputation: 8,282

### » Wed Jul 10, 2013 11:03 am

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.

Paul.
B
5
S
1
Posts: 47
Reputation: 542

### » Wed Jul 10, 2013 11:16 am

@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.
B
34
S
6
G
7
Posts: 494
Reputation: 8,282

### » Wed Jul 10, 2013 11:40 am

@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.

Paul.
B
5
S
1
Posts: 47
Reputation: 542

### » Wed Jul 10, 2013 11:42 am

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)
B
5
S
1
Posts: 47
Reputation: 542

Next