AS3 function running before variables are defined! - actionscript-3

I am trying to add an init() function to a MovieClip, but when I run the function from scene1 the variables that were set in the MovieClip are not defined yet... The MovieClip was dragged to the stage from the library.
scene1:
mc.init(null);
MovieClip:
var _default = 5;
function init(num) {
if(num == null) {
trace(_default);
} else {
trace(num);
}
}
This is tracing "undefined" instead of "5"; Is there a way of fixing this problem?

The problem is that any code thats placed directly in the main timeline will always run before the code thats directly in the MovieClip.
The way to get around this would be to let flash finish running that code in both timeline and the MovieClip first, and then call the function from the timeline after its done.
The easiest way to do this would be to use an event listener:
Timeline:
addEventListener( Event.ENTER_FRAME, onEnterFrame );
function onEnterFrame( e:Event ):void {
myObject.init(null);
removeEventListener( Event.ENTER_FRAME, onEnterFrame );
}
That way the timeline will wait until the first frame has started to call the init function of your MovieClip.

Related

Play timeline in reverse when holding a button

Basically i'm trying to create two buttons. They both call actions while the user has their cursor held on them (rather than onClick or anything like that).
The first one plays the timeline, which works great.
The second one I need it to play the timeline in reverse while the user is holding down the button, how do I do that? Code below:
import flash.events.MouseEvent;
// Play timeline forwards on click //
function onButtonClick( event:MouseEvent ):void
{
play();
}
RotateRight.addEventListener(MouseEvent.MOUSE_DOWN, onButtonClick);
// Stop timeline forwards on release //
function onButtonClick2( event:MouseEvent ):void
{
stop();
}
RotateRight.addEventListener(MouseEvent.MOUSE_UP, onButtonClick2);
// Play timeline in reverse on click //
function onButtonClick3( event:MouseEvent ):void
{
// PLAY IN REVERSE?
}
RotateLeft.addEventListener(MouseEvent.MOUSE_DOWN, onButtonClick3);
// Stop timeline in reverse on release //
function onButtonClick4( event:MouseEvent ):void
{
stop();
}
RotateLeft.addEventListener(MouseEvent.MOUSE_UP, onButtonClick4);
Any help is most appreciated :)
You should add an ENTER_FRAME event on MOUSE_DOWN:
RotateLeft.addEventListener(MouseEvent.MOUSE_DOWN, onButtonClick3);
function onButtonClick3( event:MouseEvent ):void
{
stop();
stage.addEventListener(Event.ENTER_FRAME, reverse);
}
function reverse(e:Event):void {
//currently, this is reversing the main timeline. Can also do it to a specific movieclip with something like myMovieClip.gotoAndStop(myMovieClip.currentFrame-1) instead.
gotoAndStop(currentFrame - 1);
}
RotateLeft.addEventListener(MouseEvent.MOUSE_UP, onButtonClick4);
// Stop timeline in reverse on release //
function onButtonClick4( event:MouseEvent ):void
{
stage.removeEventListener(Event.ENTER_FRAME, reverse);
//You don't need stop() here, but you can change it to play() if you want to resume playback when the mouse is released.
}

AS3 - Access MovieClip from stage within a class

I have been stuck on this for a long time and have looked at past questions on here about this similar issue such as this post: How do I access a movieClip on the stage using as3 class?.
I use the constructor to listen for the ADDED_TO_STAGE event, and then initiate the main function to set up the eventListeners from the ADDED_TO_STAGE handler. Within the same handler I also try to get a MovieClip from the stage using the following code:
player = stage.getChildByName("player") as MovieClip;
player is defined globally as a MovieClip.
Within another handler (after the class has been added to the stage) I set the player to go to an particular frame label using player.gotoAndStop("jump");. However an output warning shows up saying "Cannot access a property or method of a null object reference".
Here is the code which I use:
public var player:MovieClip;
public function PlayerControl():void {
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(event:Event):void{
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
player = stage.getChildByName("player") as MovieClip;
}
private function enterFrameHandler(event:Event):void{
if(up == true && moviePlaying == false){
player.gotoAndStop("jump");
moviePlaying = true;
}
}

Action Script 3: Problems with gotoAndStop() after a Movie Clip

Upon the click of a button, an animation starts. Then the program directs you to a certain frame when the animation is done.
Is this possible?
So this is what I've got so far: a Movie Clip movQuizIntro and a Button btnBond in Frame 1.
stop()
movQuizIntro.stop()
btnBond.addEventListener(MouseEvent.CLICK, BondQuiz)
btnReg.addEventListener(MouseEvent.CLICK, Registrering)
function BondQuiz (evt:MouseEvent)
{
if (currentFrame == 1)
{
movQuizIntro.alpha = 1
movQuizIntro.play()
}
}
What is the code and proper syntax you need to write in order to go to frame 2 after the animation is done?
`
stop();
movQuizIntro.stop();
int frameCounter=0;
btnBond.addEventListener(MouseEvent.CLICK, BondQuiz);
btnReg.addEventListener(MouseEvent.CLICK, Registrering);
function BondQuiz (evt:MouseEvent)
{
if (currentFrame == 1)
{
movQuizIntro.alpha = 1
movQuizIntro.play()
movQuizIntro.addEventListener(EventType.ENTER_FRAME, onEnterFrame);
}
}
// event handler function, runs every enter frame
private function onEnterFrame(event:Event):Void
{
frameCounter++;
if(frameCounter > movQuizIntro.totalFrames)
{
//Place code here because you know the MovieClip finished playings
//Go to desired frame
}
}
`
I wrote this code outside of an editor nor did I get to compile, so the gist is there and may have some minor errors.
NOTE:This is just a quick way of doing this. If you want something more reusable/cleaner then you would want to consider subclassing or alternate Object Oriented tricks.
In button event handler:
function onClick(e:MouseEvent):void{
ANIMATION_MC.addEventListener(Event.EXIT_FRAME, onFromeExit);
}
function onFrameExit(e:Event):void {
if (ANIMATION_MC.currentFrame == SOME_FRAME) {
ANIMATION_MC.removeEventListener(Event.EXIT_FRAME, onFromeExit);
TARGET.gotoAndPlay(NEW_FRAME);
}
}
And you can just use addFrameScript on ANIMATION_MC too.

Multiple hittest AS3

I have a draggable movieclip on my stage. When it hits another object/movieclip the word: "hit" appears. So far, so good. But, I want the draggable movieclip to hit multiple other objects with each an different reaction (like loading an other movieclip or something).
This is the code I have for the hittest part:
import flash.events.Event;
this.addEventListener( Event.ENTER_FRAME, handleCollision)
function handleCollision( e:Event ):void
{
if(blok3.hitTestObject(schaap))
{
output_txt.text = "HIT"
}
else
{
output_txt.text = ""
}
}
SCHAAP is the draggable object, blok3 is the object that trigger the word "hit" when the draggable object hits it.
I hope you guys can help me out!
You already have a trigger and an output for one collision (if blok3 htis schaap, output "HIT") so a simple extension would be to add another if statement below the existing:
function handleCollision( e:Event ):void
{
if(blok3.hitTestObject(schaap))
{
output_txt.text = "HIT"
}
// else if statement to check for collision on second object
else if(differentBlok.hitTestObject(schaap))
{
output_txt.text = "SMASH"
}
else
{
output_txt.text = ""
}
}
From here you can look at storing objects in arrays and looping through the array to check for a collision, rather than having a separate if statement for every one.

Play movieClip before initialize

So I have a custom preloader with 200 frames and the corresponding in Flex:
gotoAndStop(Math.ceil(e.bytesLoaded/e.bytesTotal*100));
So basically each procent is one frame in the movieClip. So when 100% the movie ends and application initializes.
How can I say for example so that when 100% don't start the app but play from frame 100-200 in the movieClip and then initialize the app?
Thanks,
How about adding an event listener when loading is complete, then displaying the MovieClip frame one after another from 100-200. When that is done, you can dispatch the complete event.
private var _currentFrame:int;
private function initComplete(e:Event):void
{
_currentFrame = 100;
addEventListener( Event.ENTER_FRAME, onEnterFrame );
}
private function onEnterFrame( e:Event ):void {
if ( _currentFrame < 200 ) {
_currentFrame++;
cp.gotoAndStop( _currentFrame );
} else {
removeEventListener( Event.ENTER_FRAME, onEnterFrame );
dispatchEvent(new Event(Event.COMPLETE));
}
}