How to repaint the display in ActionScript 3 - actionscript-3

Can anybody say to me how to refresh the display in ActionScript e?
The correspondent from Java:
pane.repaint();

The DisplayObject system of Flash/Flex is a "retained" or "stateful" system. This means that if you add MovieClips or other DisplayObjects onto the stage, they will stay there in whatever position until you deliberately change or remove them.
This is the opposite of a system like what DirectX or OpenGL have where you have to purposefully redraw every frame, even if nothing has changed, to keep objects alive.
If you want to "update the display" in Flash then this will vary depending on whatever it is you did to put the display items on the screen in the first place. You might need to erase or add some, move them about, change the frame they're on.
If you have correctly moved or changed items on the screen, the screen will automatically update to indicate this. There is no need for a repaint call as with Java.
I'm sorry for the vague answer, but you are the architect here. Whatever it was you did before, you need to do it again. :p

Related

To reset game, will I need to add objects dynamically?

Good evening/ morning.
I am creating a game in As3 where the enemies are added not added dynamically.
if(hero attacks enemy)
{ then parent.removeChild(enemy) }
When the hero attacks the enemy the enemy removes it self from the display list I presume.
but when you reset the level, i.e go back to the start menu and go back to the level you will see the enemies are not in the level, since they've been removed.
My question is, is there a way I can reset the display object in that frame, in As2 I refreshed the flash movie. But that's not good in terms of coding as it shows you are not developing or learning.
IF there is no way, will you suggest adding the objects that will be removed, dynamically?
another question is that my level is in a container.
//on the stage, there is a movieclip called container.
//In this container mc(movie clip) it contains the whole level, including platforms,
//enemies and props
IF I remove a prop from that container, in order to reset the game will I do this
container.addChild(prop)
//the question is that how do I set it's x and y position?
Thank you, I will appreciate every ones feedback and advice.
Yes, as you remove them dynamically, you need to add them dynamically again in order to reset them. You either predefine their positions and set them again, or you can just hide those enemies and make them visible once again the game resets. Depends on your code and structure.
Ideally, instead of removing them from the display list, your best bet is to hide then and put them and reset their position back to their original point.
A good practice to get into is to avoid the chance of runaway instantiation if at all possible; this means, if you're going to instantiate and remove enemies every time they're spawned and killed, the garbage collector has to work very hard to keep up. A better approach would be to have a pool of enemies that is as large enough to support as many enemies as you would ever need at one time.

Moving character on a specific stage Actionscript 3

I'm making a game with ActionScript 3.0. There's is one thing I can't figure out:
I have a big circle on the background. On this circle I want to let a character randomly move. But this character can only move on this circle, so it can't go to the background. Where do I need to start with this, how can i specify a specific stage like this?
P.S. I can't upload a picture yet, I've not enough "reputation points", otherwise I would have uploaded some pictures to get my question more clearly.
A general idea around which you can evolve is using hitTestPoint() method on your stage using current player's coordinates as the point being tested, and if this method returns false, this means the player has left the stage, and you need to put him back. Using previous location is the failsafe method, but you might also invent some other more smart way to do this.
The manual on hitTestPoint()

What does the garbage collector when you have a movieclip into one frame, and your movie is constantly ending and returning to beginning?

I already know all the problems in programming based on timeline, but I'm curious to know what happens to garbage collector in that situation.
Talking about code in timeline - as a movieclip isn't accessible if the code isn't in the same frame as it, does this raises the possibility of memory leak?
Seems to me that the movieclips in timeline are added when the "playhead" currently is on it's frame, and removed when it goes out.
The Garbage Collector will not be able to clean/dispose a movie clip removed from stage if:
that clip is still playing and does stuff (create/remove other things...)
that clip is still referenced by some other instance that is "reachable"
you try to remove a way too complicated hierarchy containing too many items [I dont have any number for this, but I'd guess around 2^32 items?]
One way I usually use to see if there is a memory leak is to run SWF Profiler in FlashDevelop and force GC to run at certain situations and see what gets deleted.
Please check following article for more information:
http://active.tutsplus.com/tutorials/workflow/quick-tip-understanding-garbage-collection-in-as3/
The code is still apart of that MovieClip. So it is still there. And since the MovieClip is presumed still attached to its parent then it stays.
(I say attached to its parent instead of on screen, because its possible to have parent reference the child, and the child references the parent with the .parent variable, so since they both have something pointing to them, they stay in memory).

Dynamic UI window drawing in Flash?

Wasn't sure if this question belongs here, but I'll try. So I'm about to move my project from Director | Shockwave Player (if you ever heard of these) to Flash Player for numerous reasons and while I'm thinking how to better start off I got a question which really made me wonder. To the point.
Currently in Director each game window in the user interface (like small alerts or large windows with lot of elements in them) is drawn upon need - meaning that the actual window's graphical image is being put together from configuration (all sorts of properties like width, height and all elements and their properties) from numerous pieces of graphics (e.g. window background is made from 9 small pieces like the 4 corners, 4 middle pieces between corners for dynamic width and height and 1 central piece to fill the window) and then added to stage. This approach makes it easy to edit each graphical element without having to redraw the actual windows and everything in them if we would like to change the color scheme or improve something in a element. It also saves the resources as windows are being drawn only when requested.
Now I got to figure out if it's worth writing such code in Flash too as opposite to just creating all the windows and placing them in the library and adding to stage when they are requested. What do you think? Is it worth writing such implementation?
IMO, it depends on 1) How comfortable you are with Flash's drawing API / graphics class, and 2) How flexible each window/dialog needs to be.
If it's easier to just throw them together as static objects -- and if they don't necessarily need the flexibility to change dimensions/style much (can you count on one hand how many times they've needed to change since it was originally done in Director many years ago?), it's obviously easier to do this than going through the time/energy to recreate them dynamically, especially if you're not very comfortable with Flash's drawing API.
That said, a lot can be accomplished dynamically with Flash's drawing API, so if you have the time/interest I'd certainly suggest digging in and doing it the "right" way if you want to familiarize yourself with the drawing API.
My method for doing this sort of thing usually goes like this:
Create a separate class that extends Sprite/MovieClip; something like 'Dialog.as'.
Create public init(), show(), and hide() methods, and a private build() method.
init() is called just once upon initialization of your app, and takes some global parameters to hold on to internally (padding, colors, etc).
show() takes an argument of either text (a String), or even a Sprite/MovieClip -- whatever it is you want to show in this dialog. When called (when you want to spawn a window), it uses this -- plus whatever init parameters were originally passed in during init() -- to draw itself, and then unhides itself (tween the .alpha property, or simply set the .visible property).
When you want to close the dialog, make sure to invoke the hide() method, which first hides itself, and then cleans up whatever was created (removing listeners, etc) so that the next time it is called upon it can draw itself fresh.
It might also make sense to pass into this object a reference to the Stage (in the original init() call), so that it can center and size itself accordingly on the stage, and also addChild itself to the top of the display list so that it's always above everything else.
I hope this helps.
If is 2D you can check this:
http://pushbuttonengine.com/
if is 3D you can start here:
http://alternativaplatform.com/en/alternativa3d/
I can suggest you also Unity3D but this is outside of flash
Hope it helps

Howto dynamically render space background in actionscript3?

I'm creating a space game in actionscript/flex 3 (flash). The world is infinitely big, because there are no maps. For this to work I need to dynamically (programatically) render the background, which has to look like open space.
To make the world feel real and to make certain places look different than others, I must be able to add filters such as colour differences and maybe even a misty kind of transformation - these would then be randomly added and changed.
The player is able to "scroll" the "map" by flying to the sides of the screen, so that a certain part of the world is only visible at once but the player is able to go anywhere. The scrolling works by moving all objects except for the player in the opposite direction, making it look like it was the player that moved into that direction. The background also needs to be moved, but has to be different on the new discovered terrain (dynamically created).
Now my question is how I would do something like this, what kind of things do I need to use and how do I implement them? Performance also needs to be taken into account, as many more objects will be in the game.
You should only have views for objects that are within the visible area. You might want to use a quad tree for that.
The background should maybe be composed of a set of tiles, that you can repeat more or less randomly (do you really need a background, actually? wouldn't having some particles be enough?). Use the same technique here you use for the objects.
So in the end, you wind up having a model for objects and tiles or particles (that you would generate in the beginning). This way, you will only add a few floats (you can achieve additional performance, if you do not calculate positions of objects, that are FAR away. The quad tree should help you with that, but I think this shouldn't be necessary) If an object having a view leaves the stage, free the view, and use the quad tree to check, if new objects appear.
If you use a lot of objects/particles, consider using an object pool. If objects only move, and are not rotated/scaled, consider using DisplayObject::cacheAsBitmap.