Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-11-2012, 12:46 PM   #1
Registered User
Join Date: Mar 2012
Posts: 2
Default Help with Event Handling

So I am new at flash I won't deny it. Anyways I found how to do what I almost needed to do with a certain code. I'm using a sound spectrum as the background of a flash website. The Only Way You can start and stop the script running though is a play and stop button on stage. I can not figure out how to get both the audio and the spectrum started as soon as it loads.

Here is the code I was using which is in a class.as

ActionScript Code:
package {     import flash.display.Sprite;     import flash.media.Sound;     import flash.utils.ByteArray;     import flash.events.Event;     import flash.media.SoundMixer;     import flash.filters.GlowFilter;     import flash.net.URLRequest;     import flash.events.MouseEvent;     import flash.media.SoundChannel;       public class Main extends Sprite{         private var sound:Sound;         private var channel:SoundChannel;         private var byteArr:ByteArray = new ByteArray();         private var glow:GlowFilter = new GlowFilter();         private var filterArr:Array;         private var line:Sprite = new Sprite();         private var num:Number;         public const GRAFT_HEIGHT:int = 150; //set animation height         public const CHANNEL_SIZE:int = 256; //set left/right channel size           public function Main(){             glow.color = 0x009900;             glow.alpha = 1;             glow.blurX = 10;             glow.blurY = 10;               sound = new Sound(new URLRequest("sound.mp3"));               filterArr = new Array(glow); //add glow to the animation             line.filters = filterArr;             addChild(line);               play_btn.addEventListener(MouseEvent.CLICK,onPlayHandler);             stop_btn.addEventListener(MouseEvent.CLICK,onStopHandler);         }           private function onPlayHandler(event:MouseEvent):void{             channel = sound.play(0,1000); // play sound 1000 times             addEventListener(Event.ENTER_FRAME,spectrumHandler);         }           private function onStopHandler(event:MouseEvent):void{             channel.stop();             line.graphics.clear();             removeEventListener(Event.ENTER_FRAME,spectrumHandler);         }           private function spectrumHandler(event:Event):void{             num = 0;               line.graphics.clear(); // create current graphics             line.graphics.lineStyle(0, 0x00FF00);             line.graphics.beginFill(0x00FF00,0.5);             line.graphics.moveTo(0,GRAFT_HEIGHT);               SoundMixer.computeSpectrum(byteArr);// add bytes to Sound mixer               for (var i:int = 0; i < CHANNEL_SIZE; i++) {                         num = (byteArr.readFloat() * GRAFT_HEIGHT);                         line.graphics.lineTo(i * 2, GRAFT_HEIGHT - num);                     }               line.graphics.lineTo(CHANNEL_SIZE * 2, GRAFT_HEIGHT);             line.graphics.endFill();               line.graphics.lineStyle(0, 0xFF0000);             line.graphics.beginFill(0xFF0000, 0.5);             line.graphics.moveTo(CHANNEL_SIZE * 2,GRAFT_HEIGHT);               for (i = CHANNEL_SIZE; i > 0; i--) {                 num = (byteArr.readFloat() * GRAFT_HEIGHT);                 line.graphics.lineTo(i * 2, GRAFT_HEIGHT - num);             }               line.graphics.lineTo(0, GRAFT_HEIGHT);             line.graphics.endFill();         }     } }

Thank You in advance any help would be great.
neosa is offline   Reply With Quote
Old 03-11-2012, 05:51 PM   #2
Registered User
Join Date: Mar 2012
Posts: 2

Anyone know how to do this?

How bout this. Is there a definition I can use to start the spectrumHandler as soon as the song is loaded and started?
neosa is offline   Reply With Quote
Old 03-13-2012, 06:39 AM   #3
Site Contributor
Mazoonist's Avatar
Join Date: Jun 2006
Posts: 3,174
Send a message via AIM to Mazoonist

At the end of the constructor function, add in this line, which will manually call the function that is normally triggered by the mouse clicking the play button:


EDIT: By constructor, I mean the function called Main. Add in that line as the last line of that function.
My Tutorials * My Website

Last edited by Mazoonist; 03-13-2012 at 06:41 AM.
Mazoonist is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

All times are GMT. The time now is 07:19 AM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.