I also get errors on my output such as:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
AttackonSharkwithMovement_fla::MainTimeline/fl_AnimateHorizontally()
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
AttackonSharkwithMovement_fla::MainTimeline/fl_AnimateHorizontally_2()
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
AttackonSharkwithMovement_fla::MainTimeline/fl_EnterFrameHandler_2()[
Scene 1 - Main Menu
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
import flash.system.fscommand
import flash.events.MouseEvent
stop();
//Button Scripts
Play_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene);
function fl_ClickToGoToScene(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Game");
}
Instructions_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_10);
function fl_ClickToGoToAndStopAtFrame_10(event:MouseEvent):void
{
gotoAndStop(6);
}
function quit (event:MouseEvent):void
{
fscommand ("quit");
}
Quit_Button.addEventListener(MouseEvent.MOUSE_DOWN,quit);
Scene 2 - Game
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
import flash.system.fscommand;
import flash.events.TimerEvent;
import flash.utils.Timer;
stop();
//Variables
var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var sharkSpeed:Number = 10;
var score1:Number = 0;
var maxHP:int = 100;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;
//Health Script
function updateHealthBar():void
{
percentHP = currentHP / maxHP;
healthBar.barColor.scaleX = percentHP;
}
//Button Scripts
MainMenu_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_2);
function fl_ClickToGoToScene_2(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Main Menu");
}
Instructions_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene_2);
function fl_ClickToGoToNextScene_2(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(6, "Main Menu");
}
//Keyboard Movement
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function keyDownHandler(KeyEvent:KeyboardEvent):void
{
if (KeyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = true;
}
else if (KeyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = true;
}
else if (KeyEvent.keyCode == Keyboard.DOWN)
{
downPressed = true;
}
else if (KeyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if (keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
}
else if (keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
}
else if (keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
}
else if (keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
}
}
function gameLoop(loopEvent:Event):void
{
if (rightPressed)
{
shark.x += sharkSpeed;
}
else if (leftPressed)
{
shark.x -= sharkSpeed;
}
else if (downPressed)
{
shark.y += sharkSpeed;
}
else if (upPressed)
{
shark.y -= sharkSpeed;
}
}
//AI Movement
addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
function fl_AnimateHorizontally(event:Event)
{
enemy1.x += 2;
enemy2.x += 2;
enemy3.x += 2;
enemy4.x += 2;
enemy5.x += 2;
enemy6.x += 2;
megaladon.x += 2;
}
addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally_2);
function fl_AnimateHorizontally_2(event:Event)
{
fishes.x += 1.5;
}
//Colission
function hitsTheObject(e:Event)
{
if (shark.hitTestObject(enemy1))
{
trace("player collided with enemy");
currentHP -= 50;
if (currentHP <= 0)
{
currentHP = 0;
trace("You died!");
MovieClip(this.root).gotoAndPlay(1, "Game Over");
}
updateHealthBar();
}
}
//Score Script
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_2);
function fl_EnterFrameHandler_2(event:Event):void
{
gameScore.text = String(score1);
score1 += 1;
trace("gameScore.text is : " + gameScore.text);
trace("score1 is : " + score1);
}
//Timer Script
var myTimer:Timer = new Timer(1000,50);
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void
{
myText_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "You Survived");
}
Scene 3 - You Survived
stop();
//Button Scripts
MainMenu_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_4);
function fl_ClickToGoToScene_4(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Main Menu");
}
PlayAgain_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_12);
function fl_ClickToGoToScene_12(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Game");
}Scene 4 - Game Over
stop();
//Button Scripts
MainMenu_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_9);
function fl_ClickToGoToScene_9(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Main Menu");
}
PlayAgain_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_11);
function fl_ClickToGoToScene_11(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Game");
}
As identified in the comments, you need to remove the eventListener which you can achieve with:
removeEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);.
I would suggest implementing the following line whenever you bind to a frame event such as Event.ENTER_FRAME
this.addEventListener(Event.REMOVED_FROM_STAGE, function(){
try{
removeEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
}catch(error){
//error handling optional in this case.
}
});
This will get called ONCE only right before the object is destroyed/removed from the stage i.e. when you call MovieClip(this.root).gotoAndPlay(1, "Game");
Note: You can just put all of your 'global' events into the try area - you don't need this call every time you add an event.
Additionally, you don't need this whatsoever for movieclips as all of your events will get cleaned up automatically once they are removed from the stage via the garbage collector.
this is my code:
stop();
import gs.*;
import gs.easing.*;
import gs.TweenMax;
stage.addEventListener(MouseEvent.CLICK, rijden); // Add the button click
function rijden(e:MouseEvent):void {
TweenLite.to(auto, 4, {x:666.15, y:375.6});
}
var grav:Number = 7.5;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(MouseEvent.CLICK, spring); // Add the button click
stage.addEventListener(Event.ENTER_FRAME, update);
function spring(e:MouseEvent):void {
if(jumping != true)
{
TweenLite.to(man, 0.5, {rotation:360});
jumpPow = -50;
jumping = true;
}
}
function update(evt:Event):void
{
if(jumping)
{
man.y += jumpPow;
jumpPow += grav;
if(man.y >= 375)
{
jumping = false;
man.y = 375;
}
}
}
but now if I click on the stage both the objects start to tween or something. but it needs to be like this: if I click the stage: first function rijden needs to be activated, on a second Click on the stage function spring needs to be activated.
Can someone help me??
Reformat your code like this:
//...
stage.addEventListener(MouseEvent.CLICK, rijden); // Add the button click
function rijden(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, rijden); // unplug "rijden" handler
stage.addEventListener(MouseEvent.CLICK, spring); // Add the button click
TweenLite.to(auto, 4, {x:666.15, y:375.6});
}
var grav:Number = 7.5;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(Event.ENTER_FRAME, update);
function spring(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, spring); // unplug "spring" handler
if(jumping != true)
{
TweenLite.to(man, 0.5, {rotation:360});
jumpPow = -50;
jumping = true;
}
}
//...
I'm trying to take this very simple "game" and give it three save/load slots. Following a separate tutorial I can make it work with a single save slot but once I try adding more, it gives me the following error message.
1046:Type was not found or was not compile-time constant: save2.
1046:Type was not found or was not compile-time constant: save3.
I am new to actionscript 3 so I'm sure I'm being very newbish but I have tried to figure this out for quite some time now but just can't seem to. The whole thing is controlled by buttons already placed on the scene. I appreciate any help I can get.
The code:
import flash.net.SharedObject;
var saveDataObject:SharedObject;
var currentScore:Number = 0
init();
function init():void{
btnAdd.addEventListener(MouseEvent.CLICK, addScore);
btnSave1.addEventListener(MouseEvent.CLICK, save1);
btnSave1.addEventListener(MouseEvent.CLICK, saveData);
btnSave2.addEventListener(MouseEvent.CLICK, save2);
btnSave2.addEventListener(MouseEvent.CLICK, saveData);
btnSave3.addEventListener(MouseEvent.CLICK, save3);
btnSave3.addEventListener(MouseEvent.CLICK, saveData);
btnLoad1.addEventListener(MouseEvent.CLICK, save1);
btnLoad1.addEventListener(MouseEvent.CLICK, loadData);
btnLoad2.addEventListener(MouseEvent.CLICK, save2);
btnLoad2.addEventListener(MouseEvent.CLICK, loadData);
btnLoad3.addEventListener(MouseEvent.CLICK, save3);
btnLoad3.addEventListener(MouseEvent.CLICK, loadData);
}
function save1(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile1");
}
function save2(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile2");
}
function save3(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile3");
}
function addScore(e:MouseEvent):void{
currentScore += 1;
updateScoreText();
}
function saveData(e:MouseEvent):void{
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}
function loadData(e:MouseEvent):void{
currentScore = saveDataObject.data.savedScore;
updateScoreText();
trace("Data Loaded!");
}
function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore);
trace("Score text updated");
}
I tried your code and it works like a charm...
Anyways, I've made a simpler version that doesn't use so many functions and Events.
Here is a pure AS3 version of it (just save it as Test.as3 and use as Document Class in Flash), but you can copy the content of the Test() method and paste in a action frame.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.SharedObject;
import flash.text.TextField;
public class Test extends Sprite
{
public function Test()
{
/***** START: Faking buttons and field *****/
var txtScore:TextField = new TextField();
addChild(txtScore);
var btnAdd:Sprite = new Sprite();
var btnSave1:Sprite = new Sprite();
var btnSave2:Sprite = new Sprite();
var btnSave3:Sprite = new Sprite();
var btnLoad1:Sprite = new Sprite();
var btnLoad2:Sprite = new Sprite();
var btnLoad3:Sprite = new Sprite();
var items:Array = [btnAdd, null, btnSave1, btnSave2, btnSave3, null, btnLoad1, btnLoad2, btnLoad3];
for (var i:int = 0; i < items.length; ++i)
{
var item:Sprite = items[i];
if (item)
{
item.graphics.beginFill(Math.random() * 0xFFFFFF);
item.graphics.drawRect(0, 0, 100, 25);
item.graphics.endFill();
item.x = 25;
item.y = i * 30 + 25;
addChild(item);
}
}
/***** END: Faking buttons and field *****/
var saveDataObject:SharedObject;
var currentScore:Number = 0
btnAdd.addEventListener(MouseEvent.CLICK, addScore);
btnSave1.addEventListener(MouseEvent.CLICK, save);
btnSave2.addEventListener(MouseEvent.CLICK, save);
btnSave3.addEventListener(MouseEvent.CLICK, save);
btnLoad1.addEventListener(MouseEvent.CLICK, load);
btnLoad2.addEventListener(MouseEvent.CLICK, load);
btnLoad3.addEventListener(MouseEvent.CLICK, load);
function getLocal(target:Object):String
{
if (target == btnSave1 || target == btnLoad1)
{
return "savefile1";
}
else if (target == btnSave3 || target == btnLoad2)
{
return "savefile2";
}
else if (target == btnSave2 || target == btnLoad3)
{
return "savefile3";
}
}
function save(e:MouseEvent):void
{
var local:String = getLocal(e.target);
saveDataObject = SharedObject.getLocal(local);
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}
function load(e:MouseEvent):void
{
var local:String = getLocal(e.target);
saveDataObject = SharedObject.getLocal(local);
currentScore = saveDataObject.data.savedScore;
updateScoreText();
trace("Data Loaded!");
}
function addScore(e:MouseEvent):void
{
currentScore += 1;
updateScoreText();
}
function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore);
trace("Score text updated");
}
}
}
}
For some reason my value is not being updated outside of the function. I'm trying to make a button, instanced "plus", move a movie clip "topArrow" constantly upward. I figured the boolean would be an easy way to trigger this, but it isn't being updated outside of the function. Why is this?
import flash.events.Event;
import flash.events.MouseEvent;
var speed:Number = 1;
plus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_up);
plus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);
minus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_down);
minus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);
var move_up:Boolean = false;
var move_down:Boolean = false;
function arrow_up(event:MouseEvent):void
{
trace("button pressed");
move_up = true;
}
function arrow_stop(event:MouseEvent):void
{
move_up = false;
move_down = false;
}
function arrow_down(event:MouseEvent):void
{
move_down = true;
}
while (move_up==true)
{
topArrow.y += speed;
}
while (move_down==true)
{
topArrow.y -= speed;
}
if(move_up)
{
trace("true");
}
Those while loops are scary, once move_up is true it will go into that loop and never exit?
I would do something like the below instead to animate the movie clip :
var speed:Number = 1;
plus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_up);
plus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);
minus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_down);
minus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
var move_up:Boolean = false;
var move_down:Boolean = false;
function arrow_up(event:MouseEvent):void
{
trace("button pressed");
move_up = true;
}
function arrow_stop(event:MouseEvent):void
{
move_up = false;
move_down = false;
}
function arrow_down(event:MouseEvent):void
{
move_down = true;
}
function onEnterFrame(event:Event):void
{
if(move_up)
topArrow.y += speed;
else if(move_down)
topArrow.y -=speed;
if(move_up)
{
trace("true");
}
}
Decompile binary and take a look at move_up setter function.
How would I go about converting my actionscript 3 (on the timeline) into a class? As I am using the same functionality across multiple FLA files.
// IMPORTS
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.external.ExternalInterface;
// VARIABLES
mcToolTip.toolTip.blendMode = BlendMode.LAYER;
mcToolTip.toolTip.text = "";
var toolio = false;
var settext;
var urlButton1:String = 'URL STRING 1';
var urlButton2:String = 'URL STRING 1';
var urlButton3:String = 'URL STRING 1';
var urlButton4:String = 'URL STRING 1';
var urlButton5:String = 'URL STRING 1';
// MISC
mcButton1.stop();
mcButton2.stop();
mcButton3.stop();
mcButton4.stop();
mcButton5.stop();
mcButton1.buttonMode = true;
mcButton2.buttonMode = true;
mcButton3.buttonMode = true;
mcButton4.buttonMode = true;
mcButton5.buttonMode = true;
// EVENT LISTENERS
//button1
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton1.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,button1Text);
mcButton1.addEventListener(MouseEvent.MOUSE_DOWN,callButton1);
mcButton1.addEventListener(MouseEvent.CLICK,mclick);
//button2
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton2.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,button2Text);
mcButton2.addEventListener(MouseEvent.MOUSE_DOWN,callButton2);
mcButton2.addEventListener(MouseEvent.CLICK,mclick);
//button3
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton3.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,button3Text);
mcButton3.addEventListener(MouseEvent.MOUSE_DOWN,callButton3);
mcButton3.addEventListener(MouseEvent.CLICK,mclick);
//button4
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton4.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,button4Text);
mcButton4.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
mcButton4.addEventListener(MouseEvent.CLICK,mclick);
//button5
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton5.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,button5Text);
mcButton5.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
mcButton5.addEventListener(MouseEvent.CLICK,mclick);
// FUNCTIONS
function mclick(e:MouseEvent):void {
toolio = true;
e.currentTarget.gotoAndStop(5);
e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT,mout);
e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER,mover);
settext = mcToolTip.toolTip.text;
if (e.currentTarget !== mcButton2) {
mcButton2.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton2.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton3) {
mcButton3.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton3.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton4) {
mcButton4.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton4.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton1) {
mcButton1.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton1.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton5) {
mcButton5.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton5.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
}
function mover(e:MouseEvent):void {
stopPlayReverse(e.currentTarget as MovieClip);
e.currentTarget.play();
var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
}
function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
if ( toolio == false ) {
var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
}
if (settext != undefined) {
mcToolTip.toolTip.text = settext;
}
}
function playReverse(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
} else {
mc.prevFrame();
}
}
function stopPlayReverse(mc:MovieClip):void {
if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
mc.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
function button1Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 1"; }
function button2Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 2"; }
function button3Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 3"; }
function button4Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 4"; }
function button5Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 5"; }
function callButton1(evt:MouseEvent):void { ExternalInterface.call("button1", urlButton1);}
function callButton2(evt:MouseEvent):void { ExternalInterface.call("button2", urlButton2);}
function callButton3(evt:MouseEvent):void { ExternalInterface.call("button3", urlButton3); }
function callButton4(evt:MouseEvent):void { ExternalInterface.call("button4", urlButton4);}
function callButton5(evt:MouseEvent):void { ExternalInterface.call("button5", urlButton5);}
First thing, it appears you have "Automatically declare stage instances" checked in your settings. I avoid this like the plague. Follow the directions here: under "disabling stage instance auto-declaration." Unchecking that box will force you to declare your other stage instances, like mcToolTip and the mcButtons. But it will help in future development efforts (trust me.)
Anyway, I figured this would be your document class since you said it was used per swf. I named it MyFirstClass, you probably want a better name. You need to chance "BOTH" instances of that name... one in the class signature "public class MyFirstClass extends MovieClip" and the second in the constructor (the function with the same name as the class) which gets run as soon as the class is "instantiated"
Put this file in the same directory as your .fla. Add "MyFirstClass" as the document class.
Read up on more info about how to write your own class: here
package {
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.external.ExternalInterface;
impprt flash.display.MovieClip;
public class MyFirstClass extends MovieClip {
public var toolio = false;
public var settext;
public var urlButton1:String = 'URL STRING 1';
public var urlButton2:String = 'URL STRING 1';
public var urlButton3:String = 'URL STRING 1';
public var urlButton4:String = 'URL STRING 1';
public var urlButton5:String = 'URL STRING 1';
public function MyFirstClass():void {
mcToolTip.toolTip.blendMode = BlendMode.LAYER;
mcToolTip.toolTip.text = "";
mcButton1.stop();
mcButton2.stop();
mcButton3.stop();
mcButton4.stop();
mcButton5.stop();
mcButton1.buttonMode = true;
mcButton2.buttonMode = true;
mcButton3.buttonMode = true;
mcButton4.buttonMode = true;
mcButton5.buttonMode = true;
// EVENT LISTENERS
//button1
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton1.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,button1Text);
mcButton1.addEventListener(MouseEvent.MOUSE_DOWN,callButton1);
mcButton1.addEventListener(MouseEvent.CLICK,mclick);
//button2
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton2.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,button2Text);
mcButton2.addEventListener(MouseEvent.MOUSE_DOWN,callButton2);
mcButton2.addEventListener(MouseEvent.CLICK,mclick);
//button3
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton3.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,button3Text);
mcButton3.addEventListener(MouseEvent.MOUSE_DOWN,callButton3);
mcButton3.addEventListener(MouseEvent.CLICK,mclick);
//button4
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton4.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,button4Text);
mcButton4.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
mcButton4.addEventListener(MouseEvent.CLICK,mclick);
//button5
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton5.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,button5Text);
mcButton5.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
mcButton5.addEventListener(MouseEvent.CLICK,mclick);
}
// FUNCTIONS
public function mclick(e:MouseEvent):void {
toolio = true;
e.currentTarget.gotoAndStop(5);
e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT,mout);
e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER,mover);
settext = mcToolTip.toolTip.text;
if (e.currentTarget !== mcButton2) {
mcButton2.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton2.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton3) {
mcButton3.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton3.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton4) {
mcButton4.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton4.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton1) {
mcButton1.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton1.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
if (e.currentTarget !== mcButton5) {
mcButton5.addEventListener(Event.ENTER_FRAME, playReverse);
mcButton5.addEventListener(MouseEvent.MOUSE_OUT, mout);
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
}
public function mover(e:MouseEvent):void {
stopPlayReverse(e.currentTarget as MovieClip);
e.currentTarget.play();
var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
}
public function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
if ( toolio == false ) {
var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
}
if (settext != undefined) {
mcToolTip.toolTip.text = settext;
}
}
public function playReverse(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
} else {
mc.prevFrame();
}
}
public function stopPlayReverse(mc:MovieClip):void {
if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
mc.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
public function button1Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 1"; }
public function button2Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 2"; }
public function button3Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 3"; }
public function button4Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 4"; }
public function button5Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 5"; }
public function callButton1(evt:MouseEvent):void { ExternalInterface.call("button1", urlButton1);}
public function callButton2(evt:MouseEvent):void { ExternalInterface.call("button2", urlButton2);}
public function callButton3(evt:MouseEvent):void { ExternalInterface.call("button3", urlButton3); }
public function callButton4(evt:MouseEvent):void { ExternalInterface.call("button4", urlButton4);}
public function callButton5(evt:MouseEvent):void { ExternalInterface.call("button5", urlButton5);}
}
}
I like to generalize as much as possible. You did a good thing by calling the buttons and movieclips with numbers after them, but you're missing out on one of the advantages to doing that which is that you can now generalize event handlers.
This is what I came up with by editing the code sberry2A posted:
package {
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.external.ExternalInterface;
import flash.display.MovieClip;
public class MyFirstClass extends MovieClip {
public var toolio = false;
public var settext;
public var buttonSettings :Object;
public function MyFirstClass():void {
buttonSettings = {
1: {
url: "http://example.org",
tooltip: "Some text here"
},
2: {
url: "http://example.org",
tooltip: "Some text here"
},
3: {
url: "http://example.org",
tooltip: "Some text here"
},
4: {
url: "http://example.org",
tooltip: "Some text here"
},
5: {
url: "http://example.org",
tooltip: "Some text here"
}
};
mcToolTip.toolTip.blendMode = BlendMode.LAYER;
mcToolTip.toolTip.text = "";
initializeButtons();
addEventListeners();
}
// FUNCTIONS
public function initializeButtons():void {
for(var i:String in buttonSettings) {
this["mcButton"+i].stop();
this["mcButton"+i].mouseChildren = false;
this["mcButton"+i].buttonMode = true;
this["mcButton"+i].useHandCursor = true;
}
}
public function addEventListeners():void {
for(var i:String in buttonSettings) {
this["mcButton"+i].addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
this["mcButton"+i].addEventListener(MouseEvent.MOUSE_OUT,handleMouseOut);
this["mcButton"+i].addEventListener(MouseEvent.CLICK,handleMouseClick);
}
}
public function addPlayReverseListenerButSkip(skip:String = null):void {
for(var i:String in buttonSettings) {
if(skip && skip == i) { continue; }
this["mcButton"+i].addEventListener(Event.ENTER_FRAME, playReverse);
}
}
public function handleMouseClick(e:MouseEvent):void {
toolio = true;
e.target.gotoAndStop(5);
e.target.removeEventListener(MouseEvent.MOUSE_OUT,mout);
e.target.removeEventListener(MouseEvent.MOUSE_OVER,mover);
settext = mcToolTip.toolTip.text;
var targetNumber :String = e.target.name.split("mcButton").join("");
addPlayReverseListenerButSkip(targetNumber);
if(ExternalInterface.available) {
ExternalInterface.call("button" + targetNumber, buttonSettings[targetNumber].url);
}
}
public function handleMouseOver(e:MouseEvent):void {
var mc :MovieClip = e.target as MovieClip;
stopPlayReverse(mc);
mc.play();
var targetNumber = e.target.name.split("mcButton").join("");
setTooltipTextTo(targetNumber);
var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
}
public function handleMouseOut(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
if ( toolio == false ) {
var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
}
if (settext != undefined) {
mcToolTip.toolTip.text = settext;
}
}
public function stopPlayReverse(mc:MovieClip) {
if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
mc.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
public function playReverse(e:Event):void {
var mc :MovieClip = e.target as MovieClip;
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
}
else {
mc.prevFrame();
}
}
public function setTooltipTextTo(targetNumber:String):void {
mcToolTip.toolTip.text = buttonSettings[targetNumber].tooltip;
}
}
}
This code isn't tested, but with a little debugging, you should be able to get this working like the code above.