Keep a Bitmap on top at literally all times - actionscript-3

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);

Related

Global Event Listener

Is there any way by which I can make my button work, no matter in which scene or frame it is.
Is there any way by which i can call that Event Listener to another frame for that particular instance?
for example:
I have a button home_btn, I want this button to work in all scenes, without changing it's instance name. I already added an Event listener in first scene, but it doesn't work,
In another frame or scene.
Below is the code.
home_btn.addEventListener(MouseEvent.CLICK, process_it);
function process_it(event:MouseEvent):void
{
nextFrame();
}
I don't know how to use dispatch event function for my button.
// This only works for that particular frame.
Since you're doing this in the timeline, if you put the home_btn.addEventListener(MouseEvent.CLICK, process_it);
on the first frame of your main timeline, it will be registered even if you change to a different frame.
You can keep the function in its existing frame as that should still be called.
The reason why this works as opposed to the code being on a different frame is because frame 1 is where your timeline will start and process all the display items and code needed during this frame. That is any function, variable, or listener you want active will be running and/or stored into memory at the start of the application since it will always start on frame 1 until specified otherwise. If you have the listener on a different frame then you will have to gotoAndStop() to that frame to add the listener ( otherwise you are specifying that you don't want to add the listener until your application is at the frame ). Adding a listener to a display object on frame 1 is contingent upon the display object also being on that frame.

Deleting everything from the stage AS3

So, I am wanting to clear my whole stage. I already searched through the internet, and unfortunately nothing has worked for my situation.
Basically, what I am doing is a somewhat complex maze generator and before I create a new one, I want to get rid of everything I created prior to that. So far, I hear that the best way to remove movieclips from the stage is buy using:
while(numChildren > 0)
removeChildAt(0);
However this only works for the current movieclip I call it in, which doesn't include the maze I generated. I just want to get rid of absolutely everything.
Any ideas on how to do this?
You're thinking along the right lines, you can use numChildren and removeChildAt however you need to call them in the scope of the stage:
while(stage.numChildren > 0)
{
stage.removeChildAt(0);
}
To just remove it from the stage:
stage.removeChildren();
Just removing clips from stage isn't always equal of removing them from memory
removeChildren, removeChild or removeChildAt does not actually remove an Sprite or any other DisplayObject from memory, it only removes it from the displaylist. That means if you create 1000 sprites and add them to the stage (displaylist), and then use removeChildren they could still exist in memory (forever). Then you have a memory leak.
To remove it from memory, all objects with a relation to the displayObject should be set to null. This includes event listeners and relations from / to non-displaylist related objects.
If you want to be sure all related stuff should be gone, just null it and check these things:
Remove it from the displaylist using removeChild or removeChildAt or removeChildren. (note this can be done from the stage)
Remove all eventListeners that are attached to the clip, or use weak event listeners.
If you used a reference in an Array, Vector, Dictionary or any other object, remove it from the object, set it to null or splice it using Array.splice()
setTimeout/setInterval should be cleared
Set the object = null
You can profile the memory with Mr Doob stats or performance stats from the Temple Library. You should see a drop (garbage collection) after a while when removing all clips. After removing multiple times the memory indicator should not be higher.

ActionScript 3 Removing All RESIZE Event Listeners

I'm working on a Flash project which is separated into separate scenes.
In Scene 1 I have multiple MovieClips (which include event listeners for RESIZE (and others) inside them).
In Scene 2 I have a few common MovieClips and new ones (which also include event listeners for RESIZE (and others) inside them).
After clicking a button from Scene 1 to go to Scene 2, it's fine, except for if I resize the stage and then I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I know it's related to the event listeners, but it would be unrealistic to remove them each individually as it's expected there will be many.
If I undertand your situation correctly, I think you will in the end need to remove each listener individually, or add the resize listener only once. Since you mentioned scenes, am I right to assume you are working on the timeline? I also am assuming the null object reference error comes from a scene that has been removed from the stage, making reference to a display object that is no more, a ref to the stage after the scene has been removed, or just calling a function (the resize handler) on an object that no longer exists.
Some way to deal with this are:
Add some checking in the listener handler functions
if (!this.stage) return
To avoid the errors, but will not help if the object the function is a method of has been removed.
To avoid needing remember to remove hundreds of listeners, create removeAllListeners and addCustomEventListener functions. Instead of the usual addEventListener, call you addCustomEventListener which in turn will call addEventListener. Have addCustomListener store the target, listener function and event string in a dictionary or array of objects. removeAllListeners can loop through the dictionary or array and remove all your listeners. It is a bit like setting up an event hub, but does not go quite that far.
Instead of adding the RESIZE event listener to each scene, add it only once. Then in the listener function call a function on whichever scene is the active scene or view.
This last one is the approach I have seen most often, and is the most bullet proof. It may be tricky to implement on the time line, I have always been a little hazy on timeline variable scope.
Yes, so far as I know there is no good automated way to do this, however it would be a good practice to create a registerAllListeners and a removeAllListeners methods that manually add and remove the appropriate listeners to your object.

Tweening in as3 stopping the next tween to happen until the last one is finished

How to stop the next tween action to start until the previous tween completes playing in as3.0?
and also i want to stop the tween to happen on the same object twice.
Basically i have a container (movie-clip) in which there are n number of movie-clips (arranged as bricks). When i click on the container the target (brick) will disappear (made scaleX and alpha to 'o'). also i am tracking how many bricks are closed.
But the problem is if i do a fast double click the tween seems to happen twice. and the count also seems to increase for the same brick. how to solve this problem.
Tween after Tween
Use an event handler to wait for the first tween to complete, Something like
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
... // Call the next tween here
}
Double click problem,
Remove the event listener for the button click as soon as it is clicked, Something like:
my_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent){
my_btn.removeEventListener(MouseEvent.CLICK, onClick);
myTween.start();
}
You could use Tweener and use
isTweening() or autoOverwrite = false to get around this.
I haven't used those properties/functions in a while but I'm sure something in Tweener can do this easily.
Any reason you are using Tweener. Greensock's Tweenmax/lite package is seemingly the industry standard these days.
The problem you seem to be having is that you are initiating the tween multiple times. If all the blocks need to shrink simultaneously, i would probably use a for loop and run a tween on each of them.
The thing you have to make sure though is once its clicked to remove the click event handler until its complete so it won't be clicked again.
I also would recommend using TweenMax/Lite because it has something called TimelineMax/Lite where you can compound a bunch of tween be it simultaneous or staggered and listen for one COMPLETE event on that Timeline object. When that event fires you can reactivate the clicks or do whatever it is you need to do after the animation is complete.
TimelineMax has some nice features as well like reverse and timeScale.
would you be able to paste some code? that would also help us help you.

Getting started with a simple Flash game

I'd like to make a simple game where "discs" fall from the top of the screen and the user must catch them. I have a MovieClip which I want to resize to one of three randomly chosen sizes.
As I see it, there are four things that must be done.
Create and size the MovieClip
Position the MovieClip
Make the MovieClip fall
Determine when it is finished "falling" and see if the user has "caught" it.
My question is: How do I create, size and position the MovieClip? I've given it an identifier of "disc". Now what? Do I make an ENTER_FRAME event and do my creation there? How do I move the disc downwards? Do I use tweens, something else?
I'm primarily asking this as a sanity check.
I would use some kind of factory class that would be responsible for dropping random discs from the top of the stage.
Beside what you correctly mentioned, you will also need to:
define if the speed for falling is constant or not, you may need some acceleration tween. To move the objects downwards, you could use a native tween method, you will need to apply it to every disk that gets dropped.
define where the disk will start falling, it can be random or always from the same place.
you can find out if two objects are colliding using the AS3 hitTestObject method which belongs to the DisplayObject class.
you factory class could have a start() and stop() method. Once start() is fired, an infinite or ENTER_FRAME loop is started and disks start falling. If you want to create disks at a specific rate you could combine your loop with a timer to run code on a defined interval. For instance, every 3 seconds create 10 disks (using main loop) and drop them onto the stage.
Assuming that you have MovieClips named 'disc' & 'userHand' exported into actionscript, I'll summarize it in the foll way:
Generate the no of discs & Randomize their location. Start with something like:
var n:int = 30; //Total no of Discs
for(i:int=0;i<n;i++)
{
var mc:disc = new disc();
mc.x=Math.random()*stage.width(); //to scatter the discs across the stage
mc.y=-mc.height; //initially hide out a disc
addchild(mc);
}
Also add the Movie clip 'userhand' to stage.
Add enterframe handler function.
Fill in the enterframe handler to update (keep increasing) the y position of each disc.
Use hitTestObject() in the enterframe handler to determine hit among each 'disc' & 'userHand'.
Reset position of all the 'disc' clips which fall off the screen to random positions as initially.
You might want to look at programming particles.
http://r3dux.org/2010/01/actionscript-3-0-particle-systems-3-rain-effect/
At a very high level what you would do is.
You need to create a disc class.
You could give this class some variable properties like width, height,x etc.
In an enter frame in your main class you would add an enterframe function that creates new instances of disc passing it random values for each property.
Each instance of disc could also have its own entframe which increases its y position until it reaches the bottom of the screen. The disc would then remove its self from the stage. You could use an easing function passing it a random number to determine the rate at which its falls.
Say if its y position is greater than the stage height, remove the disc. If the user catches it (using a hit test maybe) also remove it.
It really recommend having a look at that link I posted.