Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Distance to code...The Agrippa Defense A (JS) [solved]

what is the code for distance to for code combat?

example of what the context is:
// Find the distance to the enemy with distanceTo.
// If the distance is less than 5 meters…

var distance = hero.distanceTo(target);

thank you and what would it be for distance<5?

What do you think it would be? If you are working on a level that needs the code, it should be expected that you’d know this already. If not, it might be a good idea to review prior levels.

Im in the sixth grade and I’m struggling since im not at school and can’t get help in person.

That is understandable and to be honest, I had figured it was something like that. :wink:

Still, that was a very good question to answer…how would you write an if statement? While sure, I can just give you the answer, but what would you learn from that? Instead, it would be best, for you, to walk through it and thereby learn from it…don’t ya think?


1 Like

I originally wrote the code as

 if (distance<5) {
            if (enemy) {
               var ready = hero.isReady("cleave"); 

I don’t think it is right though.

Thank you. :blush: (20 characters)

Now that is a good try!

So, the challenge is to test to see if the distance is less than 5…you have met that in the first ‘if’ statement, but…how have you defined ‘distance’? Remember, you define a variable by giving it a name, then determining (or specifying) its characteristics. Your statement ‘var ready = hero.isReady(“cleave”)’ is a perfect example of defining the variable ‘ready’…more on that part later.

I forgot how to define distance but would it be
distance = nearestEnemyDistance()

It’s Been a long time since I did this so excuse me for not remembering :sweat_smile:

Um, no…remember my first reply? That is exactly how. So, the code would look something like:

var distance = hero.distanceTo(target);
if (distance < 5) {
   // and so on

There are ways to simplify the code, combining certain steps in to one single line of code, but that is best saved for later.

Good joke :slightly_smiling_face:

what’s the joke?:joy:
(20 characters)

heh…it’s a xython thing. You get used to them, in time :stuck_out_tongue_winking_eye:

Thank you but now when I try to do that code along with my other code, it still won’t let me move on.

ohhhhhhhhhhhhhhhhhhh ok :joy:

Remember I said ‘we will come back to that’? your code as you have it now; the complete code please. Also, what level are you working on?

Noble @xython showed screenshot with hero saying distance to his pet found through Vector.distance way. It could be done so but I’m afraid, it’s a little bit early for you) For what level do you need this question?