adding a sound to "timeanimation" demo

Hello to everybody, I’m new to Corona so I’m sorry for my simple questions. I’ve created a page for an interactive book where I added the code from the example “TimeAnimation”, it’s a ball that can be dragged and collides with screen borders, very simple, everything works fine.
Now I want to add a sound every time the ball collides. I modified the “onMoveCircle” function adding an audio.play for every condition, it works and sounds are played, but they are emitted also when the ball is not moving and it’s still on the ground. How can I set the audio.play so that it works only when the ball is moving ? Here is the code, anyone can help me ? Thank you

[lua]function onMoveCircle(event)
local timePassed = event.time - lastTime
lastTime = lastTime + timePassed

speedY = speedY + gravity

ball.x = ball.x + speedX*timePassed
ball.y = ball.y + speedY*timePassed

if ball.x >= screenW - ball.width*.5 then
ball.x = screenW - ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction

audio.play( pop, {channel=myChannel} )

elseif ball.x <= ball.width*.5 then
ball.x = ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction

audio.play( pop, {channel=myChannel} )

elseif ball.y >= screenH - ball.height*.5 then
ball.y = screenH - ball.height*.5
speedY = speedY*friction
speedX = speedX*friction
speedY = speedY*-1 --change direction

audio.play( pop, {channel=myChannel} )

elseif ball.y <= ball.height*.5 then
ball.y = ball.height*.5
speedY = speedY*friction
speedY = speedY*-1 --change direction

audio.play( pop, {channel=myChannel} )

end
end[/lua]
[import]uid: 106680 topic_id: 19303 reply_id: 319303[/import]

You’d want to have a collision listener which played the sound - take a look at Corona For Newbies Part 4 (it’s on Techority) and try that.

Peach :slight_smile: [import]uid: 52491 topic_id: 19303 reply_id: 74492[/import]

Hello Peach,
thank you for your reply and compliments for your work, I will read the Corona for newbies for sure :slight_smile:

In the page I’m building I think there are no collisions, the function just tells the ball to invert the speed when it arrives at the end of the screen, is it possible to add a listener in this case ?

Thank you

Ray [import]uid: 106680 topic_id: 19303 reply_id: 74517[/import]

Hey, sorry, I was a little rushed earlier - I should have welcomed you - welcome!

I’ve added < lua > tags to your code to make it easier to read :slight_smile:

Also, don’t feel bad for what you consider “simple questions” - asking is how you learn. Plus the info will help others in the future.

For your problem regarding the sounds playing when the ball is on the ground and not moving, perhaps you could check that the speedX and speedY are greater or less than a certain number?

If you could provide more code or upload a sample I can run I could likely give you more specific info on that if needed.

Peach :slight_smile: [import]uid: 52491 topic_id: 19303 reply_id: 74533[/import]

Hello Peach,
yes I thought about using speedX and speedY and I tried but without good results, because I don’t know where to put this condition, is it better to use the same function ‘onMoveCircle’ or to create another one ?

Here is the link to the complete page, the function is at line 410:

http://dl.dropbox.com/u/10259282/page_10.rar

Thank you :slight_smile:

Ray [import]uid: 106680 topic_id: 19303 reply_id: 74542[/import]

Hey Ray,

I can’t do anything with that page as I don’t have your sound files, etc. The purpose of uploading is so people can run it and assist with the issue.

If you’d prefer not to upload the project you could provide plug and play code and I (or others) could use a sound file they already have for testing purposes.

Peach :slight_smile: [import]uid: 52491 topic_id: 19303 reply_id: 74613[/import]

Sorry Peach,
you’re right, here is the link to a complete project:
http://dl.dropbox.com/u/10259282/ball_animation.rar

Thank you

Ray
[import]uid: 106680 topic_id: 19303 reply_id: 74633[/import]

Try adding this in;

[lua]if speedY < -0.2 then
audio.play( boing_audio, {channel=myChannel} )
end[/lua]

At line 57 in main.lua.

It’s a bit rough but should be a good starting point.

Let me know how that goes.

Peach :slight_smile: [import]uid: 52491 topic_id: 19303 reply_id: 74649[/import]

Peach, you’re great, it works !
I did the same but with speedY < 0 and it didn’t work, why did you use 0.2 ?

Thank you so much

Ray [import]uid: 106680 topic_id: 19303 reply_id: 74662[/import]

I did -0.2 because that would suggest the ball is moving downward at speed, rather than < 0 which could fire too easily.

Play with the numbers bit. (Print speedY - you’ll see it isn’t 0 always even if the ball appears still.)

Peach :slight_smile: [import]uid: 52491 topic_id: 19303 reply_id: 74841[/import]