actionscript process time delay? - actionscript-3

I wanted to know how i would be able to make a tween wait a few extra seconds before it is carried out? MY CODE BELOW:
btn_1.addEventListener(MouseEvent.CLICK, about_navigate);
function about_navigate(event:MouseEvent):void
{
topbarTween.yoyo();
//how to make the below tween wait 2 seconds before it is carried out
btmTween.yoyo();
}
I am new to AS3 so any help would be much appreciated!

I don't think the Native Tween class has a built in delay method (like most third party solutions do - Though someone please correct me if I'm mistaken).
What you can do is use flash.utils.setTimeout or a Timer.
function about_navigate(event:MouseEvent):void
{
topbarTween.yoyo();
setTimeout(btmTween.yoyo,2000);
}

Related

How to replace Mutiple Timers with TweenLite delayedCall or ENTER_FRAME event?

Hey everyone so I have a lot of timers in my game to be specific around 8 timers. They all control different Movie clip objects that appear on the stage at different times. I also change the timers in my difficulty update function. Now I have read a lot to understand that Timers do cause lag and decrease the performance. I am creating this game using AS3 Adobe AIR for Android devices. My game seems to freeze for half a second every second which I believe is do to the timers as well as the garbage collector. Either way I was wondering if I remove all these timers and instead replace them with TweenLite TweenLite.delayedCallfuncion if it would dramatically increase performance. I Have tried this on one of my old timers that i removed and replaced with the tweenlite function and it seems to be working just fine but not sure if this is the correct way of doing it here is how i have it set up in my constructor:
TweenLite.delayedCall(6.0, addWatch);
and the addWatch function:
private function addWatch():void
{
TweenLite.delayedCall(6.0, addWatchTimer);
var newWatch:mcWatchTimer = new mcWatchTimer();
stage.addChild(newWatch);
aWatchTimerArray.push(newWatch);
//Start screen sound
watchSoundChannel;
watchSound = new watch();
watchSoundChannel = watchSound.play(0, 9999);
}
this seems to loop it without me having to attach an ENTER_FRAME Eveent listener to it. But not sure if this would be wise since I want to be able to change the delayedCall in my difficulty update to a faster time interval.
Any feedback on the situation would be appreciated. Hope I made enough sense.

functions in actionscript (addChild)

I've Just started learning actionscript 3.0, and I got a problem.
I just want a symbol to be in the stage for many times ,not in a same time, but it brings them all in at the beginning. I just used this piece of code below:
for (i=1,i<10,i++){
stuff();
}
function stuff():void {
theSymbol.x=(Math.random()*100);
addChild("theSymbol");
}
Is there any other ways to :
Bring the symbol into the stage many times "not in a same time" ,in a while.
Call a function many times without using "for".
Thanks for any Ideas .PLZ answer soon as possible!
;)
I believe you wanna do something like:
setInterval(stuff, 1000) // call stuff() once every second.
function stuff():void {
theSymbol.x=(Math.random()*100);
addChild("theSymbol");
}
Does it helps you?

Animating from within classes in Actionscript 3 (Not on the timeline), what's the best way?

I've found some stuff online about how to animate in actionscript 3 from within a class, but haven't been able to find a really good tutorial. I want to control the animations from a class because at some point I intend to move from the flash IDE to using flash develop, where I won't have access to the Flash IDE's timeline.
I have to be able to control an initial animation (opening a bag) which joins onto an animation loop (searching through a bag).
The only way I have been able to do this so far is to add an event listener to listen for the initial animation's final frame. Then when initialAnimation.currentFrameLabel = "Last" then I gotoAndStop("animationLoop").
This has been working fine, if a bit time-consuming. I'm just wondering if there's a better, easier way to do it? Can anyone tell me or point me towards a tutorial that does it better? Thanks very much!
Romano
I recommend instead of using an event listener, you use the method addFrameScript. Essentially you can fire a method when a specific frame number is reached.
Read the following question for more information.
actionscript3 whats the point of addFrameScript
It depends on what it is you want to do:
Usually if you are working together with an artist or want to do animations that are non-code driven, the "best way" is usually to listen for something to happen, and then start animations and on last frame of animation (or when you want to return control to code) you create an event, or use a callback or something else to let code notify that animation is complete or reached a certain point.
If you want to do something from code, the easiest way is to use an external animation library.
Tweener (https://code.google.com/p/tweener/)
TweenLite (http://www.greensock.com/tweenlite/)
Using those libraries, you would write something similar to:
function fadeOut():void {
mc.alpha = 1;
Tweener.addTween(mc, {alpha:0, time:0.275, delay:1, onComplete:onDone});
}
function onDone():void {
trace("Animation finished");
}

How to get rid of lag caused by lots of enemy instances?

im making a flash shooter game and ive encountered one problem. When there are a lot of monsters on the stage which are visible by player, the game starts to lag. In my opinion, its due to Event.ENTER_FRAME (each enemy instance has it) where z-sorting, enemy movement, updating other stuff like health is done. Since things like theese, cant be done each second or at similar time interval,im using ENTER_FRAME. My question is, how can i have many instances of the enemy in my game and still dont have it lagging. Ive done optimising in all over the code and if im not mistaken,big ammount of enemies is the performance bottleneck here.
Question me if i wasnt clear; to see the game go to http://ernyz.lhosting.info/bandymas.html or if you want to see the code,i will be able to put it here,just ask :)
Having an enter frame events for each instance is most likely the problem. A single event where you loop over all instances and do actions is usually faster.
There shouldn't be much for you to change: Instead of adding the listener to each enemy, add only one listener to the stage and call the enemies' update functions.
class Enemy {
function update(e:Event) { /* ... */ }
}
class Main {
function onEnterFrame(e:Event) {
for each (var enemy:Enemy in enemies) {
enemy.update(e);
}
}
}
From my experience, unless you're doing something very wrong, flash rendering pipeline is what takes the most time of your application. And since you get more enemies, you get more MovieClips and more complex rendering.
But having one ENTER_FRAME event for each object is indeed a big overhead that can be easily avoided.
A good practice before optimizing your code is to actually run it through a profiler. I don't believe the actual Flash program has it, but Flash Builder surely does. If you post us a screenshot or a log of the game being profiled, we can be of more assistance.
By quickly playing your game, I've seen that all your enemies are a bunch of graphics with a bunch of gradients, therefore costly to render. Have you tried setting the quality to low? Does the lag go away?

Going from Flash 8 to CS3

After many years of using Flash 8, I'm moving to CS3 at work. I know I'll have to learn AS 3.0 so, does anyone have any good references or summaries of the major/most noticeable changes? Also, are there any tips/tricks for the flash environment? After spending a few minutes in CS3, I noticed that you can't directly attach actionscript to a button, which is new to me. Any other such pitfalls to watch over?
I made the total switch just about 3 months ago, here are some things that helped me ramp up rather quickly:
1) Do everything in Class files
A lot of AS3 tutorials out there deal with just code pasted on the timeline (which I can't stand because now you have to hunt for what import you need), but is fine for quick tiny stuff. In the long run it's way better work primarily in Class files. Learning how Classes work opened a huge door for me, it was the same feeling/experience I had when I first discovered Functions in AS2 :)
2) Keep graphics in library and off the workspace
Example, you have a jpg, gif, png file you just imported into your library. Made a movieClip and gave it a class name(MyButton). Now the code below will place the graphic into the workspace for you:
var myButton:MovieClip = new MyButton();
myButton.x = 6;
myButton.y = 22;
myButton.buttonMode = true;
addChild(myButton);
3) Get use to the new button code in AS3
It's something all of us new converts had to deal with painfully, but now it's a piece of cake :)
myButton.addEventListener(MouseEvent.MOUSE_UP, clickThis);
function clickThis(event:MouseEvent):void
{
navigateToURL(new URLRequest("form.html"), "_self");
//navigateToURL(request, '_self');
}
4) Make sure you remove Event Listeners after use
It took me a bit to wrap my around this one... remove em why? Oh they are still running in the background and when I listen again I'll get all kinds of mutated errors.
private function volDown(e:MouseEvent):void
{
masker.width = volControl.mouseX;
userVolume = (masker.width / 100) * 1;
volControl.addEventListener(MouseEvent.MOUSE_MOVE, volMove);
}
private function volUp(e:MouseEvent):void
{
lastVolPoint = masker.width;
setVolume(userVolume);
e.updateAfterEvent();
volControl.removeEventListener(MouseEvent.MOUSE_MOVE, volMove);
}
5) Don't forget to pass Events
I'm not a programmer by trade and this has caused so much grief, I'm glad I'm done with this birthing pain:
myButton.addEventListener(MouseEvent.MOUSE_UP, clickThis);
Since the clickThis function is launched via an Event, you have to pass: event:MouseEvent into it like so:
function clickThis(event:MouseEvent):void
Because the code below will throw the dreaded AS3 "Access of undefined property" error that new AS3 guys will always run into.
function clickThis():void
6) Read and post questions on StackOverflow... a lot!
btw I'm still a noob and originally a designer then AS2 dev, I still don't know why we put :void behind a function name.. if we have similar coding backgrounds I hope all that helps :)
I suggest you to look at the ActionScript language migration page on the Adobe devnet. It offers quite a lot articles about the key changes with ActionScript 3.
To answer your problem with the actions on a button, this no longer works (and was already with ActionScript 2 not the best way to do it). AS3 requires the code to be centralized on the timeline. So for giving a button some action, you'll need to give it an instance name and add an event listener for the CLICK event, like this:
function doSomething ( event:MouseEvent ):void
{
trace( "test" );
}
myButton.addEventListener( MouseEvent.CLICK, doSomething );
Get an Actionscript 3 IDE. Such as Flash Builder, FlashDevlop, or FDT. This will force you to learn really fast.