At long last, user-driven interactivity!
Click here to download the .fla, .as, and .swf files for the above project. (2mb download)
Click here to download an even simpler project that demonstrates how to call an instance to the stage.
Click here to download an even simpler project that demonstrates how to call an instance to the stage.
Here is the code that makes the above little .swf work (this is what's in the .as file).
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.media.Sound; //sound
import flash.media.SoundChannel;//sound
public class RunningManInteraction extends MovieClip
{
var sceneStanding:SceneStanding;
var sceneRunning:SceneRunning;
var technoMuzak:TechnoMuzak;//sound
var soundChannel:SoundChannel;//sound
public function RunningManInteraction()// This is the "constructor method!"
{
sceneStanding = new SceneStanding();
sceneRunning = new SceneRunning();
technoMuzak = new TechnoMuzak();//sound
soundChannel = new SoundChannel();//sound
addChild(sceneStanding);
sceneStanding.x = 200;
sceneStanding.y = 160;
/*These two lines place the instance in the middle of the stage which is 400 x 320
This could be done in better fashion by changing the registrations of the two scene symbols or
by creating a stage center constant. */
//Add event listeners
sceneStanding.invisBut.addEventListener(MouseEvent.CLICK, onStandButClick);
sceneRunning.invisBut.addEventListener(MouseEvent.CLICK, onRunButClick);
}
//Event handlers
function onStandButClick(event:MouseEvent):void
{
trace("you clicked me");//Explain use of trace. Also how to "comment out."
addChild(sceneRunning);
sceneRunning.x = 200;
sceneRunning.y = 160;
removeChild(sceneStanding);
soundChannel = technoMuzak.play(0,int.MAX_VALUE);/*The play method takes three parameters which can be left blank () or filled to create loops and such. This set-up creates a loop as follows. The first parameter (0) is how far in you want the sound file to start (in milliseconds). The second parameter (int.MAX_VALUE) is how many times you want the sound file to loop. (int.MAX_VALUE) gives us an endless loop for all practicial purposes. If you wanted to apply a sound transform, you'd give that as the third parameter. */
sceneRunning.aniRoad.gotoAndPlay(1); /* This line was added later to fine-tune the user experience and is an example of addressing unexpected issues (and learning on-the-fly). This line was added to get the road animation to restart each time the button is clicked; without it, the loop keeps playing and appears to restart at a random position.*/
}
function onRunButClick(event:MouseEvent):void
{
trace("back to basics");
addChild(sceneStanding);
sceneStanding.x = 200;
sceneStanding.y = 160;
removeChild(sceneRunning);
soundChannel.stop();/* sound. Note that this is a different syntax to stop the sound than to play the sound! Stop is a method of the soundChannel variable, while play is a method of the technoMuzak instance.*/
}
}
}
Hey Kids! Here is a simplified version of the scene change code. No sound! etc.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class SceneJumpDemo extends MovieClip
{
var scene1:Scene1;// creates a variable called “scene1” that will hold instances of the symbol named “Scene1.”
var scene2:Scene2;
public function SceneJumpDemo()// This is the "constructor method!" Whatever is in here happens automatically when the program starts.
{
scene1 = new Scene1(); // fills the variable named “scene1” with a new instance of the library symbol named, “Scene1.”
scene2 = new Scene2();
addChild(scene1);//The addChild keyword adds the instance contained in the scene1 variable to the stage.
//Add event listeners. Event listeners tell the computer to pay attention if something happens.
scene1.bigBut.addEventListener(MouseEvent.CLICK, onScene1ButClick);//"bigBut" is the instance name of a button in the Scene1 symbol
scene2.bigBut.addEventListener(MouseEvent.CLICK, onScene2ButClick); //"bigBut" is the instance name of a button in the Scene2 symbol
}
//Event handlers. Event handlers tell the computer what to do once something happens.
function onScene1ButClick(event:MouseEvent):void //this is what happens when you click the button in Scene1
{
addChild(scene2);
removeChild(scene1);//removeChild takes an instance off the stage.
}
function onScene2ButClick(event:MouseEvent):void //this is what happens when you click the button in Scene2
{
addChild(scene1);
removeChild(scene2);
}
}
}