AS3 Code Library and Sample Files
//Exploring circular movement
View .swf file: circularMotion.swf | Download .fla file: circularMotion.fla (right click and "Save Link As")
- //Exploring circular movement
- var myClip:MovieClip = new Clip(); //initialize clip from library
- addChild(myClip); //add it to the stage
- var angle:Number = 0; //configure variables
- var centerX:Number = stage.stageWidth/2;
- var centerY:Number = stage.stageHeight/2;
- var radiusX:Number = 100;
- var radiusY:Number = 100;
- var speed:Number = .1;
- addEventListener(Event.ENTER_FRAME, onLoop);
- function onLoop(e:Event) {
- //by changing the angle and using Math.sin for x and Math.cos for y, you can move in a circle
- myClip.x = centerX + Math.sin(angle) * radiusX;
- myClip.y = centerY + Math.cos(angle) * radiusY;
- angle += speed;
- //move mouse to change radius
- radiusX = Math.abs(mouseX - stage.stageWidth/2);
- radiusY = Math.abs(mouseY - stage.stageHeight/2);
- }
//Timer Event that counts down from 10 seconds
View .swf file: TimerEvents_CountDown.swf | Download .fla file: TimerEvents_CountDown.fla (right click and "Save Link As")
- //Timer Event that counts down from 10 seconds
- //Timer requires delay and repeat count
- //1000 = 1000 miliseconds, 10 = number of times to count
- var myTimer:Timer = new Timer(1000, 10);
- //tell the timer when to start
- myTimer.start();
- var myCount:int = 10;
- //listen for when the timer fires
- myTimer.addEventListener(TimerEvent.TIMER, onTimer);
- function onTimer(e:TimerEvent) {
- myCount--;
- //trace(myCount);
- time_txt.text = String(myCount);
- time_txt.x += 20;
- }
- myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onDone);
- function onDone(e:TimerEvent) {
- time_txt.text = "Blast Off";
- }
//Keyboard Events and Input
View .swf file: KeyboardEvents.swf | Download .fla file: KeyboardEvents.fla (right click and "Save Link As")
- //Keyboard Events and Input
- stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
- //stage.addEventListener(KeyboardEvent.KEY_UP, upKey);
- function downKey(e:KeyboardEvent) {
- trace("keycode: " + e.keyCode); //which key, A and a are on the same key
- trace("charcode: " + e.charCode); //which character, A and a are different
- trace (String.fromCharCode(e.charCode));
- trace("ctrlKey: " + e.ctrlKey);
- trace("keyLocation: " + e.keyLocation);
- trace("shiftKey: " + e.shiftKey);
- trace("altKey: " + e.altKey);
- switch (e.keyCode) {
- case Keyboard.UP:
- //case 38:
- box_mc.y -= 10;
- break;
- case Keyboard.DOWN:
- //case 40:
- box_mc.y += 10;
- break;
- case Keyboard.LEFT:
- //case 37:
- box_mc.x -= 10;
- break;
- case Keyboard.RIGHT:
- //case 39:
- box_mc.x += 10;
- break;
- case Keyboard.SPACE:
- trace ("space");
- break;
- }
- // IF METHOD
- // if (e.keyCode == 38) {
- // trace("up");
- // box_mc.y -= 10;
- // //box_mc.y = box_mc.y - 10;
- // } else if (e.keyCode == 40) {
- // box_mc.y += 10;
- // } else if (e.keyCode == 37) {
- // box_mc.x -= 10;
- // } else if (e.keyCode == 39) {
- // box_mc.x += 10;
- // }
- }
//Scrolling thumbnails within a bounding area, programmatic animation based on mouse position
View .swf file: scrolling_thumbs.swf | Download .fla file: scrolling_thumbs.fla (right click and "Save Link As")
- //Scrolling thumbnails within a bounding area, programmatic animation based on mouse position
- var xdistance:Number;
- var scrollspeed:Number = 8; //lower numbers equal faster speed
- panel_mc.addEventListener(MouseEvent.ROLL_OVER, onOver);
- function onOver(e:MouseEvent)
- {
- panel_mc.addEventListener(Event.ENTER_FRAME, onEnter);
- //establish enterframe loop and remove over event
- panel_mc.removeEventListener(MouseEvent.ROLL_OVER, onOver);
- function onEnter(e:Event)
- {
- //move panel_mc based on distance from center
- xdistance = mouseX - 250;
- panel_mc.x += Math.round(-xdistance/scrollspeed);
- //check if moving too far and set limits
- if (panel_mc.x >= 133) {
- panel_mc.x = 133;
- } else if (panel_mc.x <= -106) {
- panel_mc.x = -106;
- }
- //alternative to a rollout, check if outside of bounding area and if so remove enterframe loop
- if (mouseX<stroke_mc.x || mouseX>stroke_mc.width + stroke_mc.x || mouseY<stroke_mc.y || mouseY>stroke_mc.height + stroke_mc.y) {
- panel_mc.removeEventListener(Event.ENTER_FRAME, onEnter);
- panel_mc.addEventListener(MouseEvent.ROLL_OVER, onOver);
- }
- }
- }
- panel_mc.addEventListener(MouseEvent.CLICK, onClick);
- function onClick(e:MouseEvent) {
- trace(e.target.name);
- if (e.target.name == "b1") {
- trace ("Do whatever button 1 is supposed to do");
- }
- }
//Sound - Loading and Basic Controls
View .swf file: soundbasics.swf | Download .fla file: soundbasics.fla (right click and "Save Link As")
- //Scripting Sound
- //Loading an external mp3
- var mySound:Sound = new Sound();
- mySound.load(new URLRequest("fire.mp3"));
- mySound.play();
- ////The above can be shortened to 2 lines of code like this:
- var mySound2:Sound = new Sound(new URLRequest("whoosh.mp3"));
- mySound2.play();
- //Loading an internal sound
- var iSound:Sound = new Clang();
- iSound.play();
- ////Loading the full sound file or buffering a sound before playing
- var myMusic:Sound = new Sound();
- myMusic.load(new URLRequest("track1.mp3"));
- myMusic.addEventListener(Event.COMPLETE, onLoaded);
- function onLoaded(e:Event) {
- myMusic.play();
- }
- //Buffer a sound using the SoundLoaderContext class
- //Pass miliseconds to SoundLoaderContext parameters, so 100000 is 10 seconds of sound to load before playing
- var myContext:SoundLoaderContext = new SoundLoaderContext(10000);
- var myMusic2:Sound = new Sound();
- myMusic2.load(new URLRequest("track1.mp3"), myContext);
- myMusic2.play();
- //Controlling Sound
- var myMusic:Sound = new Sound();
- var myChannel:SoundChannel = new SoundChannel();
- var myTrans:SoundTransform = new SoundTransform();
- myMusic.load(new URLRequest("track1.mp3"));
- myChannel = myMusic.play();
- //the play method can take two parameters -- the offset (in miliseconds) and # of times to loop... so myMusic.play(1000, 3) would offset by 1 second and loop 3 times
- var pausePos:Number = 0; //keep track of position
- var musicPlaying:Boolean = true; //keep track of if playing or not
- stop_btn.addEventListener(MouseEvent.CLICK, onClick);
- play_btn.addEventListener(MouseEvent.CLICK, onClick);
- pause_btn.addEventListener(MouseEvent.CLICK, onClick);
- volDown_btn.addEventListener(MouseEvent.CLICK, onClick);
- volUp_btn.addEventListener(MouseEvent.CLICK, onClick);
- panRight_btn.addEventListener(MouseEvent.CLICK, onClick);
- panLeft_btn.addEventListener(MouseEvent.CLICK, onClick);
- function onClick(e:MouseEvent) {
- switch (e.target) {
- case stop_btn: //stop sound
- myChannel.stop();
- pausePos = 0;
- musicPlaying = false;
- break;
- case play_btn: //play sound only if not already playing
- if (!musicPlaying) {
- myChannel = myMusic.play(pausePos);
- musicPlaying = true;
- }
- break;
- case pause_btn: //pause sound by tracking position
- pausePos = myChannel.position;
- myChannel.stop();
- musicPlaying = false;
- break;
- case volDown_btn: //volume
- myTrans.volume -= .1;
- if (myTrans.volume < 0) {
- myTrans.volume = 0;
- }
- myChannel.soundTransform = myTrans;
- break;
- case volUp_btn:
- myTrans.volume += .1;
- myChannel.soundTransform = myTrans;
- break;
- case panRight_btn: //panning
- myTrans.pan = 1;
- myChannel.soundTransform = myTrans;
- break;
- case panLeft_btn:
- myTrans.pan = - 1;
- myChannel.soundTransform = myTrans;
- break;
- }
- }
//Sound - Advanced Controls, Sliders, Multi-Track, Keeping Track of Position/Time, Visualization
View .swf file: soundcontrols.swf | Download .fla file: soundcontrols.fla (right click and "Save Link As")
- //Sound - Advanced Controls, Sliders, Multi-Track, Keeping Track of Position/Time, Visualization
- var mySound:Sound = new Sound(); //create Sound object and assign in the variable name mySound
- var myChannel:SoundChannel = new SoundChannel(); //create SoundChannel, needed for controlling sound
- var mySoundTransform:SoundTransform = new SoundTransform(); //create SoundTransform needed for volume and panning
- var myFile:URLRequest = new URLRequest("track1.mp3"); //myFile is variable for the URLRequest
- mySound.load(myFile); //loads the file/request into the sound object
- var myPosition:Number = 0; //variable to track position of sound
- var soundPlaying:Boolean = false; //variable to track if sound is playing or not
- var skipSpeed:Number = 500; //variable for ff and rewind speed
- //Add event listeners to all button
- stop_btn.addEventListener(MouseEvent.CLICK, onClick);
- play_btn.addEventListener(MouseEvent.CLICK, onClick);
- pause_btn.addEventListener(MouseEvent.CLICK, onClick);
- track1_btn.addEventListener(MouseEvent.CLICK, onClick);
- track2_btn.addEventListener(MouseEvent.CLICK, onClick);
- //For rewind and ff use MOUSE_DOWN and MOUSE_UP so that you can hold the buttons down for continous effect
- rewind_btn.addEventListener(MouseEvent.MOUSE_DOWN, onSkipDown);
- ff_btn.addEventListener(MouseEvent.MOUSE_DOWN, onSkipDown);
- rewind_btn.addEventListener(MouseEvent.MOUSE_UP, onSkipUp);
- ff_btn.addEventListener(MouseEvent.MOUSE_UP, onSkipUp);
- function onClick(e:MouseEvent) {
- //With the switch conditional and the event target(e.target) we can use one oClick function for each of these buttons
- switch (e.target) {
- case play_btn: //if the target is the play_btn do this
- if (!soundPlaying) { //check if the sound is playing or not and if not do this
- myChannel = mySound.play(myPosition); //play wit offset of myPosition var
- myChannel.soundTransform = mySoundTransform; //apply any sound transformations too
- soundPlaying = true; // now its true that the sound is playing
- }
- break; // use the break statement to end each case
- case stop_btn:
- myChannel.stop(); //stop the sound in the sound channel
- soundPlaying = false;
- myPosition = 0; //reset the position var to 0 (rewind it)
- break;
- case pause_btn:
- myPosition = myChannel.position; //reset position var to current channel position to keep track of it
- myChannel.stop();
- soundPlaying = false
- break;
- case track1_btn: //change to track 1 -- stop the sound, say the sound is going to be new, and load/play it
- myChannel.stop();
- mySound = new Sound();
- myFile = new URLRequest("track1.mp3");
- mySound.load(myFile);
- myPosition = 0;
- myChannel = mySound.play(myPosition);
- myChannel.soundTransform = mySoundTransform;
- soundPlaying = true;
- break;
- case track2_btn: //change to track 2
- myChannel.stop();
- mySound = new Sound();
- myFile = new URLRequest("track2.mp3");
- mySound.load(myFile);
- myPosition = 0;
- myChannel = mySound.play(myPosition);
- myChannel.soundTransform = mySoundTransform;
- soundPlaying = true;
- break;
- }
- }
- //Rewind and FF functions
- function onSkipDown(e:MouseEvent) {
- switch (e.target) {
- case rewind_btn: //use enterframe event to continously rewind while down
- rewind_btn.addEventListener(Event.ENTER_FRAME, onRewind);
- break;
- case ff_btn: //use enterframe to continuously ff while down
- ff_btn.addEventListener(Event.ENTER_FRAME, onFF);
- break;
- }
- }
- function onSkipUp(e:MouseEvent) {
- skipSpeed = 500; //reset speed var
- switch (e.target) {
- case rewind_btn: //remove continous enterframe on up
- rewind_btn.removeEventListener(Event.ENTER_FRAME, onRewind);
- break;
- case ff_btn: //remove continous enterframe on up
- ff_btn.removeEventListener(Event.ENTER_FRAME, onFF);
- break;
- }
- }
- function onRewind(evt:Event) { //enterframe function to continously rewind
- myPosition = myChannel.position - skipSpeed; //subtract speed from current position
- skipSpeed +=100; //increase speed so rewind goes faster the longer you hold the buton down
- myChannel.stop(); //stop the sound
- myChannel = mySound.play(myPosition); //and replay from new position
- myChannel.soundTransform = mySoundTransform; //maintaining all transformations on the sound
- soundPlaying = true
- }
- function onFF(evt:Event) { //enterframe function to continously ff, similar to above for rewind
- myPosition = myChannel.position + skipSpeed;
- skipSpeed +=100;
- myChannel.stop();
- myChannel = mySound.play(myPosition);
- myChannel.soundTransform = mySoundTransform;
- soundPlaying = true
- }
- //VOLUME CONTROL - SLIDER BAR
- //Note the bar is 100 pixels in height which will allow for an easy conversion of 0 to 100% sound volume
- var constrain:Rectangle = new Rectangle(0, 0, 0, 100); //used to constrain the dragging of the slider
- var mySetting:int = 100 - bar_mc.vslide_mc.y; //The mySetting var will be used to determine volume setting, its based on y position of slider
- mySoundTransform.volume = mySetting/100; //divide transform by 100 sound that is within the range of 0 to 1 instead of 0 to 100
- bar_mc.vslide_mc.buttonMode = true; //gives you the hand cursor when over vslide_mc clip/button
- bar_mc.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
- function onDown(e:MouseEvent) {
- bar_mc.vslide_mc.startDrag(true ,constrain);//drag on down, set to the constraint so you can't drag outside of the bar
- }
- //bar_mc.addEventListener(MouseEvent.MOUSE_UP,onUp);
- stage.addEventListener(MouseEvent.MOUSE_UP,onUp); //call also outside of bar_mc, otherwise you'll keep dragging after rollout
- function onUp(e:MouseEvent) {
- bar_mc.vslide_mc.stopDrag(); //stop dragging the slider
- mySetting = 100 - bar_mc.vslide_mc.y; //reset the var based on slider y position
- trace(mySetting)
- mySoundTransform.volume = mySetting/100; //adjust sound volume based on new setting
- myChannel.soundTransform = mySoundTransform;
- }
- //PANNING CONROL SLIDER BAR
- //Panning works similar to volume slider, but with a wider bar of 200 pixels
- var myPanSetting:int = 100 - panbar_mc.vslide_mc.y;
- mySoundTransform.pan = myPanSetting/100;
- var constrain2:Rectangle = new Rectangle(0, 0, 0, 200);
- panbar_mc.vslide_mc.buttonMode = true;
- panbar_mc.addEventListener(MouseEvent.MOUSE_DOWN, onPanDown);
- function onPanDown(e:MouseEvent) {
- panbar_mc.vslide_mc.startDrag(true ,constrain2);
- }
- panbar_mc.addEventListener(MouseEvent.MOUSE_UP,onPanUp);
- stage.addEventListener(MouseEvent.MOUSE_UP,onPanUp);
- function onPanUp(e:MouseEvent) {
- panbar_mc.vslide_mc.stopDrag();
- myPanSetting = 100 - panbar_mc.vslide_mc.y;
- trace(myPanSetting)
- mySoundTransform.pan = myPanSetting/100;
- myChannel.soundTransform = mySoundTransform;
- }
- //KEEP TIME - Track current time and length of sound into text field
- stage.addEventListener(Event.ENTER_FRAME, keepTime); //keep time updated every frame
- function keepTime(e:Event) {
- // position_txt.text = String(int(myChannel.position/1000)); //this would work but is in total seconds rather than 0:00 format
- //variables for minutes, tens of seconds, and seconds so you can translate to 0:00 format from miliseconds
- //first calculate current position
- var minutes:Number = Math.floor(myChannel.position/60000); //minutes by dividing position by 60 seconds
- var seconds10:Number = Math.floor(((myChannel.position/1000) % 60)/10); //to get tens of seconds value use modulo (%) for remainder
- //modulo (%) returns remainder after division... so 10 % 6 would return 4 for example, whereas 10 % 5 returns 0 (no remainder)
- var seconds:Number = Math.floor(((myChannel.position/1000) % 60)%10); //gets seconds from 0 - 9
- //second calculate overall sound length
- var lminutes:Number = Math.floor(mySound.length/60000);
- var lseconds10:Number = Math.floor(((mySound.length/1000) % 60)/10);
- var lseconds:Number = Math.floor(((mySound.length/1000) % 60)%10);
- //third put all these variables into the text string for current position/song length
- position_txt.text = String(minutes) + ":" + String(seconds10) +""+ String(seconds) + " | " + String(lminutes) + ":" + String(lseconds10) +""+ String(lseconds)
- //keeping track of time has the added benefit of showing load progress as the sound length will continue to increase until sound fully loaded
- //SOUND VISUALIZATION -- also running on the keepTime EnterFrame event
- //rightPeak and leftPeak properties of the Sound Channel are easy ways to get amplitude info
- //Here the peak data manipulates the mask named peakbar_mc to reveal bar
- lpeakmeter_mc.peakbar_mc.height = myChannel.leftPeak * 200; //multiply by 200 to translate 0 to 1 into 0 to 200
- rpeakmeter_mc.peakbar_mc.height = myChannel.rightPeak * 200;
- //Compute Spectrum -- right click on computeSpectrum method to see flash help info upon which this is based
- var bytes:ByteArray = new ByteArray();
- //const PLOT_HEIGHT:int = 200;
- const PLOT_HEIGHT:int = 398;
- const CHANNEL_LENGTH:int = 256;
- //SoundMixer.computeSpectrum(bytes, false, 0);
- SoundMixer.computeSpectrum(bytes,true,0); //compute the spectrum and put in byte array
- var g:Graphics = this.graphics; // will make graphics based on spectrum
- g.clear(); //clear any current graphics
- //left channel graphics
- g.lineStyle(0, 0x000000, .5); //dictates line thickness, color, alpha)
- g.beginFill(0x000000, .3); //fill color, alpha
- g.moveTo(0, PLOT_HEIGHT); //set initial position of line
- var n:Number = 0;
- for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
- n = (bytes.readFloat() * PLOT_HEIGHT);
- g.lineTo(i * 2.15, PLOT_HEIGHT - n); //draw line for all data in computeSpectrum byte array left channel (2.15 is the factor that will allow the graphics to extend all the way accross the stage (256 * 2.15 = 550, our stage with)
- }
- g.lineTo(CHANNEL_LENGTH * 2.15, PLOT_HEIGHT);
- g.endFill();
- //right channel graphics
- g.lineStyle(0, 0xFFFFFF, .5);
- g.beginFill(0xFFFFFF, 0.3);
- g.moveTo(CHANNEL_LENGTH * 2.15, PLOT_HEIGHT);
- for (i = CHANNEL_LENGTH; i > 0; i--) {
- n = (bytes.readFloat() * PLOT_HEIGHT);
- g.lineTo(i * 2.15, PLOT_HEIGHT - n);
- }
- g.lineTo(0, PLOT_HEIGHT);
- g.endFill();
- }
//Distance based collision detection
View .swf file: distance-based-collision-detection..swf | Download .fla file: distance-based-collision-detection.fla (right click and "Save Link As")
- //distance based collision detection
- mc1.startDrag(true);
- addEventListener(Event.ENTER_FRAME, onEnterFrame);
- function onEnterFrame(e:Event) {
- //determine the distance with the Pythagorean Theorem
- var dx:Number = mc2.x - mc1.x;
- var dy:Number = mc2.y - mc1.y;
- var distance:Number = Math.sqrt(dx * dx + dy * dy);
- //put distance into dynamic text field
- thedistance.text = Math.ceil(distance);
- //use distance to determine collision - I added 2 to accomodate the stroke
- if (distance < 62) {
- mc2.play();
- }
- //or use the movieclip widths rather than a number
- if (distance < mc1.width/2 + mc2.width/2) {
- thedistance.text = "HIT " + Math.ceil(distance);
- }
- }
//The Display List - Adding, Removing, and Reparenting Display Objects
View .swf file: DisplayList-Reparenting.swf | Download .fla file: DisplayList-Reparenting.fla (right click and "Save Link As")
- //The Display List, Adding, Removing, and Reparenting Display Objects
- //By Karl Cleveland - 2009
- //Calling the addChild() method a second time can move the display object to a different parent
- //Doing so also deletes the object from the orignal/previous parent without the need to remove it using removeChild first
- //Thus, you can easily move clips/content from one clip to another
- //To add object from library, it must be exported for Actionscript.
- var myPhoto = new Pic(); //Pic is the Class name of the movieclip in the library that was exported for AS
- //move the photo away from the top left 0, 0 corner, so it centers in parent containers once added
- myPhoto.x = 10;
- myPhoto.y = 10;
- //use addChild to add the Display list, in this case into the movieclip called "parent_mc"
- parent1_mc.addChild(myPhoto);
- //Toggle the parent of myPhoto
- move_btn.addEventListener(MouseEvent.CLICK, onClick);
- function onClick(e:MouseEvent) {
- //numChildren is a read-only property, returning the value of the number of display objects in the display object container
- //if > 1 checks if parent1_mc has more than just the background shape/graphic in it, but the photo too
- if (parent1_mc.numChildren > 1) {
- //if so, reparent the photo to parent2_mc
- parent2_mc.addChild(myPhoto);
- } else {
- parent1_mc.addChild(myPhoto);
- }
- }
- //removeChild can be used to remove the Photo... but doesn't delete it from memory
- myPhoto.addEventListener(MouseEvent.CLICK, onMyPhotoClick);
- function onMyPhotoClick(e:MouseEvent) {
- myPhoto.parent.removeChild(myPhoto);
- //myPhoto = null; //This would delete from memory, uncomment to test
- }
- //To cycle through and remove all objects in the display list, use the while loop
- //Hit the Space Bar to test/activate
- stage.addEventListener(KeyboardEvent.KEY_DOWN, onSpace);
- function onSpace(e:KeyboardEvent) {
- if (e.keyCode == Keyboard.SPACE) {
- //this removes all children from the root/main timeline dispaly list
- //but doesn't remove the listeners or delete the objects from memory -- which would be required for true/full removal
- while (numChildren > 0) {
- trace (numChildren);
- removeChildAt(0);
- }
- //For Resource Management/Garbage Collection you can aslo remove all object referenced and listeners like so
- move_btn.removeEventListener(MouseEvent.CLICK, onClick);
- myPhoto.removeEventListener(MouseEvent.CLICK, onMyPhotoClick);
- stage.removeEventListener(KeyboardEvent.KEY_DOWN, onSpace);
- myPhoto = null;
- parent1_mc = null;
- parent2_mc = null;
- move_btn = null;
- trace (numChildren + " - Everything removed");
- }
- }
//Timeline Scroller
View .swf file: Timeline_Scroller.swf | Download .fla file: Timeline_Scroller.fla (right click and "Save Link As")
- //This demo explores scrolling the timeline of a movieclip (called pics_mc) using buttons and a custom scrollbar. The coding of the buttons is below. The coding for the scrollbar at the bottom below.
- //By Karl Cleveland - 2009
- var currentf:int; //current frame var
- //The reposition function works for the jump buttons on the bottom; it progessively moves the timeline from its current frame to the target frame as passed to the function when it is called by the buttons.
- function reposition(targetframe) {
- //loop on enterFrame at the fps
- pics_mc.addEventListener(Event.ENTER_FRAME, onLoop);
- function onLoop(e:Event) {
- //what frame are we on?
- currentf = pics_mc.currentFrame;
- //how far do we have to go?
- var differencef = targetframe - currentf; //difference between current and target frames
- //how fast do we want to get there? How about 28% of the way at a time. The higher the number, the faster the speed.
- var speed =.28;
- //if there is no difference between our currentframe and the target, then let's remove the enterframe loop.
- if (differencef == 0) {
- trace("done");
- gotoAndStop(targetframe);
- pics_mc.removeEventListener(Event.ENTER_FRAME, onLoop);
- // otherwise move foward or back using our speed
- } else if (differencef > 0) {
- moveamount = Math.ceil(differencef*speed);
- moveto = currentf + moveamount;
- pics_mc.gotoAndStop(moveto);
- } else if (differencef < 0) {
- moveamount = Math.floor(differencef*speed);
- moveto = currentf + moveamount;
- pics_mc.gotoAndStop(moveto);
- }
- //we need to update the scrollbar to correspond to our new position, let's call the custom function
- updateScrollBar();
- //trace(currentf);
- }
- }
- //This function repositions the scrollbar
- function updateScrollBar() {
- //The factor is the length of the movieclip timeline (100 frames) divided by (the height of the scrollbar minus scroll button height).
- //240 - 30 = 210 --- 100/210 = .4762
- var factor:Number = pics_mc.totalFrames /(scrollbar_mc.height - scrollbar_mc.scroller_mc.height - 2);
- //var factor = .4762;
- currentf = pics_mc.currentFrame;
- scrollbar_mc.scroller_mc.y = Math.round(currentf/factor - 1);
- }
- //These buttton instances call the reposition function, with the target frame indicated in the parenthesis
- b1.addEventListener(MouseEvent.CLICK, onClick);
- b2.addEventListener(MouseEvent.CLICK, onClick);
- b3.addEventListener(MouseEvent.CLICK, onClick);
- b4.addEventListener(MouseEvent.CLICK, onClick);
- b5.addEventListener(MouseEvent.CLICK, onClick);
- b6.addEventListener(MouseEvent.CLICK, onClick);
- b7.addEventListener(MouseEvent.CLICK, onClick);
- b8.addEventListener(MouseEvent.CLICK, onClick);
- b9.addEventListener(MouseEvent.CLICK, onClick);
- b10.addEventListener(MouseEvent.CLICK, onClick);
- function onClick(e:MouseEvent) {
- switch (e.target) {
- case b1:
- reposition(1);
- break;
- case b2:
- reposition(12);
- break;
- case b3:
- reposition(23);
- break;
- case b4:
- reposition(34);
- break;
- case b5:
- reposition(45);
- break;
- case b6:
- reposition(56);
- break;
- case b7:
- reposition(67);
- break;
- case b8:
- reposition(78);
- break;
- case b9:
- reposition(89);
- break;
- case b10:
- reposition(100);
- break;
- }
- }
- var rate:Number;
- //The left and right buttons scroll the timeline with the speed increasing at a rate of 0.10
- rarrow_btn.addEventListener(MouseEvent.MOUSE_DOWN, onRArrowDown);
- function onRArrowDown(e:MouseEvent) {
- rate = 1;
- pics_mc.addEventListener(Event.ENTER_FRAME, onRLoop);
- }
- function onRLoop(e:Event) {
- //if we only want a frame at a time, we could use nextFrame
- //pics_mc.nextFrame();
- pics_mc.gotoAndStop(Math.floor(pics_mc.currentFrame + rate));
- rate += 0.1;
- updateScrollBar();
- }
- //Delete the scrolling on enterframe function on mouse up
- rarrow_btn.addEventListener(MouseEvent.MOUSE_UP, onRArrowUp);
- function onRArrowUp(e:MouseEvent) {
- pics_mc.removeEventListener(Event.ENTER_FRAME, onRLoop);
- }
- larrow_btn.addEventListener(MouseEvent.MOUSE_DOWN, onLArrowDown);
- function onLArrowDown(e:MouseEvent) {
- rate = 1;
- pics_mc.addEventListener(Event.ENTER_FRAME, onLLoop);
- }
- function onLLoop(e:Event) {
- //pics_mc.prevFrame();
- pics_mc.gotoAndStop(Math.floor(pics_mc.currentFrame - rate));
- //You can't go a frame less than zero, so if it trys... let's just go to frame 1
- if (Math.floor(pics_mc.currentFrame - rate) < 1) {
- pics_mc.gotoAndStop(1);
- }
- rate += 0.1;
- updateScrollBar();
- }
- larrow_btn.addEventListener(MouseEvent.MOUSE_UP, onLArrowUp);
- function onLArrowUp(e:MouseEvent) {
- pics_mc.removeEventListener(Event.ENTER_FRAME, onLLoop);
- }
- //larrow_btn.onReleaseOutside= function() {
- // delete pics_mc.onEnterFrame;
- //}
- //Scrollbar code
- var dragging:Boolean = false;
- scrollbar_mc.scroller_mc.scroller_btn.addEventListener(MouseEvent.MOUSE_DOWN, onScrollBtnDown);
- function onScrollBtnDown(e:MouseEvent) {
- //when you press the scroll button start draging it, constrained to the vertical length of the scrolling bar/area
- var myContrainRect:Rectangle = new Rectangle(0,0,0,210);
- scrollbar_mc.scroller_mc.startDrag(false, myContrainRect);
- dragging = true;
- //The mover function makes the timeline move to the appropriate frame
- stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
- }
- //Delete the scrolling on up... (or on release outside or up on stage code also below)
- scrollbar_mc.scroller_mc.scroller_btn.addEventListener(MouseEvent.MOUSE_UP, onScrollBtnUp);
- function onScrollBtnUp(e:MouseEvent) {
- trace(scrollbar_mc.scroller_mc.y);
- scrollbar_mc.scroller_mc.stopDrag();
- stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove)
- }
- function onMove(e:MouseEvent) {
- //The factor is the length of the movieclip timeline (100 frames) divided by the height of the scrollbar (210). Take the y position of the scroll bar button and use it to determine the appropriate frame to goto in the movieclip.
- var factor:Number = pics_mc.totalFrames /(scrollbar_mc.height - scrollbar_mc.scroller_mc.height - 2);
- //factor = .4762;
- mytarget = Math.ceil(scrollbar_mc.scroller_mc.y*factor);
- pics_mc.gotoAndStop(mytarget);
- e.updateAfterEvent();
- //trace(mytarget);
- }
- //code to remove scrolling if you've moved off the buttons (to the stage) and then release/up the mouse
- stage.addEventListener(MouseEvent.MOUSE_UP, onScrollBtnUpOutside);
- function onScrollBtnUpOutside(e:MouseEvent) {
- if (dragging) {
- trace(scrollbar_mc.scroller_mc.y);
- scrollbar_mc.scroller_mc.stopDrag();
- stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove)
- }
- pics_mc.removeEventListener(Event.ENTER_FRAME, onLLoop);
- pics_mc.removeEventListener(Event.ENTER_FRAME, onRLoop);
- }