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

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.

Related

ActionScript: Separating child from parent on Stage

I'm primarily a C++ programmer trying my hand at Flash and ActionScript3 for game development. I'm finding myself a little confused/constricted by the whole Parent/Child relationship, and was wondering if anybody here could give a little insight.
I'm currently making an Astro Panic clone, where each EnemyShip object also contains an array of EnemyShots. My problem is this: when one of the players' shots collides with an EnemyShip, the ship is supposed to disappear. However, at the moment, if the shot collides with one of a ship's shots, this also causes the entire corresponding EnemyShip (as well as all it's previous shots) to disappear. On top of that, it seems that the entire area between an EnemyShip and it's shot(s) trigger a collision. For example, in the following image, if the player's shot reaches anywhere within the red rectangle, the game will register it as the enemy having been hit:
Obviously I can't have this, but I'm unsure how to deal with it. Is there any way to separate child objects from parents on the stage, or is that just not how ActionScript works?
If I were you I would create a clip for all the shots. This means there would be 3 principal clips: the player, the enemies and the shots. This way you could achieve shot-ship and shot-shot interactions.
I hope this helps.
You should separate the model (Ships, Shots, Enemies) from their actual representation. Although a shot might be associated with a ship, the visual representations might share the same parent.
So basically I'm trying to say that you can flat the display hierarchy.

Is it better to hide or remove MovieClips that aren't on the screen?

I'm working on a program in Flash coded in Actionscript 3 and I'm trying to determine if I should remove MovieClips from the stage when they're not on screen, or just stop moving them once they're outside the viewing area. If I do not removeChild() the MovieClips once they move out of the viewing area, I can potentially end up with tens of thousands of MovieClips on the stage (though not in view).
Is there a standard recommendation for hiding vs. removing MovieClips when not in view?
Basically, I'm trying to find out:
Does having MovieClips on the stage but outside of the viewing area use a lot of resources?
How much of a penalty does removing and adding MovieClips to and from the stage incur?
Is there an estimate of how many MovieClips you are working with before you should switch from one method to the other?
Thanks!
--EDIT--
In my program, MovieClips will often have to reappear on the screen. This means that when I remove them from the stage, they might have to be added back. If I hide them off-screen, they might have to be moved back on-screen. This is why I don't just removeChild() everything and null out the object.
Does having MovieClips on the stage but outside of the viewing area
use a lot of resources?
Your MovieClip uses the same amount of memory whether it is inside or outside of the viewing area.
How much of a penalty does removing and adding MovieClips to and from
the stage incur?
Removing/nulling is cheap, creating new object is much more expensive.
Is there an estimate of how many MovieClips you are working with
before you should switch from one method to the other?
If you are dealing with hundreds or thousands of instances have a look into object pooling.
There is also a nice video tutorial about object pooling. It is worth watching if you want to know more about destroying/creating display objects.
If you do not intend to reuse the movieclips, you should definitely remove them. The action is costless and it frees the memory of your movieclip. Having a hidden movieclip uses the same amount of memory, it is just easier on the display process (your application won't lag but this is a major memory leak).
It's a good idea to remove them if they are not being used. If it's an item that moves (say at an x speed of 5px/s), even once it's left the screen the item is still being processed and is moving farther and farther away.
Basically if it's not in use and serves no purpose then remove it to save on resources.

AS3 adding and removing objects, is it a good idea?

In AS3 I create a number of playing cards through a class that extends a sprite.
I then add and remove these to stage throughout the game.
I was wondering, is this the right way to do it? Is it processor intensive to keep adding and removing objects?
I suppose that when you say "adding and removing objects" you mean adding and removing to/from the stage.
It is more processor intensive to create and destroy the objects over and over than to add and remove to/from the stage. So I'd say that you're safe.
However, an alternative to adding and removing to the stage is to change the visible propety of your sprites. This is even less processor intensive and could be a suitable solution for you. Note that when an object is invisible, it's still there. It just doesn't show. See this question on StackOverflow: visible property of DisplayObject

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

How to repaint the display in 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