Activating a sound when entity moves

Blitz3D Forums/Blitz3D Beginners Area/Activating a sound when entity moves

seferey(Posted 2006) [#1]
I was wondering if there's a way to attach a sound to the keyboard keys that move your entity


WolRon(Posted 2006) [#2]
Do you mean this?
If KeyHit(30) Then PlaySound(AKeySound)
If KeyHit(31) Then PlaySound(SKeySound)
If KeyHit(32) Then PlaySound(DKeySound)



seferey(Posted 2006) [#3]
Yes thank you soo much :) WolRon


I'm still waiting for aliencodec.com to work again but I hope I can still buy the tree makers and stuff


seferey(Posted 2006) [#4]
There is one more question to ask

I want to load one sound file at a time but both of them load up at the same time

here's my code


;Name of application
AppTitle "My First Terrain And First animated Character"

Print "Press F12 to exit"
Print "Press F11 to see Wireframe Mode."
Print "After the wheels start moving"
Print "Press Spacebar to move the car."

Delay 3000

;Initialise Graphics mode
Graphics3D 1024,768,32,1

SetBuffer BackBuffer()

Const GRAVITY#=-0.01

;Camera has to be set, so the scene is visible
  
Camera=CreateCamera()
CameraRange Camera,1,8000 
;CameraFogMode camera,1
;CameraFogColor camera,1,150,150
;CameraFogColor camera,20,23,150
PositionEntity camera,30,0,1,True
 
;Light The Scene, so that it doesn't look flat

Light=CreateLight()

RotateEntity Light,90,0,0

;Create the "Terrain" Entity...
terrain=LoadMesh("Land\Land2.b3d")
PositionEntity terrain,40,17,0
RotateEntity terrain,0,0,0 
TranslateEntity terrain,40,-20,-.1 
;...and texture it
Grass=LoadTexture("Land\terrain-1.jpg")
EntityTexture terrain,Grass

;Create A Border Line 

Wall=CreateCube() 
ScaleMesh Wall,600,600,600
PositionMesh Wall,60,17,0 
EntityAlpha Wall,0
EntityOrder Wall,20
FlipMesh Wall

;Create a skybox using a model sphere

Sky1=CreateSphere(32)
ScaleMesh Sky1,5000,5000,5000
PositionEntity Sky1,30,-.9000,0 
Sky=LoadTexture("Sky\sky.bmp") 
EntityTexture Sky1,Sky
EntityFX Sky1,1
EntityOrder Sky1,20
FlipMesh Sky1

;FOG_RANGE=400

;Create the "Box" entity and animate it
box=LoadAnimMesh("model\box.b3d")
ScaleEntity box,.1,.1,.1
PositionEntity box,0,-.9000,5
TranslateEntity box,30,-.70,-.70
EntityShininess box,1
EntityBlend box,1
SetAnimKey box,15,True,True,True
AddAnimSeq(box,15)
Animate box,1,2,0,100

;Set Collision types fot the "collisions" command 1-player, 2-ground 3-border
type_box=1
type_terrain=2
type_Wall=2
EntityType box,1
EntityType terrain,2
EntityType Wall,2

;Adjust Radius of Sphere, that's used for Sphere-Polygon collisions
box_radius#=1
EntityRadius box,box_radius
;Initialise Collisions between player,ground and border

Collisions type_box,type_terrain,2,3 
Collisions type_terrain,type_box,2,3
Collisions type_Wall,type_terrain,2,3 
Collisions type_terrain,type_Wall,2,3

;Dither scene, so that it does look good with 16 Bit Color-Depth

; Toggle dither enable value between true and false when F10 is pressed

If KeyHit( 68 )=True Then enable=1-enable

;Dither enable 

;other information about gravity

speed#=0
x_vel#=0:prev_x#=EntityX( box )
y_vel#=0:prev_y#=EntityY( box )
z_vel#=0:prev_z#=EntityZ( box )

;Play and Loop some sound :)

box1=LoadSound("Sounds\car start.wav") 

Listener=CreateListener(Camera,box,1,1) 

EmitSound box1,box

PlaySound box1 

PauseChannel box2

box2=LoadSound("Sounds\gpidle.wav")

EmitSound box2,box 



; Create the timer to track speed
frameTimer=CreateTimer(60)

;Main Loop

While Not KeyHit(88) ;press F12 To Exit

;Press F11 to see the world in Wireframe mode
 
If KeyHit(87)=True Then enable=1-enable 
  
   WireFrame enable 

;CameraFogRange camera,1,FOG_RANGE

   ;calculate box velocities	
	cx#=EntityX( box ):x_vel=cx-prev_x:prev_x=cx
	cy#=EntityY( box ):y_vel=cy-prev_y:prev_y=cy
	cz#=EntityZ( box ):z_vel=cz-prev_z:prev_z=cz

EntityParent(camera,box,1)  

;Keyboard Input (cursor-keys) For moving the player around
    
If KeyHit(57) 
  If move_entities = True 
    move_entities = False 
  Else 
    move_entities = True   
  EndIf 
EndIf 

If move_entities = False 
  
  ;all of your movement code
  If KeyDown(30)=True Then TurnEntity box,0,1,0 
  If KeyDown(32)=True Then TurnEntity box,0,-1,0 
  If KeyDown(31)=True Then MoveEntity box,0,0,-.01 
  If KeyDown(17)=True Then MoveEntity box,0,0,.01 
  If EntityCollided( box,terrain)  
    If KeyDown(17) 
      speed=speed+.00 
      If speed>.0 speed=.0 
    Else If KeyDown(31)
      speed=speed-.02
      If speed<-.5 speed=-.5
    Else
      speed=speed*.9
    EndIf
    MoveEntity box,0,0,speed
    TranslateEntity box,0,GRAVITY#-.01,0
  Else
    TranslateEntity box,x_vel,y_vel+GRAVITY,z_vel
  EndIf
EndIf

TurnEntity Sky1,0,.02,0 

PositionEntity Sky1,EntityX(camera),EntityY(camera),EntityZ(camera)

  If KeyDown(17)=False Then PlaySound("Sounds\gpidle.wav")

WaitTimer(frameTimer) ; Pause until the timer reaches 60

Cls 

;Update the animation-Frames and Render the calculated scene, Flip Back- with Frontbuffer after that, so the new frame becomes visible
  UpdateWorld
  RenderWorld
  Flip  

Wend

;Free the Memory and end the program
ClearWorld

End




WolRon(Posted 2006) [#5]
First of all, it only pays to post the RELEVANT code.
I assume the relevant code in this case is:
;Play and Loop some sound :)
box1=LoadSound("Sounds\car start.wav") 
Listener=CreateListener(Camera,box,1,1) 
EmitSound box1,box
PlaySound box1 
PauseChannel box2
box2=LoadSound("Sounds\gpidle.wav")
EmitSound box2,box 
and
If KeyDown(17)=False Then PlaySound("Sounds\gpidle.wav")


Secondly, make your question clear. I don't know whether to answer your question literally as you asked it, OR to try to interpret what you may have meant...

Your question:
I want to load one sound file at a time but both of them load up at the same time

Taken literally:
Then just remove the other Load command, duh...
so erase these lines:
PauseChannel box2
box2=LoadSound("Sounds\gpidle.wav")
EmitSound box2,box 


Interpreted as something else
Assuming what you actually meant to ask was how to PLAY only one at a time, and since I ASSUME you want the "car start" sound to play first, then the idle sound second, you need to fix your mistake of attempting to PAUSE the idle sound channel before it's even created.
Notice these lines:
PauseChannel box2
box2=LoadSound("Sounds\gpidle.wav")
EmitSound box2,box 
Remember that code is always executed from top to bottom, so in the code above, the first thing you try to do is pause a channel that doesn't exist. In fact, you reference a variable that will later contain a SOUND HANDLE, not a SOUND CHANNEL.
Then you create a SOUND HANDLE by loading a sound and storing that HANDLE in the box2 variable.
Then you tell the sound to be emitted from the box.

To correct this, all you have to do is store the CHANNEL that is returned from the EmitSound command into some variable and (only then) pausing it.
So your three lines of code should look like this:
box2=LoadSound("Sounds\gpidle.wav")
IdleChannel = EmitSound box2,box
PauseChannel IdleChannel

Then later, when you want it to play, you should change your PlaySound code (which, by the way defeats the purpose of loading it earlier...) to read like this:
If (Not KeyDown(17)) And (Not ChannelPlaying(StartChannel)) Then ResumeChannel(IdleChannel)
Notice that I referenced a channel by the name of StartChannel? That's because you will have to remember that channel when you created it on this line:
EmitSound box1,box
So change it to read:
StartChannel = EmitSound box1,box

By the way, why are you telling it to play the box1 sound after you told it to emit the box1 sound? Pointless. Erase the PlaySound line:
PlaySound box1


...
You know, it would make more sense to name your sound handles and channels more descriptive names than box1 and box2.

...
By the way, if you want your sounds to diminish with distance (and other 3D sound effects) then you need to load your sounds with Load3DSound, not LoadSound.


seferey(Posted 2006) [#6]
Thanks WolRon for your help :)



oh and one more question. Is there a way to change a sky realistically using millisecs from day to night.

I wish I could use lightcolor command on the sky as well as the terrain.


IPete2(Posted 2006) [#7]
That depends on how you create your sky.

If you just want a very simple, plain blue sky bgd use cameraclscolor and change it's parameters from 0,0,255 to 0,0,0 over a number of seconds. I would probably also add in an ambientlight 255,255,255 command and decrease it's values too 255,255,255 - 0,0,0 over the same period. Here's a clue:

;outside main loop

r#=255
g#=255
b#=255

cameraclscolor 0,0,b
ambientlight r,g,b

.
.
.

Inside main loop


;to make world generally go darker
r=r-0.1
g=g-0.1
b=b-0.1


THEN use a function where
r=r+0.1
g=g+0.1
b=b+0.1

to make it go light again.


Don't forget to use these values in the main lopp thus:

ambientlight r,g,b
cameraclscolor 0,0,b


Also put in a check to ensure the light does not go into negative numbers (if r<0 then r=0 etc) or of course on the brighten function bigger than 255

IPete2.


seferey(Posted 2006) [#8]
what number do I change to show first baby blue then navy blue


WolRon(Posted 2006) [#9]
Sorry, but I don't understand your question...


IPete2(Posted 2006) [#10]
seferey,

You will need to decie the colours yourself - your thread is becoming 'can you programme this entire application for me please...' come on dude get off your butt and do some leg work already!

lol.

Okay that said the colours do this:

0,0,0, = black
255,0,0 = red
0,255,0 = green
255,255,0 = yellow
255,0,255 = magenta
0,0,255 = blue

The components are Red, Green, Blue i.e. R, G, B. Each component can have a 0 - 255 setting and every number inbetween

White would be 255,255,255 as all colours are set to their top setting - black is 0,0,0 - all colours set to their minimum setting.

Baby blue would be somewhere around: 100,160,255 and navy blue would be 8,50,100.

IPete2.


Ross C(Posted 2006) [#11]
If you go into Windows PAINT, and choose a colour there, you can see the R G B colours used.


seferey(Posted 2006) [#12]
by the when using RGB numbers from windows paint
in blitz 3D it's incorrect but When using Adobe photo Shop 7

the colors shows perfectly

and go ahead and delete this thread I'm done with this I gonna play with this One more time

see ya next week :)