468,507 Members | 1,511 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

(Mobile) 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:

Expand|Select|Wrap|Line Numbers
  1. import flash.events.Event;
  3. var accelX:Number;
  4. var accelY:Number;
  6. var fl_Accelerometer:Accelerometer = new Accelerometer();
  7. fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);
  8. function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void
  9. {
  10.     accelX = event.accelerationX;
  11.     accelY = event.accelerationY;
  12. }
  14. ball.addEventListener(Event.ENTER_FRAME, moveBall);
  15. function moveBall(evt:Event){
  16.     ball.x -= accelX*30;
  17.     ball.y += accelY*30;
  19.     if(ball.x > (480-ball.width/2)){
  20.         ball.x = 480-ball.width/2;
  21.     }
  22.     if(ball.x < (0+ball.width/2)){
  23.         ball.x = 0+ball.width/2;
  24.     }
  25.     if(ball.y > (800-ball.width/2)){
  26.        ball.y = 800-ball.width/2;
  27.     }
  28.     if(ball.y < (0+ball.width/2)){
  29.         ball.y = 0+ball.width/2;
  30.     }
  31. }
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:

Expand|Select|Wrap|Line Numbers
  1. if(ball.x = 0)
  2. {
  3. sound.play();
  4. }
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.
Dec 4 '13 #1
0 4824

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Mike Minor | last post: by
2 posts views Thread by Alex K. | last post: by
1 post views Thread by Lam | last post: by
10 posts views Thread by CM | last post: by
16 posts views Thread by Chris Ianson | last post: by
3 posts views Thread by illmagination | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.