
What represents the hand/finger of the player of a 3D game on the virtual space? The pointer over an object or the avatar body (his hand?) colliding against the object?
You can have your preference and use it at BBG. We saw how to use a MouseClick and now we will see how to capture the collision of the avatar.
Imagine that we will capture the collison of the avatar against that "blue key" of a previous lesson. The "event block" will be:
Actor.onCollision = function( oCollisionInfo ){
if(oCollisionInfo.collisionObject2 == bkEntity || oCollisionInfo.collisionObject1 == bkEntity ){
try{
// What to do if the collision occurs
}
catch(e){
}
}//if
}//onCollision
Very easy.
When someting happens (like a collision, by example) or at some other situations, the use of sound is very important in games. Blink3D has a Class Sound.
For the creation of an object/prefix of this Class, write, by example:
mySound = SoundSystem.createSound();
Having this object (you can have differents objects, like we said) you can use the properties:
mySound.setFileName( file name or address ); The following file formats are supported: .wav, .aiff, .ogg, .spx,
.flac, .mod, .xm, .s3m, .it . NOT FOR: mp3!
If the sound file is on a web site (.ogg and .flc formats only) you must
specify the full URL of the file.
TIP: The advantage of the .ogg file is that if is downloaded indirectly, it is streamed. So, use this trick if the file is big
You can download (7 MB) many (58) sound effects and Audacity 1.2.4b, a converter from some file types to .ogg and other Blink3D compatibles file types, from (rightclick):
Other elements:
mySound.ambient = true; The sound can have the same volume at any point of the Scene (true here)
or can be "positional", having atenuation far of some point.
mySound.volume = 1; From 0 to 1.
mySound.loopCount = 1; If you like or not to repeat the sound. The value 0 means: forever.
mySound.worldSpacePosition = Vector(x,y,z); The sound will be positional. Can be attached to a moveable object.
Set mySound.ambient to false.
mySound.near = 500; Set the distance from the positional sound source where volume will be max
mySound.far = 2000; Set the distance from the positional sound source where volume will be min
mySound.play(); Plays the previously specified sound. Has no effect if the sound is already
mySound.pause();
mySound.rewind();
mySound.stop();
mySound.attachToObject(SceneNode); The Sound object position is then automatically set to the position of the SceneNode.
If the SceneNode's positon changes so does the position of the sound object.
mySound.detachFromObject(SceneNode);
And an "event block":
mySound.onEnd = function(){
Chat.print( "Sound has finnished playing" );
}
The exercice of this lesson is: put the bluekey at some position over the ground and, using "collision detection", put it in the Inventory. When the collision occurs: a sound please! Complete code after the figure:
// Attach the Land
oLandEntity = Scene.createEntity( "Land", "land.mesh" );
Scene.rootNode.attachObject(oLandEntity );
oLandEntity.fixed = true;
oLandEntity.createPhysicalModel();
oLandEntity.active = false;
oLandEntity.position = Vector(0, 0, 0);
// Create a point light
oPointLight = Scene.createPointLight( "SceneLight" );
oPointLight.position = Vector( 20, 300, 50 );
Chat.print("Finding the Blue Key, collide the avatar against it to pick and put it in the Inventory");
Chat.print(" INVENTORY RULES: Open the INVENTORY combo-box and click an item to select it. Click 'USE ITEM' to use. Or click 'DESELECT ITEM'.");
bkEntity = Scene.createEntity( "BlueKey", "bluekey.mesh" );
bkNode = Scene.createSceneNode( "bkNode" );
bkNode.attachObject( bkEntity );
bkNode.translate(Vector(-1000 , 100 , -1500 ), bkNode.TRANSFORM_WORLD );
bkEntity.fixed = true;
bkEntity.createPhysicalModel();
bkEntity.active = true;
//Flag
bkUnPicked=true;
//Text
myText = GUISystem.createStaticText( "AnyName" );
myText.active = true;
myText.alwaysOnTop = true;
myText.text = "Exerc 11";
myText.absolutePositionX=0;
myText.absolutePositionY=0;
myText.backgroundEnabled = false;
myText.frameEnabled = true;
myText.absoluteWidth= 150 ;
myTextColorRect = GUISystem.createGUIColorRect();
myTextColorRect.setColors( Color( 1, 0, 0, 1 ));//red
myText.textColors= myTextColorRect;
GUISystem.addChildWindow(myText);
//ComboBox
myCB = GUISystem.createComboBox( "Invent" );
myCB.active = true;
myCB.alwaysOnTop = true;
myCB.absolutePositionX=300;
myCB.absolutePositionY=20;
myCB.normalTextColor = Color( 0, 1, 0, 1 );//green
myCB.readOnly=true;
myCB.absoluteHeight = 180;//width
myCB.absoluteWidth = 100;
myCB.text = "INVENTORY";
GUISystem.addChildWindow( myCB);
//Buttons
oNewButton = GUISystem.createButton( "BtDes" );
oNewButton.active = true;
oNewButton.alwaysOnTop = true;
oNewButton.text = "DESELECT ITEM";
oNewButton.absolutePositionX=100;
oNewButton.absolutePositionY=20;
oNewButton.absoluteWidth = 40;//HEIGHT
oNewButton.absoluteHeight = 180;//width
oNewButton.normalTextColor = Color( 0, 0, 1, 1 );//blue
oNewButton.onClicked = function( sWindowName){
myCB.clearAllSelections();
myCB.text = "INVENTORY";
}
oNewButton.addListener(oNewButton.ON_EVENT_CLICKED );
GUISystem.addChildWindow( oNewButton);
oButton = GUISystem.createButton( "BtSel" );
oButton.active = true;
oButton.alwaysOnTop = true;
oButton.text = "USE ITEM";
oButton.absolutePositionX=100;
oButton.absolutePositionY=70;
oButton.absoluteWidth = 40;//HEIGHT
oButton.absoluteHeight = 180;//width
oButton.normalTextColor = Color( 0, 0, 1, 1 );//blue
oButton.onClicked = function( sWindowName){
for(it=0;it<myCB.getItemCount();it++){
if ( myCB.isItemSelected(it) ) {
itext=myCB.getItem(it).text;
myCB.removeItem(myCB.getItem( it ));
Chat.print( "The item "+itext+" was used" );
myCB.update();
myCB.clearAllSelections();
myCB.text = "INVENTORY";
}
}//for
}
oButton.addListener(oButton.ON_EVENT_CLICKED );
GUISystem.addChildWindow( oButton);
//Sound
oSound = SoundSystem.createSound();
oSound.setFileName( "ziiip.ogg" ); //Available in the kit.
oSound.ambient = true;
oSound.volume = 1;
oSound.loopCount = 1;
//OnCollision
Actor.onCollision = function( oCollisionInfo ){
if( oCollisionInfo.collisionObject2==bkEntity || oCollisionInfo.collisionObject1==bkEntity ){
try{
if(bkUnPicked){
oSound.play();
myCB.addItem(GUISystem.createListBoxTextItem("Blue Key") );
Chat.print( "The avatarpicked the object BlueKey");
Chat.print( "Blue Key included in the Inventory" );
bkUnPicked=false;
bkNode.translate(Vector(-66600 ,-66600 , -66600 ), bkNode.TRANSFORM_WORLD );
bkEntity.collide= false;
}
}
catch(e){
}
}//if
}//onCollision
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
