Concepts
Introduction
Game board
Coordinates

Code
moveTo()

Game hints
Attacks vs range

Lesson 4 - Move

Introduction

In this lesson, we'll learn how to command your Dragons to move! Soon, you will master the skies. To do that, we need to first learn about space.

Game board

The Arena's game board is a 2-D space. You can think of it like a piece of paper in the shape of a square. Your Dragons start each game at different spots on this square. During a game, you can tell them to move to different positions on this square.

To tell your Dragons to move, you'll need to know how to describe a position to move to. In a 2-D space, you can describe any position using two numbers. This pair of two numbers is called a coordinate.

Coordinates

An example of a coordinate is: (0,0). The first number is the x-coordinate and the second number is the y-coordinate. The x-coordinate describes how far left or right you are on the board. The y-coordinate describes how far up or down you are on the board.

The picture below shows what the game board looks like in this Solo Quest. (0,0) represents the center of the game board.
Your x-coordinate increases when you go right. It decreases when you go left.
Your y-coordinate increases when you go up. It decreases when you go down.


Your Dragon starts to the left of the center, at (-350, 0).
Its x-coordinate is -350 and its y-coordinate is 0.
x=-350 means it's 350 units left of the center.
y=0 means it's neither above or below the center -- it's right in the middle.
See how you can describe any position on the board by combining the x- and y- coordinates? Let's try a few more examples.

The Enemy Dragon is to the right of the center, at (350, 0).
x=350 means it's 350 units to the right of the center.
y=0 means it is neither above or below the center, just like your Dragon.

Here's a quiz: What are the coordinates of the corners of the game board? Stop reading and look at the picture above and try to figure it out. Then check back here to see if you got it right.
Answer: The top-left corner is (-1000, 1000). The top-right corner is (1000, 1000). The bottom-left corner is (-1000, -1000). The bottom-right corner is (1000, -1000). Good work!

Game.moveTo()

Now that you know how coordinates work, you now have the "secret key" to command your Dragons to move. Whenever you want to move your Dragon, call this Game function:

Game.moveTo(unitID, x, y)

Attacks vs range

In this Quest, your Dragon starts on the left and the enemy Dragon starts on the right. If you ask your Dragon to attack from where it is, it is slightly too far to be of any threat to the enemy Dragon.

What you have to do is move closer to the enemy Dragon. This is so your Dragon can do enough damage to defeat the enemy Dragon before time runs out. Remember: Dragons do more damage the closer they are to their target.


For example, you can update your run function so it looks like the following: function run(state) { // get array of all my units var myUnits = Helper.getMyUnits(state); // get the first dragon in the array var dragon = myUnits[0]; // get first dragon's ID var dragonId = dragon.id; // get this dragon's closest enemy var enemyFound = Helper.getClosestEnemy(state, dragon); if (!enemyFound) { // yay! no more enemies -- nothing left to do. // calling "return" stops this function from // executing any other code below. return; } // get enemy dragon's ID var enemyId = enemyFound.unit.id; // move my Dragon to the center of the Arena Game.moveTo(dragonId, 0, 0); // attack! Game.attack(dragonId, enemyId); }
Hint: you can look up the position of any Dragon in the Arena via unit.pos. E.g., you can print any unit's position to the Javascript console like this: console.log(unit.pos); Bring up the console by pressing F12 in Chrome. What you'll see in the console is something like this: {x: 0, y: 0, z: 0}.

Congratulations, with this quest conquered, you'll graduate from Nooblet to Novice!

Cheatcode

Spoiler alert! Click to reveal full solution.
<!DOCTYPE html> <html> <body> <div id="display">hello</div> <script> function run(state) { // get array of all my units var myUnits = Helper.getMyUnits(state); // get the first dragon in the array var dragon = myUnits[0]; // get first dragon's ID var dragonId = dragon.id; // get this dragon's closest enemy var enemyFound = Helper.getClosestEnemy(state, dragon); if (!enemyFound) { // yay! no more enemies -- nothing left to do. // calling "return" stops this function from // executing any other code below. return; } // get enemy dragon's ID var enemyId = enemyFound.unit.id; // move my Dragon to the center of the Arena Game.moveTo(dragonId, 0, 0); // attack! Game.attack(dragonId, enemyId); } Game.register(run); </script> </body> </html>