ActionScript - Tween With Zero Duration? - actionscript-3

i have a tween function which fades in a display object over time.
when i set my tween duration to 0 nothing happens. isn't the tween suppose to animate in 0 seconds (or frames) to emulate no animation at all?
new Tween(this, "alpha", None.easeOut, 0.0, 1.0, 0, true);

I had a look at the source code for the Tween class. There is a simple conditional in it where it checks to see if the time passed is greater than the duration. If it is, then it dispatches the MOTION_FINISH event, stops any timers, removes listeners to ENTER_FRAME and that is it. There is no code to then set the target object to the finished state.

Related

Adobe Flash CS3 - "do something" only when playhead hit a defined keyframe

I would like to know how to create in Adobe Flash CS3 a button that will execute a function (like gotoAndPlay(51) but ONLY when playhead hit a defined keyframe from timeline.
Here is a drawing that does explain better what I want to do
So after I click the button, it will wait until the playhead hit the closest defined keyframe, only then it will execute my desired function.
Any help is appreciated.
There are a few ways to accomplish this. The most succinct way is to use an undocumented* feature called addFrameScript
What you can do with that method, is place code on a specific frame at runtime.
So, you could do something like:
//listen for the click on your button
myButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
//which frame should the gotoAndPlay run on?
var targetFrame:int = 19; //this is actually frame 20, since frames are 0 based in script
//this function will run on the above frame
function goto51(){
//goto frame 51
gotoAndPlay(51);
//remove the frame script
addFrameScript(targetFrame, null);
}
function btnClickHandler(e:Event) {
//use addFrameSCript to run the function goto51 on the target frame
addFrameScript(targetFrame, goto51);
}
*-one should point out that using undocumented features of a language comes with risk of the feature being taken out on a future version, however given the current life cycle stage of AS3, this is very unlikely

Actionscript 3, walk cycle loop during tween motion

I have a MC character, inside of it there's a walk cycle frame by frame animation
My character move on the stage with a tween (used tweenLite from GreenSock) wherever you click with the mouse.
I want the walk cycle loops till the end of the tween, actually it iterates only one time, any advice?
If you're using TweenLite, you can its onComplete callback to repeat your tween.
e.g.
TweenLite.to(mc, { onComplete: function(tweenliteInstance):void{
tweenLiteInstance.play();},
onCompleteParams : ["{self}"] }
);

Make the first tick of a timer instant(ignore the millisecond delay)

I have function where a series of buttons are created, tween for a second to their position in quick succession and then the timer that triggers the tween ends.
I experimented with the delay/time settings on the tween and couldn't get the desired effect without a timer class since I want each new button to begin it's tween before the previous one finishes.
I almost have it looking how I want now using a timer, but the delay at the start is still a problem. Is there a quick way to skip the delay for the first fire of the timer, or will I need to go the long route and create additional timer/tween/functions to simulate this effect?
One solution would be to just call the method without the whole timer thing. Take a look at this example:
// Here, we create the timer
var t:Timer = new Timer(200);
t.addEventListener(TimerEvent.TIMER, onTimer);
Let's say that some event happened, like a button press. Upon pressing the button a space-ship in a game starts to shoot, but you want the first bullet to fly out right away, and not wait for the 200ms delay:
t.start(); // make sure t is a class member, not a local variable
shoot();
This way the timer will start, but the first action will happen right away ;)
Just for clarity, the timer handler could look something like this:
private function onTimer(e:TimerEvent):void
{
shoot();
}
As you see, there is no need to put the implementation of shooting right into the handler function to preserve it's re-usability and better maintainability.
Simply as a cool thing to know, you can actually change the delay of your timer on run-time.
Here's a small example:
// Creating the timer with 2 handlers
var t:Timer = new Timer(200);
t.addEventListener(TimerEvent.TIMER, onTimerSetNewDelay);
t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();
The implementation of onTimer() doesn't matter, but onTimerSetNewDelay can be interesting:
private function onTimerSetNewDelay(e:TimerEvent):void
{
t.delay = YOUR_NEW_DELAY_VALUE;
t.removeEventListener(TimerEvent.TIMER, onTimerSetNewDelay); // removing the listener so it doesn't set the same delay over and over again.
}
what about calling the function using onTimer(null);?

Keep a Bitmap on top at literally all times

I've got a timer event which seems to once in a while call itself before the ENTER_FRAME event handler which I made to force a Bitmap object to be top-most at all times by adding itself to the stage again.
The timer event fires every 50 milliseconds.
I tried setting the priority of the ENTER_FRAME event to 1, which reduced the ugly flickering, but did not remove it.
How can I force this Bitmap object to be top-most at literally all times with no flickering whatsoever?
Create two container MovieClips at the root of your application. Put all of your other display objects in the lower one and the Bitmap in the higher one. It saves you having to run a loop at all.
Alternatively, whenever you add something to the stage, instead of using addChild, use addChildAt(newChild,getChildIndex(bitmap)-1);

stage Event.ENTER_FRAME vs. CREATION_COMPLETE

I've got an AS3 project that I'm building in FlexBuilder.
In my main class's constructor, I've got this:
stage.addEventListener(Event.ENTER_FRAME, stage_enterFrameHandler);
And then the event handler function:
private var tempCounter:uint = 0;
private function stage_enterFrameHandler(event:Event):void
{
stage.removeEventListener(Event.ENTER_FRAME, stage_enterFrameHandler);
tempCounter += 1;
trace(tempCounter);
}
When I run in debug mode, tempCounter writes out once, as 1. Why is this? I though the whole point of Event.ENTER_FRAME is that it keeps firing.
The documentation says:
If the playhead is not moving, or if there is only one frame, this event is dispatched continuously in conjunction with the frame rate. This event is dispatched simultaneously to all display objects listening for this event.
So why would I not see that counter incrementing about 30x a second (which is what I have the frame rate set to)?
EDIT NOTE:
OK, well, doh, I've figured it out. It's because I immediately remove the eventlistener. The ENTER_FRAME event does keep firing... The only reason the code is done this way (code I inherited) is, I suppose, that CREATION_COMPLETE isn't available if you aren't using the flex framework.
The answer is simple as soon as enter in your enter frame you are removing the listener so the next time the enter frame event occured there is no one to react to it.
remove the line
stage.removeEventListener(Event.ENTER_FRAME, stage_enterFrameHandler);