A Flash Developer Resource Site

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    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();
    fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);
    function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void
    {
    	accelX = event.accelerationX;
    	accelY = event.accelerationY;
    }
    
    ball.addEventListener(Event.ENTER_FRAME, moveBall);
    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. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    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. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center