
In the previous lesson you saw an "event block" of the Class Actor, that has elements about the avatar behavior.
We can use a method of the same Class for, using a timestep loop, create another "event block" that will trigger some action when the avatar is "n" centimeters near some point in the ground. Look this sample:
isFar=true;
myLoop1 = new Object();
myLoop1.timestep = function(appLag, dt){
if(Actor.dist(Vector(3000,0, -2000)) < 500 && isFar){
Chat.print("I am near the point 3000,0, -2000 ");
isFar=false;
}
}
Application.addTimestep(myLoop1 );
TIPS: Do not use the position of an object like the vector.
The distance is really from the "camera", normally 200/300 cm. behind the avatar.
The exercice of this lesson is: when the avatar is near a character (a "bbgerian"), this character will ask a question to be answered by the player clicking one of the numeric keys of the keyboard.
But we need first to learn how to capture the press of a keyboard key. There are the Class Key.
We will not present each one of the elements of the Class but a generic sample of its use:
notPressed1=true;
notPressed2=true;
notPressed3=true;
oKeyListener = new Object();
oKeyListener.onEvent = function(){
if ( Key.isKeyDown( Key.KEY_1) && notPressed1 ){
Chat.print( "Pressed 1" );
notPressed1=false;
}
if ( Key.isKeyDown( Key.KEY_2) && notPressed2 ){
Chat.print( "Pressed 2" );
notPressed2=false;
}
if ( Key.isKeyDown( Key.KEY_3) && notPressed3 ){
Chat.print( "Pressed 3" );
notPressed3=false;
}
}
Key.addListener(oKeyListener );
There are some values to specify a key. The complete list you find in the documentation:
KEY_1 KEY_2 KEY_3 KEY_4 . . KEY_A KEY_B KEY_C KEY_D . . KEY_UP KEY_LEFT KEY_RIGHT KEY_DOWN . . KEY_DELETE . .
Look two pictures of the exercice. Try to do it by yourself. The complete code is after the figures.
TIP: You can download all you need (included .BLEND file) for the creation of a customized "bbgerian":
// Attach the Land
oLandEntity = Scene.createEntity( "Land", "land.mesh" );
Scene.rootNode.attachObject(oLandEntity );
oLandEntity.fixed = true;
oLandEntity.collisionGeometryType = oLandEntity.COLLISION_GEOM_TRI_SOUP;
oLandEntity.createPhysicalModel();
oLandEntity.active = false;
oLandEntity.position = Vector(0, 0, 0);
// Create a light
oDistantLight = Scene.createDistantLight( "light" );
Scene.shadowTechnique = Scene.SHADOW_STENCIL_MODULATIVE;
Scene.shadowColor = Color (0.7, 0.7, 0.7, 0 );
oDistantLight.diffuseColor = Color( 1, 1, 1, 0 );
oDistantLight.specularColor = Color( 1, 1, 1, 0 );
oDistantLight.direction = Vector( -0.45, -1, -1 );
oDistantLight.castShadows = true;
//The bbgerian
oObjEntity = Scene.createEntity( "Obj", "bbgerian.mesh" );
oObjNode = Scene.createSceneNode( "ObjNode" );
oObjNode.attachObject( oObjEntity );
oObjNode.translate(Vector(3000,0, -2000 ), oObjNode.TRANSFORM_WORLD );
oObjEntity.fixed = true;
oObjEntity.collisionGeometryType = oObjEntity.COLLISION_GEOM_TRI_SOUP;
oObjEntity.createPhysicalModel();
oObjEntity.active = true;
Chat.print("Find the bbgerian. He has a question for you.");
//Flags
isFar=true;
notPressed1=true;
notPressed2=true;
notPressed3=true;
//Sound
oSound = SoundSystem.createSound();
oSound.setFileName( "aplause1.ogg" ); ////Included in the sound kit
oSound.ambient = true;
oSound.volume = 1;
oSound.loopCount = 1;
//Capturing distance
myLoop1 = new Object();
myLoop1.timestep = function(appLag, dt){
if(Actor.dist(Vector(3000,0, -2000)) < 500 && isFar){
Chat.print("What is the name of a river from China: 1 - Amazon; 2 - Nile; 3 - Yellow. \nPress the key of the correct number.");
isFar=false;
}
}
Application.addTimestep(myLoop1 );
//Capturing key press
oKeyListener = new Object();
oKeyListener.onEvent = function(){
if ( Key.isKeyDown( Key.KEY_1) && notPressed1 ){
Chat.print( "Wrong answer.Amazon is in Brazil.Try again." );
notPressed1=false;
}
if ( Key.isKeyDown( Key.KEY_2) && notPressed2 ){
Chat.print( "Wrong answer.Nile is in Egipt. Try again." );
notPressed2=false;
}
if ( Key.isKeyDown( Key.KEY_3) && notPressed3 ){
oSound.play();
Chat.print( "Correct! Congratulation! " );
notPressed3=false;
}
}
Key.addListener(oKeyListener );
NOTE OF THE BETA VERSION: There are the methods OnChatIn() and OnChatOut() of the Class Chat tha could be used for "dialog applications". But they are not working today.
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
