Actionscript

This page describes the Actionscript that went into the Projects.

Project 1 - Flash Animation

For this project, Actionscript was used to make the flash animation play forward and backward depending on the location of the mouse pointer. Below is the code used for the project, and what it does. Also, the "//" indicates a comment that is not part of the code, but explains what it does.

// Import all of the classes in the flash.events namespace. This will
// let use write out the MouseEvent class without writing the full
// classpath out, a la flash.events.MouseEvent.
import flash.events.*

// Stop the timeline.
stop();

// Define a variable for this MovieClip and give it a default variable.
// It's defined as private, so only methods in this class will be able to
// access it.
var moveForward:Boolean = false;

// Define a function that will get called every frame.
// Notice that event handlers get passed a copy of the event.
function moveMovieClip(event:Event):void {
// Test if the moveForward variable is true.
// Since moveForward is a Boolean, we do not have to write a full
// test for it, such as if(moveForward == true).
if(moveForward) {
skate_mc.nextFrame();
} else {
skate_mc.prevFrame();
}
}

// Set up an event listener so that moveMovieClip() will be called
// when the ENTER_FRAME event occurs.
skate_mc.addEventListener(Event.ENTER_FRAME, moveMovieClip);

// Do the same thing, setting up a handler for the MOUSE_OVER and MOUSE_OUT
// events, but specify each event handler with an inline function rather than
// a named function. Again, we have to make sure that each function takes the
// event which triggered them as a parameter.
skate_mc.addEventListener(MouseEvent.MOUSE_OVER, function(mouseEvent:MouseEvent):void {
moveForward = true;
});
skate_mc.addEventListener(MouseEvent.MOUSE_OUT, function(mouseEvent:MouseEvent):void {
moveForward = false;
});

Project 3 - 10 Tips

Actionscript was used in this project to do many things like formatting text through a style sheet, getting the tips through files outside of the project, and using buttons t display the different tips. Here's the actionscript that I used with the comments included.

/* Let's stop our movie right off the bat. */
stop();

/* Let's alias some namespaces so that I don't have to type everything out. */
import flash.text.StyleSheet;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;

/* Then we'll apply a style sheet to our TextArea. */
/* We need to make a new StyleSheet object first. */
var formatStyleSheet:StyleSheet = new StyleSheet();

/* Then, we set up a URLLoader class, which os used to load data from URLs, including files
and set up what function gets called when the stylesheet is loaded. */
var styleSheetLoader:URLLoader = new URLLoader();
styleSheetLoader.addEventListener(Event.COMPLETE, loadCSS);

/* This is a listener - it listens for the load to be completed, then runs the code. */
function loadCSS(e:Event):void {
if (true) { // Needed?
formatStyleSheet.parseCSS(e.target.data);
html_txt.styleSheet = formatStyleSheet;
} else {
html_txt.htmlText = "Error loading CSS file!";
}
};

/* Finally, we tell Flash to go ahead and load the stylesheet. */
styleSheetLoader.load(new URLRequest("stylesheet.css"));

/* We need to make sure that our textField looks at the fonts we put in our library. */
html_txt.embedFonts = true;

/* Just as we had to create a StyleSheet object above, here we must make another instance
of the URLLoader class. This one will load our snippets of HTML from .txt files. */
var textLoader:URLLoader = new URLLoader();
textLoader.addEventListener(Event.COMPLETE, loadText);

/* As above, we create a listener for the load event. */
function loadText(e:Event):void {
if (true) { // Needed?
html_txt.htmlText = e.target.data;
// tipTextBox.htmlText = textLoader.info;
} else {
html_txt.htmlText = "Error loading HTML text file!";
}
}

/* Now, let's make our buttons load things. */
scout_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip1.txt'));
});
soldier_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip2.txt'));
});
pyro_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip3.txt'));
});
demo_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip4.txt'));
});
heavy_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip5.txt'));
});
engy_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip6.txt'));
});
medic_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip7.txt'));
});
sniper_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip8.txt'));
});
spy_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip9.txt'));
});
spy2_btn.addEventListener(MouseEvent.MOUSE_UP, function() {
textLoader.load(new URLRequest('tip10.txt'));
});

Project 4 - Audio

I used actionscript to do many things in this project, so here's a little list of most of them. To bring information,the pictures, and sound files in via the XML. To set up the play, stop, pause, next, and previous buttons. To set a counter to tell you what interview it is on. Here's the code:

// Global variables that we'll be using over and over again.

// Some namespaces, as usual
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;

// This variable is a number, and it will be 1, 2, or 3 when our movie is running.
var currentInterview:Number = 0;

var textLoader:URLLoader = new URLLoader();

textLoader.addEventListener(Event.COMPLETE, function(e:Event) {
main_txt.text = e.target.data;
});

var xmlFile:XML;

// Make a URLLoader for the XML configuration file that we've put together.
var xmlLoader:URLLoader = new URLLoader();

xmlLoader.addEventListener(Event.COMPLETE, function(e:Event) {
xmlFile = new XML(e.target.data);

// Start the movie going (go from 0 - 1 as we start.)
nextInterview();
});

xmlLoader.load(new URLRequest("Animations/project4_baranciukas/data.xml"));

// This variable will keep track of whether or not the music is paused.
// This is optional: you do not have to use it in your project.
var isMusicPaused:Boolean = false;

// Make a Sound object to handle playing the sound.
var mySound:Sound;

//
var soundPos:Number = 0;

// Make a SoundChannel object so that we can start and stop our sounds.
var mySoundChannel:SoundChannel;

// Now we'll set up the three event handlers we need to use to make our
// sound come to life.

function setUpSound():void {
// Is a sound still going?
if(mySoundChannel != null) {
if(mySound.isBuffering)
mySound.close();
mySoundChannel.stop();
mySoundChannel = null;
}


mySound = new Sound();

// First up, when the sound's done playing, move to the next one.
mySound.addEventListener(Event.SOUND_COMPLETE, nextInterview);

mySound.addEventListener(Event.COMPLETE, function() {
// We're playing, not paused.
isMusicPaused = false;
mySoundChannel = mySound.play();
});

mySound.addEventListener(Event.ID3, function() {
// Load the "song name" field and put it into the text field under the stereo.
if(mySound.id3.songName != null)
intervieweeName.text = mySound.id3.songName;
});
}

// A new function to increase the current interview count, and wrap it back around
// if it's gotten too high.
function nextInterview(e:Event = null)
{
currentInterview++;

if(currentInterview > 3)
currentInterview = 1;

loadInterview(currentInterview);
}

// A function to decrease the current interview count, and wrap it back around
// if it's gotten too low.
function prevInterview(e:Event = null)
{
currentInterview--;

if(currentInterview < 1)
currentInterview = 3;

loadInterview(currentInterview);
}

// A function to load an interview, which takes a single number parameter
// Why take a parameter rather than just loading the current interview?
// This way, we could make clickable buttons for all three interviews and call
// them like so:

// loadInterview(1);
// loadInterview(2);
// loadInterview(3);

function loadInterview(interviewNumber)
{
// Set up some local variables to do the loading.
// Why subtract one? Arrays are zero-based.
var picturePath:String = xmlFile.interview[interviewNumber - 1].pic;
var bioText:String = xmlFile.interview[interviewNumber - 1].text;
var audioPath:String = xmlFile.interview[interviewNumber - 1].audio;

trace(picturePath);
trace(bioText);
trace(audioPath);

// First, we set up the Loader component.
pictureLoader.load(new URLRequest(picturePath));

// Then we load a little text.
main_txt.text = bioText;

// Finally, we start the audio file loading.
setUpSound();
mySound.load(new URLRequest(audioPath));

// In addition, update the number in the center, which tells
// what interview number we're on.
currentInterviewField.text = interviewNumber;
}

// Finally, some button event handlers.
nextButton.addEventListener(MouseEvent.MOUSE_UP, nextInterview);

prevButton.addEventListener(MouseEvent.MOUSE_UP, prevInterview);

stopButton.addEventListener(MouseEvent.MOUSE_UP, function() {
isMusicPaused = true;
soundPos = 0;
mySoundChannel.stop();
});

pauseButton.addEventListener(MouseEvent.MOUSE_UP, onPauseClick);
function onPauseClick(evt:MouseEvent):void {
soundPos = mySoundChannel.position;
mySoundChannel.stop();
isMusicPaused = true;
}

playButton.addEventListener(MouseEvent.MOUSE_UP, onPlayClick);
function onPlayClick(evt:MouseEvent):void {
if (isMusicPaused) {
mySoundChannel = mySound.play(soundPos);
}
}

// Finally, stop the movie.
stop();