Skip to content

Commit 1ee38d8

Browse files
committed
Merge pull request #247 from carlrobert/patch-17
Known times() limitation mentioned
2 parents e7e4b80 + 0b119da commit 1ee38d8

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

src/main/js/modules/drone/index.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -478,61 +478,65 @@ Drone.extend = function( name, func ) {
478478
/**************************************************************************
479479
### Drone.times() Method
480480
481-
The times() method makes building multiple copies of buildings
481+
The `times()` method makes building multiple copies of buildings
482482
easy. It's possible to create rows or grids of buildings without
483483
resorting to `for` or `while` loops.
484484
485485
#### Parameters
486486
487487
* numTimes : The number of times you want to repeat the preceding statements.
488488
489+
#### Limitation
490+
491+
For now, don't use `times()` inside a Drone method implementation – only use it at the in-game prompt as a short-hand workaround for loops.
492+
489493
#### Example
490494
491-
Say you want to do the same thing over and over. You have a couple of options...
495+
Say you want to do the same thing over and over. You have a couple of options:
492496
493-
* You can use a for loop...
497+
* You can use a `for` loop …
494498
495-
d = new Drone(); for ( var i =0;i < 4; i++) { d.cottage().right(8); }
499+
d = new Drone(); for ( var i = 0; i < 4; i++ ) { d.cottage().right(8); }
496500
497501
While this will fit on the in-game prompt, it's awkward. You need to
498-
declare a new Drone object first, then write a for loop to create the
499-
4 cottages. It's also error prone, even the `for` loop is too much
502+
declare a new Drone object first, then write a `for` loop to create the
503+
4 cottages. It's also error prone &ndash; even the `for` loop is too much
500504
syntax for what should really be simple.
501505
502-
* You can use a while loop...
506+
* You can use a `while` loop &hellip;
503507
504-
d = new Drone(); var i=4; while (i--) { d.cottage().right(8); }
508+
d = new Drone(); var i=4; while (i--) { d.cottage().right(8); }
505509
506-
... which is slightly shorter but still too much syntax. Each of the
510+
&hellip; which is slightly shorter but still too much syntax. Each of the
507511
above statements is fine for creating a 1-dimensional array of
508512
structures. But what if you want to create a 2-dimensional or
509513
3-dimensional array of structures? Enter the `times()` method.
510514
511515
The `times()` method lets you repeat commands in a chain any number of
512516
times. So to create 4 cottages in a row you would use the following
513-
statement...
517+
statement:
514518
515519
cottage().right(8).times(4);
516520
517-
...which will build a cottage, then move right 8 blocks, then do it
521+
&hellip; which will build a cottage, then move right 8 blocks, then do it
518522
again 4 times over so that at the end you will have 4 cottages in a
519-
row. What's more the `times()` method can be called more than once in
523+
row. What's more, the `times()` method can be called more than once in
520524
a chain. So if you wanted to create a *grid* of 20 houses ( 4 x 5 ),
521-
you would do so using the following statement...
525+
you would do so using the following statement:
522526
523527
cottage().right(8).times(4).fwd(8).left(32).times(5);
524528
525-
... breaking it down...
529+
&hellip; breaking it down &hellip;
526530
527531
1. The first 3 calls in the chain ( `cottage()`, `right(8)`, `times(4)` ) build a single row of 4 cottages.
528532
529-
2. The last 3 calls in the chain ( `fwd(8)`, `left(32)`, `times(5)` ) move the drone forward 8 then left 32 blocks (4 x 8) to return to the original x coordinate, then everything in the chain is repeated again 5 times so that in the end, we have a grid of 20 cottages, 4 x 5. Normally this would require a nested loop but the `times()` method does away with the need for loops when repeating builds.
533+
2. The last 3 calls in the chain ( `fwd(8)`, `left(32)`, `times(5)` ) move the drone forward 8 then left 32 blocks (4 x 8) to return to the original X coordinate, then everything in the chain is repeated again 5 times so that in the end, we have a grid of 20 cottages, 4 x 5. Normally this would require a nested loop but the `times()` method does away with the need for loops when repeating builds.
530534
531-
Another example: This statement creates a row of trees 2 by 3 ...
535+
Another example: This statement creates a row of trees 2 by 3:
532536
533537
oak().right(10).times(2).left(20).fwd(10).times(3)
534538
535-
... You can see the results below.
539+
&hellip; You can see the results below.
536540
537541
![times example 1](img/times-trees.png)
538542

0 commit comments

Comments
 (0)