PDA

View Full Version : Sounds!!!!!!!!!!!!!



PSPKOR
October 15th, 2006, 15:37
OK I've been stuck with this all weekend with no luck, can some one tell me whats wrong with this bit of code:
if playerMove == "no" and loopCount == 0 then
tankidle:play()
loopCount = loopCount + 1
loopCount2 = 0
tankdrive:stop()
end
if playerMove == "yes" and loopCount2 == 0 then
tankdrive:play()
loopCount2 = loopCount2 + 1
loopCount = 0
tankidle:stop()
end
Its playing .wav sounds and they are looped.

I did also try doing another way of playing it using the
local sound = filename
voice = sound:play()
but that didn't work very well, beacause every time I tried to stop one sound it was ok but if i tried stoping the other it just wouldn't work.

So if someone could help me out here please.

yaustar
October 15th, 2006, 17:15
Wouldn't this be better in terms of logic flow?

if playerMove == "no" and not tankidle:playing() then
tankdrive:stop()
tankidle:play()
end

if playerMove == "yes" and not tankdrive:playing() then
tankidle:stop()
tankdrive:play()
end

PSPKOR
October 15th, 2006, 17:17
Cheers i'll just try it.

---Edit---
No, it just goes off like it did before. Any other ideas?

yaustar
October 15th, 2006, 17:55
Okay.. In that case, I need to know what it actually does and what you want it to do in as much detail as possible. How are you trying to stop both sounds?

PSPKOR
October 15th, 2006, 18:01
Well its for a tank game that I'm making and I want it so that when the tanks moving it plays one sound and when its not moving it plays the other. But i also need it so it stops the other sound, or it sounds quite nasty. The sounds are also looped.

At the moment I have this:

if playerMove == "no" and loopCount == 0 then
local sound = tankidle
voice = sound:play()
loopCount2 = 0
loopCount = loopCount + 1
end

if voice:playing() and playerMove == "yes" then
voice:stop()
loopCount = 0
end

if playerMove == "yes" and loopCount2 == 0 then
local sound = tankdrive
noise = sound:play()
loopCount2 = loopCount2 + 1
end

This plays the stoped sound fine eg starts and stops at the right time, but the driving sound just keeps playing. If I try to add the STOP() command it crashes.

PSPKOR
October 15th, 2006, 18:35
That didn't work either.
But yeah thats what I want i want it to do.
"plays either the tankidle or tankdrive sound at one time"

yaustar
October 15th, 2006, 18:41
tankdriveVoice = tankdrive:play()
tankdriveVoice:stop()
tankidleVoice = tankidle:play()
tankidleVoice:stop()

while true do

if playerMove == "no" and not tankidle:playing() then
tankdriveVoice:stop()
tankidleVoice = tankidle:play()
end

if playerMove == "yes" and not tankdrive:playing() then
tankidleVoice :stop()
tankdriveVoice = tankdrive:play()
end
end
Completely untested.

PSPKOR
October 15th, 2006, 18:45
Dont look like thats working, I'll try and fiddle about with it a minute.

Do you have a PSP or Lua player on your PC?

Nevermind here you go have a look at the code attached, this is the whole code for it so far.

The sound stuff is under ------------drivin sounds------------.

yaustar
October 15th, 2006, 18:49
tankdriveVoice = tankdrive:play()
tankdriveVoice:stop()
tankidleVoice = tankidle:play()
tankidleVoice:stop()

while true do

if playerMove == "no" and not tankidleVoice:playing() then
tankdriveVoice:stop()
tankidleVoice = tankidle:play()
end

if playerMove == "yes" and not tankdriveVoice:playing() then
tankidleVoice :stop()
tankdriveVoice = tankdrive:play()
end
end
.

PSPKOR
October 15th, 2006, 18:57
Well it runs this time but it only plays the idle sound.

PS - look at my post above.

yaustar
October 15th, 2006, 19:46
Can you zip up the entire project with the sounds for me to look at?

PSPKOR
October 16th, 2006, 00:12
OK here you go, all there.
Also L + R are like the tracks, so hold L + R to go forwards.

yaustar
October 16th, 2006, 23:28
I had a decent shot at it and to be honest I can't get it to work even though the logic appears to be fine. I honestly think that the sound system of LuaPlayer is buggy as hell and system calls like playing sound, blitting to the screen are all on seperate threads which makes them very hard to use.

Sorry.


green=Color.new(0,255,0)
white = Color.new(255,255,255)
oldpad = Controls.read()
grass = Image.load("TankT/Grass.png")
block = Image.load("TankT/Building.png")
Bul = Image.load("TankT/Missile.png")
missile = Sound.load("TankT/Sounds/Missile.wav",false)
tankidle = Sound.load("TankT/Sounds/tankIdle.wav",true)
tankdrive = Sound.load("TankT/Sounds/tankDrive.wav",true)

tankdriveVoice = tankdrive:play()
tankidleVoice = tankidle:play()

screen.waitVblankStart(50)

tankdriveVoice:stop()
tankidleVoice:stop()

gamestate = "game"
animation = {}
animation[1] = Image.load("TankT/Tank/Tank0.png")
animation[2] = Image.load("TankT/Tank/Tank45.png")
animation[3] = Image.load("TankT/Tank/Tank90.png")
animation[4] = Image.load("TankT/Tank/Tank135.png")
animation[5] = Image.load("TankT/Tank/Tank180.png")
animation[6] = Image.load("TankT/Tank/Tank225.png")
animation[7] = Image.load("TankT/Tank/Tank270.png")
animation[8] = Image.load("TankT/Tank/Tank315.png")
animation[9] = Image.load("TankT/Tank/TankShoot.png")
animation[10] = Image.load("TankT/Tank/TankShoot2.png")

player = animation[1]

Player = { x = 30, y = 100 }

playerHeight = 26
playerWidth = 51
playerMove = "no"
Collision = "no"
acceleration = 0

loopCount = 0
loopCount2 = 0
turnCount = 1

Block = {}
Block[1] = { x = 20, y = 10, height = block:height(), width = block:width() }
Block[2] = { x = 350, y = 180, height = block:height(), width = block:width() }
Block[3] = { x = 210, y = 10, height = block:height(), width = block:width() }

Bullet = { f = "no", x = Player.x, y = Player.y, d = "r" }

---------------------Game loop-------------------------
function playgame()
if player == animation[1] or player == animation[5] then
playerHeight = 26
playerWidth = 51
end
if player == animation[3] or player == animation[7] then
playerHeight = 51
playerWidth = 26
end
if player == animation[2] or player == animation[4] or player == animation[6] or player == animation[8] then
playerHeight = 46
playerWidth = 46
end
PosToBlitX = Player.x - (playerWidth / 2 )
PosToBlitY = Player.y - (playerHeight / 2 )

pad = Controls.read()
screen:clear()
screen:blit(0,0,grass)
screen:blit(PosToBlitX, PosToBlitY, player)

for a = 1,3 do
screen:blit(Block[a].x,Block[a].y,block)
end
if Bullet.f == "yes" then
screen:blit(Bullet.x, Bullet.y, Bul)
end

------------------PlayerMove-------------------

if player == animation[1] and Collision == "no" then
Player.x = Player.x + acceleration
end
if player == animation[2] and Collision == "no" then
Player.x = Player.x + (acceleration / 2)
Player.y = Player.y + (acceleration / 2)
end
if player == animation[3] and Collision == "no" then
Player.y = Player.y + acceleration
end
if player == animation[4] and Collision == "no" then
Player.x = Player.x - (acceleration / 2)
Player.y = Player.y + (acceleration / 2)
end
if player == animation[5] and Collision == "no" then
Player.x = Player.x - acceleration
end
if player == animation[6] and Collision == "no" then
Player.x = Player.x - (acceleration / 2)
Player.y = Player.y - (acceleration / 2)
end
if player == animation[7] and Collision == "no" then
Player.y = Player.y - acceleration
end
if player == animation[8] and Collision == "no" then
Player.x = Player.x + (acceleration / 2)
Player.y = Player.y - (acceleration / 2)
end
if pad:r() and pad:l() then
playerMove = "yes"
else playerMove = "no"
end

if pad:r() and pad:r() ~= pad:l() then
turnCount = turnCount - 0.25
end
if pad:l() and pad:l() ~= pad:r() then
turnCount = turnCount + 0.25
end

--------------acceleration------------------

if pad:r() and pad:l() and Collision == "no" and acceleration <= 1.4 then
acceleration = acceleration + 0.01
end
if playerMove == "no" and acceleration >= 0.01 then
acceleration = acceleration - 0.01
end
if pad:square() and acceleration > 0 then
acceleration = acceleration - 0.05
end
if acceleration < 0 then
acceleration = 0
end

------------drivin sounds------------

if playerMove == "no" and tankidleVoice:playing() == false then
tankdriveVoice:stop()
tankidleVoice = tankidle:play()
screen:print( 1, 9, "IDLE", white )
end

if playerMove == "yes" and tankdriveVoice:playing() == false then
tankidleVoice:stop()
tankdriveVoice = tankdrive:play()
screen:print( 1, 9, "MOVING", white )
end

------------------playerOne stuff------------------------

if turnCount >= 0 and turnCount < 6 then player = animation[1]
elseif turnCount >= 6 and turnCount < 12 then player = animation[2]
elseif turnCount >= 12 and turnCount < 18 then player = animation[3]
elseif turnCount >= 18 and turnCount < 24 then player = animation[4]
elseif turnCount >= 24 and turnCount < 30 then player = animation[5]
elseif turnCount >= 30 and turnCount < 36 then player = animation[6]
elseif turnCount >= 36 and turnCount < 42 then player = animation[7]
elseif turnCount >= 42 and turnCount < 48 then player = animation[8]
end
if turnCount + 1 >= 48 then
turnCount = 1
end
if turnCount - 1 <= -1 then
turnCount = 47
end

--------------------shooting-------------------
if player == animation[1] and pad:cross() and oldpad:cross() ~= pad:cross() and Bullet.f == "no" then
Bullet.d = "r"
Bullet.x = Player.x + 25.5
Bullet.y = Player.y
Bullet.f = "yes"
player = animation[9]
acceleration = acceleration - 0.25
local sound = missile
voice = sound:play()
end

if player == animation[5] and pad:cross() and oldpad:cross() ~= pad:cross() and Bullet.f == "no" then
Bullet.d = "l"
Bullet.x = Player.x - 25.5
Bullet.y = Player.y
Bullet.f = "yes"
player = animation[10]
acceleration = acceleration - 0.25
local sound = missile
voice = sound:play()
end

if Bullet.f == "yes" and Bullet.d == "r" then
Bullet.x = Bullet.x + 2
end
if Bullet.f == "yes" and Bullet.d == "l" then
Bullet.x = Bullet.x - 2
end

if Bullet.x > 480 or Bullet.x < 0 or Bullet.y > 272 or Bullet.y < 0 then
Bullet.f = "no"
end

------------------Collision-----------------------

if playerMove == "yes" and (Player.x + playerWidth > Block[1].x) and (Player.x < Block[1].x + Block[1].width) and (Player.y + playerHeight > Block[1].y) and (Player.y < Block[1].y + Block[1].height) then
Collision = "yes"
else Collision = "no"
end

if playerMove == "yes" and (Player.x + playerWidth > Block[2].x) and (Player.x < Block[2].x + Block[2].width) and (Player.y + playerHeight > Block[2].y) and (Player.y < Block[2].y + Block[2].height) then
Collision = "yes"
end

if playerMove == "yes" and (Player.x + playerWidth > Block[3].x) and (Player.x < Block[3].x + Block[3].width) and (Player.y + playerHeight > Block[3].y) and (Player.y < Block[3].y + Block[3].height) then
Collision = "yes"
end
end

--------------------Main loop--------------------
while true do

if gamestate == "game" then
playgame()
end

screen.waitVblankStart()
screen.flip()
oldpad = pad
end

PSPKOR
October 17th, 2006, 00:03
OK, cheers anyway for your help. Not sure what to do now about SFX on the game though.

P.S- What do you think about the game(in early stages) so far.

-------------Edit----------------

Yeah!! I did it I got it to work now, its not the nicest of way to do it, but it works none the less.

thanks again Yaustar :).

In case you want to know this is how i did it -

--------Variable--------
soundCount = 0

--------Main loop--------
if gamestate == "game" and soundCount == 0 then
local sound = tankidle
voice = sound:play()
end
if gamestate == "game" and soundCount == 0 then
local sound = tankdrive
voice2 = sound:play()
end

if playerMove == "no" then
voice:volume(128)
voice2:volume(0)
soundCount = soundCount + 1
end
if playerMove == "yes" then
voice:volume(0)
voice2:volume(128)
end

yaustar
October 17th, 2006, 11:30
Impressive hack considering what you had to work with ;).