(Basic) Play sound when accelerometer hits certain coords
A Flash Developer Resource Site

Thread: (Basic) Play sound when accelerometer hits certain coords

1. (Basic) Play sound when accelerometer hits certain coords

Hello everyone,

So I'm working on a university project where we're making innovative musical instruments. My idea is to have a device that you would attach to any playground toy (seesaw, swing etc.) and it would play a sound on each swing.

So after playing around with Flash, I found this preset cellphone accelerometer app, which is perfect for my project. It just misses the play sound component. Which brings me to my questions.

So the code for the app goes as follows:

Code:
import flash.events.Event;

var accelX:Number;
var accelY:Number;

var fl_Accelerometer:Accelerometer = new Accelerometer();
function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void
{
accelX = event.accelerationX;
accelY = event.accelerationY;
}

function moveBall(evt:Event){
ball.x -= accelX*30;
ball.y += accelY*30;

if(ball.x > (480-ball.width/2)){
ball.x = 480-ball.width/2;
}
if(ball.x < (0+ball.width/2)){
ball.x = 0+ball.width/2;
}
if(ball.y > (800-ball.width/2)){
ball.y = 800-ball.width/2;
}
if(ball.y < (0+ball.width/2)){
ball.y = 0+ball.width/2;
}
}
I changed the form of "ball" to a long line. I want it to play a sound each time that line hits the middle of the screen. So we're only using the x-coordinates (I think).

I figured a simple if-function should do the trick. Which I think would look something like this:

Code:
if(ball.x = 0)
{
sound.play();
}
So I have several question:
- Is that code correct?
- Where do I put the code?
- Is it possible to have a delay? So it doesn't spam-play the sound when I put the phone flat on the table where the "ball" is constantly hitting that middle line.

I'd also like to be able to change the sound. But that's optional, I'll skip it if it's too complicated.

I'd appreciate it if anyone would wanna help me.

2. Here's a good tutorial on sounds: http://gotoandlearn.com/play.php?id=61

Sounds work pretty easily. It's when you're trying to sync sounds (like two songs at the same beat), it can get complicated because there's a little cpu pause when starting to play a sound.

3. Read Urdu Stories, Hindi Stories, Pakistani Stories, Indian Stories onlinehttp://www.stories.pk

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap