Flash AS3 | Removing or Stopping a Function - function

I need the answer to what seems like it should be a very obvious question, yet I can't find it anywhere:
How do you stop a function in Flash AS3? For basic example, when I push a button, the function starts working. When I press the button again, the function stops.
More specifically, how can I tailor this to an if/boolean (or similar) statement so that when/while the variable is true, the function starts working. Once the variable goes false, the function stops. When it goes true again, the process repeats.
The code in issue is this panning function that another user helped create for me.
if(Zoomed == true) {
MovieClip.y = stage.stageHeight / 2;
MovieClip.x = stage.stageWidth / 2;
addEventListener(MouseEvent.MOUSE_MOVE, panFunction);
function panFunction(me: MouseEvent): void {
MovieClip.x = stage.stageWidth / 2 - (stage.mouseX - stage.stageWidth / 2);
MovieClip.y = stage.stageHeight / 2 - (stage.mouseY - stage.stageHeight / 2);
}
}
As you can see, I want it so that when the boolean function 'Zoomed' is true, the function activates and the panning feature works. The problem is that when I go back to Zoomed == false, the function is still running. I am looking to find out how to stop this from happening.
I have tried a 'while' loop but it ultimately just freezes the animation (likely because its a moving image and it's trying to render every frame with this code).
Summarising: I need a way for a function to be able to be toggled on and off. Alternatively, being able to add it and remove it when it's needed/not needed should work the same. Any help on this would be a great help, thanks!

What you need to do is remove the event listener when you no longer want panning.
Something like this, which assumes you change the value of zoomed on a click:
var zoomed:Boolean = false; //a var to store whether we are currently zoomed
//change the zoom on mouse click
addEventListener(MouseEvent.CLICK, zoomClick);
function panFunction(me:MouseEvent): void {
mc.x = stage.stageWidth / 2 - (stage.mouseX - stage.stageWidth / 2);
mc.y = stage.stageHeight / 2 - (stage.mouseY - stage.stageHeight / 2);
}
function zoomClick(me:MouseEvent):void {
zoomed = !zoomed; //toggle the zoomed value
if(zoomed){
//we're zoomed, so add the panning mouse move listener
addEventListener(MouseEvent.MOUSE_MOVE, panFunction);
//do whatever else you need to when first zoomed.
}else{
//no longer zoomed, remove the mouse move listener
removeEventListener(MouseEvent.MOUSE_MOVE, panFunction);
}
}
Some notes, the word MovieClip is not a good instance name, as it may conflict the class name MovieClip, I'd recommend renaming it mc or something else to avoid potential problems later on.
Also, avoid inline functions (functions declared/contained within other functions), they can be especially problematical when attached to event listeners and are a good way to introduce memory leaks into your program.

Related

How to use event Dispatch in actionscript 3.0

I tried using the event dispatch but unable to get clear view of this one, so tell me if anyone know this.
This is Sample Code but i can't understand could you explain elaborate.
var ball:Shape = new Shape();
ball.graphics.beginFill(0xFF0000);
ball.graphics.drawCircle(0, 0, 30);
ball.graphics.endFill();
addChild(ball);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
addEventListener("myCustomEvent", myCustomEventListener);
function mouseMoveListener(event:MouseEvent):void
{
dispatchEvent(new Event("myCustomEvent"));
}
function myCustomEventListener(event:Event):void
{
ball.x = stage.mouseX;
ball.y = stage.mouseY;
}
Think about stage as a big box where magic happens. Stage knows when you resize it, when you press keyboard buttons or when you move mouse on top of it. It does extend EventDispatcher which means it can broadcast stuff (and it does!). You usually don't pay attention to it, but in this particular piece of code you have:
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
function mouseMoveListener(event:MouseEvent):void
{
dispatchEvent(new Event("myCustomEvent"));
}
What's going on in here? You're curious about MOUSE_MOVE event and that's why you add listener. Now every time you move your mouse over the stage, stage is going to broadcast (dispatch) event (like : "HEY! Mouse just moved!"). And every time that happens your mouseMoveListener would be called. You can add trace(stage.mouseX, stage.mouseY); inside it and it would trace your mouse position as you move it. You could also move this code
ball.x = stage.mouseX;
ball.y = stage.mouseY;
to that function and your ball would follow mouse cursor.
However what you're doing is:
dispatchEvent(new Event("myCustomEvent"));
Which basically means that you're broadcasting (dispatching) an event now. What event? "myCustomEvent". Now anyone who is interested might listen for it. And somebody is indeed:
addEventListener("myCustomEvent", myCustomEventListener);
function myCustomEventListener(event:Event):void
{
ball.x = stage.mouseX;
ball.y = stage.mouseY;
}
So you're basically listening for this even in the same class and when this even it being dispatched you execute myCustomEventListener function.
Hope that's gonna make it a bit more clearer for you :)

How to stop an effect from one frame to continue in the next?

I have made a program in which small green rectangles (or Greeny here) are periodically generated at the bottom of the screen and inside the movie clip symbol, I have made a classic tween such that it moves to the top of the screen. I have also declared a variable (t) that increments itself periodically (I am running it at 24 fps). When the value variable, t reaches or exceeds 96, it moves to the next frame. The problem however is that even in the next frame, the generation of these small green rectangles do not stop. Please do excuse me if I have asked the question wrongly. By the way the code on frame 2 is just stop();. Here is the code for frame 1-
var c:int;
var t:int = 0;
var s:int = 8;
function eFrame(event:Event):void
{
t++;
if (t%s == 0)
{
var i:Greeny = new Greeny ;
i.x = Math.random() * 550;
i.y = 400;
stage.addChild(i);
}
if (t > 96) {
nextFrame();
}
}
this.addEventListener(Event.ENTER_FRAME, eFrame);
stop();
EDIT - Here is the link for the file - http://www.mediafire.com/download/crjh2fubcbnx3l5/Retro.fla
you have to remove your your event listener on frame 2. Try this (on frame2 or in frame 1 when your condition t>96 is reached):
this.removeEventListener(Event.ENTER_FRAME, eFrame);
If you want to stop the generation of green rectangles, you should remove the listener of Event.Enter_Frame, or do something to stop it in function eFrame.
If you want to make all rectangles not visible, you should set theirs "visible" attribute false or remove them from stage .

AS3 - Make the screen flash for half a second

What I want to do is:
After colliding with an [object], I want the screen to flash for about half of a second. I have tried for loops and while loops but they seem to not work. I have no idea how I should program this.
I've been trying to figure out how to do this since I'v been making the game so it would be helpful if someone could help me.
Thank you for reading.
You need to use something that involves time. loops all run in a thread which doesn't pause for time - which is why they don't work.
Here is how you could do this with an AS3 Timer (let's say this code runs right after you've determined there's been a collision)
function flashScreen():void {
var timer:Timer = new Timer(50, 10); //run the timer every 50 milliseconds, 10 times (eg the whole timer will run for half a second giving you a tick 10 times)
var flash:Shape = new Shape(); //a white rectangle to cover the whole screen.
flash.graphics.beginFill(0xFFFFFF);
flash.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
flash.visible = false;
stage.addChild(flash);
timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
//we've told AS3 to run this every 50 milliseconds
flash.visible = !flash.visible; //toggle visibility
//if(Timer(e.currentTarget).currentCount % 2 == 0){ } //or you could use this as a fancy way to do something every other tick
});
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
//the timer has run 10 times, let's stop this flashing madness.
stage.removeChild(flash);
});
timer.start();
}
Other ways you can do this are with setInterval, setTimeout, a Tweening library,and an ENTER_FRAME event handler.

How to make Movie clip stretch and contract to follow another Movie clip on stage

Hey everyone so I am working on a fishing pole and I created a hook in AS3 that the user controls when they touch the screen the playerHook is allowed to move anywhere on the screen in any direction that the user directs it to.
So what i want is for the hookLine to be attached to the hook and follow it everywhere also the hookLine to be attached to the fishing pole as well, but i want the hook line to of course act like one, in the way that when the user moves the Hook to the end of the screen then the hook line stretches with it and when it is moved back, the hook line contracts. Hope you understand what I'm trying to convey. Just basically how a fishing pole acts.
Here is the code that I am using for the user to control the playerHook :
In my Main Function:
//Add hook to stage
playerHook = new mcHook;
stage.addChild(playerHook);
playerHook.x = (stage.stageWidth / 2);
playerHook.y = (stage.stageWidth / 2);
//Event listeners
playerHook.addEventListener(Event.ENTER_FRAME, playerHookMove);
In my playerHookMove function:
private function playerHookMove(e:Event):void
{
var xCoord = playerHook.x - mouseX;
var yCoord = playerHook.y - mouseY;
playerHook.x = playerHook.x - (xCoord / 3.5);
playerHook.y = playerHook.y - (yCoord / 3.5);
}
If anyone has any idea on how to have the hookLine object do what I stated above i would really appreciate it. I have ideas such as scaling and transforming. But not too sure. Please help!

Flash CS4, frame head position following the mouse

I have a small script that moves the frame head backwards and forwards depending on where my mouseX coordinate is inside the main flash movie. Here's my code and it's working so far:
function onEnterFrame(event:Event):void
{
var frame:Number = Math.round(mouseX / (stage.stageWidth / animation.totalFrames));
animation.gotoAndStop(frame);
}
However, when the mouseX cursor leaves the Flash window at x=0, the left edge of the movie, and the mouse reenters the Flash window at stage.stageWidth, the right edge of the movie, the entire movie jumps/jerks to the last framehead.
Now this is sort of what is desired, but I want to soften the effect of moving from ex. frame 0 to frame 30.
So instead of popping to 30, there should be a smooth transition. Can anyone suggest how to manipulate the above function to enable that kind of behaviour!
Thanks in advance!
You could use an easing equation, for example:
var finalFrame:uint=0;
function onEnterFrame(event:Event):void {
var frame:Number = Math.round(mouseX / (stage.stageWidth / animation.totalFrames));
finalFrame+=(frame-finalFrame)*0.2; //simple easing
animation.gotoAndStop(finalFrame);
}
Or you could even use a tweening engine for a smoother transition...
The final solution:
function onEnterFrame(event:Event):void
{
var frame:Number = mouseX / (stage.stageWidth / animation.totalFrames);
var newFrame:Number = animation.currentFrame + ((frame - animation.currentFrame) / 3);
animation.gotoAndStop(Math.round(newFrame));
}
Whew!!!