Understanding Flash External Scripts
2012 mkelly/robert morris university
thanks to Jody Hall at http://theflashconnection.com/ example and fla downloads: http://www.onestopdesktop.com/examples3.html we’re going to do a makeover on the Match Game from the previous tutorial, Creating a Flash Matching Game for Beginners. we’re going to create a script that not only tells our previous fla what to do but could also talk to any similarly set up fla. first let's set up a package to store our class, and set the class’ path so that Flash can find it. packages are folders on your hard drive. you might want to store classes in separate packages/folders to group similar classes together. the key to understanding the whole package thing is to set up one folder on your hard drive as a “gateway” directory. the location of it doesn't matter, but this directory will be designated as being in the class path. go ahead and create a new folder called “classes” somewhere safe on your hard drive, and we'll tell Flash to add it to the class path. this folder’s name won't be used at all in the package statements of our classes, nor in the import statements of our fla files. there are two ways in Flash to designate a class path. one is here: ... in the Property inspector, if you click on the stage but not on any object. you can type it in:
this only works for one document at a time. the other better way sets the class path for all new documents. go to:
click on the folder icon...
find your folder and ‌
now Flash knows. click File/New/ document's script pane, type the following:
. in the new Actionscript
package com.mysite.mouse { this would indicate that you would have to have this script live in a folder called mouse that would be the end of the line of a structure of folders going from classes to com to mysite and finally to mouse. if you store your external file directly in the same folder as your fla you would just type package... you would have to connect your fla (in this example Monica Rodriguez’s game) to the package. in frame two where this scripting becomes necessary, type in:
stop();
notice that Flash already knows of the existence and position of ClipDragger and is ready to fill it in for us?...finish it like above or below:
you would then do the rest:
you’d also eventually need these:
we won’t need target_nodes like before. the external script will take care of all of that in a single line of code. each time you alter an AS file, you will need to save it, or else when you run the fla, it won’t be using the new changes you’ve made!!! below is the final script that could power any of the Match Games you can see in the web examples: http://www.onestopdesktop.com/examples2.html the script can be downloaded and used from here: http://www.onestopdesktop.com/examples3.html package {//these are Flash classes inside their packages that must be imported first (please see the handout on importing classes called “Importing Base Classes into Packages” for more info. import flash.display.MovieClip; import flash.events.MouseEvent; import flash.text.TextField; import flash.utils.Timer; import flash.events.*; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundMixer; import flash.net.URLRequest; flash.media.SoundTransform; public class ClipDragger extends MovieClip {//inside this Class, these are variables (also called Properties) that this script will use to talk either within itself (private) or with the game (pubic). some
of them will be familiar from the timeline based script in the tutorial called “Create a Flash Matching Game for Beginners.” the sound is being brought in a different way, but it’s the same squeak. OriginalX and Y will be used to snap the movieClips back in place if they are dragged onto the incorrect target, an addition to the Beginner’s game. public var target:MovieClip; public var score:Number; private var _clip:MovieClip; private var scoreBox:TextField; private var thisParent:*; private var originalX:Number; private var originalY:Number; private var myString:String; private var _timer_txt:TextField; private var _item_txt:TextField; private var secondsToCountdown:Number = 10; private var timer:Timer = new Timer(1000,secondsToCountdown); private var req:URLRequest = new URLRequest("squeak.mp3"); private var snd:Sound = new Sound(); private var sndCh:SoundChannel = new SoundChannel(); public function ClipDragger(clip:MovieClip) {//this is the constructor function, the foundation for everything else starts by renaming the clips from the game fla. to _clip which we defined in the variables above as a movieClip. from now on whatever happens to _clip happens to clip. thisParent lets us talk to other movieClips in clip’s timeline. we just have to work u variables like we did with _clip in order to do that. as long as we match a variable with something else that Flash will understand either within its own vocabulary (blue words) or something we define ourselves well enough for Flash to understand it using a combo of its words and our own, we should get no errors. scoreBox and _timer_txt are taking the place of the two dynamic text boxes in thisParent that have the instance names of score_txt, item_txt (the name of the object will flash (in a textbox Instance named item_txt that is in the labels layer) when the object is selected, and timer_txt. we also pulled the timer out of the previous game and stuck it in here. we do this once and we’ll never have to do it again for similar games - well worth the time. _clip = clip; _clip.buttonMode = true; thisParent = _clip.parent; target = _clip.target; originalX = _clip.x; originalY = _clip.y; scoreBox = thisParent.score_txt; myString = scoreBox.text; _timer_txt = thisParent.timer_txt; _item_txt = thisParent.item_txt; _clip.addEventListener(MouseEvent.MOUSE_DOWN,drag); _clip.addEventListener(MouseEvent.MOUSE_UP,drop); timer.addEventListener(TimerEvent.TIMER,countdown);
}
timer.start(); snd.load(req);
private function countdown(event:TimerEvent) {//this is the TimerEvent we added to the timer. the secondsToCountdown— takes away one second starting in this case at 10 at one second (1000 milliseconds) in the variable definition above and filling the time number as a string (text version) into our _timer_txt text box which in turns feeds it into the text box we associated it with in the previous ClipDragger Class variable definitions and ClipDragger constructor function above. we add two conditions to test for perfect score (400 in this case) or time expiration to make us go to either the “win” or “lose” frame labels in the main movie’s timeline. the first condition also turns off the timer so it doesn’t run out and make us lose. secondsToCountdown--; _timer_txt.text = String(secondsToCountdown); if (scoreBox.text == "400") { timer.stop(); thisParent.gotoAndStop("win"); timer.removeEventListener (TimerEvent.TIMER,countdown);
}
} if (_timer_txt.text == "0") { thisParent.gotoAndStop("lose"); }
private function drag(e:MouseEvent):void {//this function lets us drag the clips on the stage. _clip.startDrag(); _item_txt.text = String(_clip.name); } private function drop(e:MouseEvent):void {//this function lets us drop the clips on their suspected targets. it sets up two possible tests. if we drag it correctly and it hits the target, the squeak sound will play, it will stick to the target’s x and y values, it will store the current value in the score box on the stage in a variable called myString and then convert that text into a number so 100 can be added to it and then replace the original string with that new number. it removes the listeners so we can start all over with the next movieClip we drag. if we drag it incorrectly (else), 100 is subtracted from the score (harsh but true - we can no longer get a perfect score so we lose even if we beat the timer), and the movieClips are snapped back into their original positions without further delay. _clip.stopDrag(); if (_clip.hitTestObject(target))
{
sndCh = snd.play(0,1); _clip.x = target.x; _clip.y = target.y; myString = scoreBox.text; score = Number(myString) + 100; scoreBox.text = String(score); _clip.removeEventListener (MouseEvent.MOUSE_DOWN,drag); _clip.removeEventListener (MouseEvent.MOUSE_UP,drop); } else { myString = scoreBox.text; score = Number(myString) - 100; scoreBox.text = String(score); _clip.x = originalX; _clip.y = originalY; } } } }
for more actionscript examples: http://www.adobe.com/devnet/flash/samples.html