event handling example tweaks: consistent form for event-handling.
This commit is contained in:
parent
2e7dfb2453
commit
d73ab1a083
2 changed files with 88 additions and 74 deletions
|
@ -926,15 +926,16 @@ pointing at the block, type the following into the in-game prompt...
|
||||||
so the next step is to repeat this over and over. This is where `for`
|
so the next step is to repeat this over and over. This is where `for`
|
||||||
loops come in. Open your favorite text editor and create a new file in
|
loops come in. Open your favorite text editor and create a new file in
|
||||||
your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then
|
your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then
|
||||||
type the following...
|
type the following code and save:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var myskyscraper = function(floors) {
|
function myskyscraper( floors ) {
|
||||||
var i ;
|
var i ;
|
||||||
if ( typeof floors == 'undefined' ) {
|
if ( typeof floors == 'undefined' ) {
|
||||||
floors = 10;
|
floors = 10;
|
||||||
}
|
}
|
||||||
this.chkpt('myskyscraper'); // saves the drone position so it can return there later
|
// bookmark the drone's position so it can return there later
|
||||||
|
this.chkpt('myskyscraper');
|
||||||
for ( i = 0; i < floors; i++ ) {
|
for ( i = 0; i < floors; i++ ) {
|
||||||
this
|
this
|
||||||
.box(blocks.iron,20,1,20)
|
.box(blocks.iron,20,1,20)
|
||||||
|
@ -942,13 +943,14 @@ var myskyscraper = function(floors) {
|
||||||
.box0(blocks.glass_pane,20,3,20)
|
.box0(blocks.glass_pane,20,3,20)
|
||||||
.up(3);
|
.up(3);
|
||||||
}
|
}
|
||||||
this.move('myskyscraper'); // return to where we started
|
// return the drone to where it started
|
||||||
|
this.move('myskyscraper');
|
||||||
};
|
};
|
||||||
var Drone = require('drone');
|
var Drone = require('drone');
|
||||||
Drone.extend( myskyscraper );
|
Drone.extend( myskyscraper );
|
||||||
```
|
```
|
||||||
|
|
||||||
... so this takes a little explaining. First I create a new function
|
So this takes a little explaining. First I create a new function
|
||||||
called myskyscraper that will take a single parameter `floors` so that
|
called myskyscraper that will take a single parameter `floors` so that
|
||||||
when you eventually call the `myskyscraper()` function you can tell it
|
when you eventually call the `myskyscraper()` function you can tell it
|
||||||
how many floors you want built. The first statement in the function
|
how many floors you want built. The first statement in the function
|
||||||
|
@ -963,11 +965,11 @@ The last 2 lines load the drone module (it must be loaded before I can
|
||||||
add new features to it) and the last line extends the 'Drone' object
|
add new features to it) and the last line extends the 'Drone' object
|
||||||
so that now it can build skyscrapers among other things. Once you've
|
so that now it can build skyscrapers among other things. Once you've
|
||||||
typed in the above code and saved the file, type `/js refresh()` in your
|
typed in the above code and saved the file, type `/js refresh()` in your
|
||||||
in-game prompt, then type ...
|
in-game prompt, then type:
|
||||||
|
|
||||||
/js myskyscraper(2);
|
/js myskyscraper(2);
|
||||||
|
|
||||||
... A two-story skyscraper should appear. If you're feeling
|
A two-story skyscraper should appear. If you're feeling
|
||||||
adventurous, try a 10 story skyscraper! Or a 20 story skyscraper!
|
adventurous, try a 10 story skyscraper! Or a 20 story skyscraper!
|
||||||
Minecraft has a height limit (256 blocks from bedrock) beyond which
|
Minecraft has a height limit (256 blocks from bedrock) beyond which
|
||||||
you can't build. If you try to build higher than this then building
|
you can't build. If you try to build higher than this then building
|
||||||
|
@ -987,11 +989,11 @@ All the programs we have seen so far have been fairly predictable - they went
|
||||||
straight through the statements, and then went back to the beginning again. This is
|
straight through the statements, and then went back to the beginning again. This is
|
||||||
not very useful. In practice the computer would be expected to make decisions and
|
not very useful. In practice the computer would be expected to make decisions and
|
||||||
act accordingly. The javascript statement used for making decisions is `if`.
|
act accordingly. The javascript statement used for making decisions is `if`.
|
||||||
While standing on the ground in-game, type the following at the command prompt...
|
While standing on the ground in-game, type the following at the command prompt:
|
||||||
|
|
||||||
/js if ( self.onGround ) { echo('You are not flying!'); }
|
/js if ( self.onGround ) { echo('You are not flying!'); }
|
||||||
|
|
||||||
the following message should have appeared on your screen...
|
the following message should have appeared on your screen:
|
||||||
|
|
||||||
You are not flying!
|
You are not flying!
|
||||||
|
|
||||||
|
@ -1003,7 +1005,7 @@ keyboard, the statement you entered previously should reappear.
|
||||||
|
|
||||||
/js if ( self.onGround ) { echo('You are not flying!'); }
|
/js if ( self.onGround ) { echo('You are not flying!'); }
|
||||||
|
|
||||||
... this time no message should appear on your screen.
|
This time no message should appear on your screen.
|
||||||
|
|
||||||
The `if` statement tests to see if something is `true` or `false` and
|
The `if` statement tests to see if something is `true` or `false` and
|
||||||
if `true` then the block of code between the curly braces ( `{` and
|
if `true` then the block of code between the curly braces ( `{` and
|
||||||
|
@ -1012,11 +1014,11 @@ in the above example is `!self.onGround` (self is not on ground) which
|
||||||
will be `true` if you are currently flying or `false` if you aren't.
|
will be `true` if you are currently flying or `false` if you aren't.
|
||||||
|
|
||||||
What if you wanted to display a message only if a condition is *not*
|
What if you wanted to display a message only if a condition is *not*
|
||||||
true ? For example to only display a message if the player is *not* on the ground...
|
true ? For example to only display a message if the player is *not* on the ground:
|
||||||
|
|
||||||
/js if ( !self.onGround ) { echo ('You are flying!'); }
|
/js if ( !self.onGround ) { echo ('You are flying!'); }
|
||||||
|
|
||||||
... This code differs in that now there's a `!` (the exclamation mark)
|
This code differs in that now there's a `!` (the exclamation mark)
|
||||||
before `self.onGround`. The `!` symbol negates (returns the opposite of)
|
before `self.onGround`. The `!` symbol negates (returns the opposite of)
|
||||||
whatever follows it.
|
whatever follows it.
|
||||||
|
|
||||||
|
@ -1098,11 +1100,12 @@ just specify the fully qualified class name instead. E.g. ...
|
||||||
If you want an event handler to only execute once, you can remove the handler like this...
|
If you want an event handler to only execute once, you can remove the handler like this...
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
events.blockDestroy( function( evt ) {
|
function myBlockDestroyHook( evt ) {
|
||||||
var breaker = evt.player;
|
var breaker = evt.player;
|
||||||
echo( breaker, 'You broke a block');
|
echo( breaker, 'You broke a block');
|
||||||
this.unregister();
|
this.unregister();
|
||||||
} );
|
}
|
||||||
|
events.blockDestroy( myBlockDestroyHook );
|
||||||
```
|
```
|
||||||
|
|
||||||
The `this.unregister();` statement will remove this function from the
|
The `this.unregister();` statement will remove this function from the
|
||||||
|
@ -1114,7 +1117,11 @@ to stop listening for events.
|
||||||
To unregister a listener *outside* of the listener function...
|
To unregister a listener *outside* of the listener function...
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var myBlockBreakListener = events.blockDestroy(function( evt ) { ... } );
|
function myBlockDestroyHook( evt ){
|
||||||
|
var breaker = evt.player;
|
||||||
|
echo( breaker, 'You broke a block');
|
||||||
|
}
|
||||||
|
var myBlockBreakListener = events.blockDestroy( myBlockDestroyHook );
|
||||||
...
|
...
|
||||||
myBlockBreakListener.unregister();
|
myBlockBreakListener.unregister();
|
||||||
```
|
```
|
||||||
|
|
37
src/docs/templates/ypgpm.md
vendored
37
src/docs/templates/ypgpm.md
vendored
|
@ -884,15 +884,16 @@ pointing at the block, type the following into the in-game prompt...
|
||||||
so the next step is to repeat this over and over. This is where `for`
|
so the next step is to repeat this over and over. This is where `for`
|
||||||
loops come in. Open your favorite text editor and create a new file in
|
loops come in. Open your favorite text editor and create a new file in
|
||||||
your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then
|
your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then
|
||||||
type the following...
|
type the following code and save:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var myskyscraper = function(floors) {
|
function myskyscraper( floors ) {
|
||||||
var i ;
|
var i ;
|
||||||
if ( typeof floors == 'undefined' ) {
|
if ( typeof floors == 'undefined' ) {
|
||||||
floors = 10;
|
floors = 10;
|
||||||
}
|
}
|
||||||
this.chkpt('myskyscraper'); // saves the drone position so it can return there later
|
// bookmark the drone's position so it can return there later
|
||||||
|
this.chkpt('myskyscraper');
|
||||||
for ( i = 0; i < floors; i++ ) {
|
for ( i = 0; i < floors; i++ ) {
|
||||||
this
|
this
|
||||||
.box(blocks.iron,20,1,20)
|
.box(blocks.iron,20,1,20)
|
||||||
|
@ -900,13 +901,14 @@ var myskyscraper = function(floors) {
|
||||||
.box0(blocks.glass_pane,20,3,20)
|
.box0(blocks.glass_pane,20,3,20)
|
||||||
.up(3);
|
.up(3);
|
||||||
}
|
}
|
||||||
this.move('myskyscraper'); // return to where we started
|
// return the drone to where it started
|
||||||
|
this.move('myskyscraper');
|
||||||
};
|
};
|
||||||
var Drone = require('drone');
|
var Drone = require('drone');
|
||||||
Drone.extend( myskyscraper );
|
Drone.extend( myskyscraper );
|
||||||
```
|
```
|
||||||
|
|
||||||
... so this takes a little explaining. First I create a new function
|
So this takes a little explaining. First I create a new function
|
||||||
called myskyscraper that will take a single parameter `floors` so that
|
called myskyscraper that will take a single parameter `floors` so that
|
||||||
when you eventually call the `myskyscraper()` function you can tell it
|
when you eventually call the `myskyscraper()` function you can tell it
|
||||||
how many floors you want built. The first statement in the function
|
how many floors you want built. The first statement in the function
|
||||||
|
@ -921,11 +923,11 @@ The last 2 lines load the drone module (it must be loaded before I can
|
||||||
add new features to it) and the last line extends the 'Drone' object
|
add new features to it) and the last line extends the 'Drone' object
|
||||||
so that now it can build skyscrapers among other things. Once you've
|
so that now it can build skyscrapers among other things. Once you've
|
||||||
typed in the above code and saved the file, type `/js refresh()` in your
|
typed in the above code and saved the file, type `/js refresh()` in your
|
||||||
in-game prompt, then type ...
|
in-game prompt, then type:
|
||||||
|
|
||||||
/js myskyscraper(2);
|
/js myskyscraper(2);
|
||||||
|
|
||||||
... A two-story skyscraper should appear. If you're feeling
|
A two-story skyscraper should appear. If you're feeling
|
||||||
adventurous, try a 10 story skyscraper! Or a 20 story skyscraper!
|
adventurous, try a 10 story skyscraper! Or a 20 story skyscraper!
|
||||||
Minecraft has a height limit (256 blocks from bedrock) beyond which
|
Minecraft has a height limit (256 blocks from bedrock) beyond which
|
||||||
you can't build. If you try to build higher than this then building
|
you can't build. If you try to build higher than this then building
|
||||||
|
@ -945,11 +947,11 @@ All the programs we have seen so far have been fairly predictable - they went
|
||||||
straight through the statements, and then went back to the beginning again. This is
|
straight through the statements, and then went back to the beginning again. This is
|
||||||
not very useful. In practice the computer would be expected to make decisions and
|
not very useful. In practice the computer would be expected to make decisions and
|
||||||
act accordingly. The javascript statement used for making decisions is `if`.
|
act accordingly. The javascript statement used for making decisions is `if`.
|
||||||
While standing on the ground in-game, type the following at the command prompt...
|
While standing on the ground in-game, type the following at the command prompt:
|
||||||
|
|
||||||
/js if ( self.onGround ) { echo('You are not flying!'); }
|
/js if ( self.onGround ) { echo('You are not flying!'); }
|
||||||
|
|
||||||
the following message should have appeared on your screen...
|
the following message should have appeared on your screen:
|
||||||
|
|
||||||
You are not flying!
|
You are not flying!
|
||||||
|
|
||||||
|
@ -961,7 +963,7 @@ keyboard, the statement you entered previously should reappear.
|
||||||
|
|
||||||
/js if ( self.onGround ) { echo('You are not flying!'); }
|
/js if ( self.onGround ) { echo('You are not flying!'); }
|
||||||
|
|
||||||
... this time no message should appear on your screen.
|
This time no message should appear on your screen.
|
||||||
|
|
||||||
The `if` statement tests to see if something is `true` or `false` and
|
The `if` statement tests to see if something is `true` or `false` and
|
||||||
if `true` then the block of code between the curly braces ( `{` and
|
if `true` then the block of code between the curly braces ( `{` and
|
||||||
|
@ -970,11 +972,11 @@ in the above example is `!self.onGround` (self is not on ground) which
|
||||||
will be `true` if you are currently flying or `false` if you aren't.
|
will be `true` if you are currently flying or `false` if you aren't.
|
||||||
|
|
||||||
What if you wanted to display a message only if a condition is *not*
|
What if you wanted to display a message only if a condition is *not*
|
||||||
true ? For example to only display a message if the player is *not* on the ground...
|
true ? For example to only display a message if the player is *not* on the ground:
|
||||||
|
|
||||||
/js if ( !self.onGround ) { echo ('You are flying!'); }
|
/js if ( !self.onGround ) { echo ('You are flying!'); }
|
||||||
|
|
||||||
... This code differs in that now there's a `!` (the exclamation mark)
|
This code differs in that now there's a `!` (the exclamation mark)
|
||||||
before `self.onGround`. The `!` symbol negates (returns the opposite of)
|
before `self.onGround`. The `!` symbol negates (returns the opposite of)
|
||||||
whatever follows it.
|
whatever follows it.
|
||||||
|
|
||||||
|
@ -1056,11 +1058,12 @@ just specify the fully qualified class name instead. E.g. ...
|
||||||
If you want an event handler to only execute once, you can remove the handler like this...
|
If you want an event handler to only execute once, you can remove the handler like this...
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
events.blockDestroy( function( evt ) {
|
function myBlockDestroyHook( evt ) {
|
||||||
var breaker = evt.player;
|
var breaker = evt.player;
|
||||||
echo( breaker, 'You broke a block');
|
echo( breaker, 'You broke a block');
|
||||||
this.unregister();
|
this.unregister();
|
||||||
} );
|
}
|
||||||
|
events.blockDestroy( myBlockDestroyHook );
|
||||||
```
|
```
|
||||||
|
|
||||||
The `this.unregister();` statement will remove this function from the
|
The `this.unregister();` statement will remove this function from the
|
||||||
|
@ -1072,7 +1075,11 @@ to stop listening for events.
|
||||||
To unregister a listener *outside* of the listener function...
|
To unregister a listener *outside* of the listener function...
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var myBlockBreakListener = events.blockDestroy(function( evt ) { ... } );
|
function myBlockDestroyHook( evt ){
|
||||||
|
var breaker = evt.player;
|
||||||
|
echo( breaker, 'You broke a block');
|
||||||
|
}
|
||||||
|
var myBlockBreakListener = events.blockDestroy( myBlockDestroyHook );
|
||||||
...
|
...
|
||||||
myBlockBreakListener.unregister();
|
myBlockBreakListener.unregister();
|
||||||
```
|
```
|
||||||
|
|
Reference in a new issue