as3 - how to define instance child from base class? - actionscript-3

I have a movieclip parent that has movieclip instances inside. However the movieclip parent extends a baseclass, and I would like to define one of the children by using instance name and defining from the base class.
for example, if there was a child with an instance name of "player" inside of the movieclip parent.
from the movieclip parent class you would just define it as
player
//or
this.player
but from the base class, this method would not work. How would I define it from the base class?

You can define player in your base class, but you have to disable "Automatically declare stage instances" in your AS3 settings. When you have the option checked the compiler blindly tries to add all named instances as properties of the linked class. If you turn it off, you can control where those properties are actually defined. For this reason I always turn this feature off.
Example:
class Base extends MovieClip {
public var player:MovieClip;
}
class SymbolClass extends Base {
// linked to a symbol that contains a 'player' instance
}
If you have "Automatically declare stage instances" checked, you can't do this because SymbolClass will try to automatically declare player and it will conflict with the Base player declaration.
Also note that if your symbol does not contain a player instance, the code is still valid but player will be null.

Related

Why does the main document extend sprite in AS3?

What is the purpose/benefits of this extending sprite:
public class Main extends Sprite
The document class is added automatically as a child of the Stage when your app starts. In consequence it needs to be a DisplayObject. It also need to be a DisplayObjectContainer so that DisplayObject can be added to its display list.

AS3 how to set initial stage size

How do I set the initial stage sizing (height/width) if I'm developing in pure AS3 utilizing the FlashDevelop tool?
You can right click your project and go to properties to set the dimensions there or you can put the following in your main class declaration:
[SWF(width="1280", height="800", backgroundColor="#000000", frameRate="30")]
public class Main extends Sprite
{
...
}

Children of Children of MovieClip not showing up when MovieClip is added to stage?

This problem seems very simple to me, but I've been unable to fix it, or find an answer anywhere.
This is in a class constructor for a class called block, block_maker is the object that called the constructor, an instance of level.
this.bitmap = new Bitmap(this.bitmap_data);
this.addChild(this.bitmap);
this.block_maker.stage_foreground.addChild(this);
In level, stage_foreground is added to the stage, but nothing appears. trace(stage_foreground.numChildren); shows the correct count of children, and var temp = (this.stage_foreground.getChildAt(0)); trace(temp.numChildren); correctly but the children OF the children don't actually show up, the stage just stays blank.
When I change the above code to
this.bitmap = new Bitmap(this.bitmap_data);
this.block_maker.stage_foreground.addChild(this.bitmap);
the blocks appear on the stage, as children of level_instance.stage_foreground, but with this method, the bitmaps aren't appropriately positioned, as they have no position data. I can simply give this.bitmap x and y positions, and it works, but I am curious as to why it won't work when just adding the bitmap as a child to the block and then adding that as a child to stage_foreground.
I've tried replacing this.bitmap with a number of other object classes, such as a temporary MovieClip I made, or a Shape, but nothing shows up, so I know it has nothing to do with it being a Bitmap.
As you stated
This is in a class constructor for a class called block, block_maker
is the object that called the constructor, an instance of level.
this.bitmap = new Bitmap(this.bitmap_data);
this.addChild(this.bitmap);
this.block_maker.stage_foreground.addChild(this);
The level class needs to to extend a display object for it to show up.
In other words
"this"
has to be a display object or extend it in some form.
The reason this doesn't work.
this.block_maker.stage_foreground.addChild(this);
and this does
this.block_maker.stage_foreground.addChild(this.bitmap);
Is because "this" is not a display object but "this.bitmap" is.
The DisplayObject class is the base class for all objects that can be placed on the display list.
First of all, even though it's not neccesary, I'd suggest to make a classname start with a capital letter, and vars always start with a lower-case letter. It's a common 'rule' in AS3 that's mainly to avoid confusion.
The problem most likely lies within you use of this; what you're doing is adding this into this. It seems to me it's not a proper way of coding.
Also, since all of the other attempts didn't work; have you tried to make the var you want ot add a public static var?

Movieclip stacking in Actionscript

I'm building a game of which the interface is one of the first items to load on screen. Sound button, pause and all the bits -
During the game - all manor of things are dynamically added and removed to the stage. Therefore my interface goes behind my game scene.
How do I ensure the movieclip always stays on top?
Can I override the addChild of my Document Class and every time a new child is added, I restack the interface to the top?
You can use setChildIndex to rearrange objects that are already contained within a MovieClip or Sprite. For example, if you have an object called container, which contains a redBall, blueBall and many other ball objects, you can use the following to make sure redBall is behind everything else:
container.setChildIndex(redBall, 0);
Equally you can make sure that blueBall will be displayed infront of everything else using:
container.setChildIndex(blueBall, container.numChildren-1);
addChildAt will sort you out for adding children straight into their correct position, and setChildIndex will allow you to re-position objects already placed. Hope that helps.
debu
Look into addChildAt, it allows you to specify the index that the new object should be added too.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#addChildAt%28%29
A good strategy is to keep all interface elements in one parent Sprite/MovieClip, and all game assets in another. (With the interface one on top)
With your guys suggestion I made this, apparently, if you addChild an object already on the screen, it's simply reindex'd to the top. Does this look ok?
private var topLevelChildrenArray:Array = [];
public function addTopLevelChild(child:DisplayObject):DisplayObject
{
topLevelChildrenArray.push(child)
return this.addChildAt( child, this.numChildren - 1 );
}
override public function addChild(child:DisplayObject):DisplayObject
{
this.addChildAt(child, this.numChildren);
for each(var topChild:DisplayObject in topLevelChildrenArray)
{
super.addChild(topChild);
}
return child
}

AS3 Accessing Variables of Parent Class From Child

i'm trying to assign a parent's variable from the parent's child
//Parent
public class Main extends Sprite
{
public var selectedSquare:Sprite;
public function Main()
{
//inits and adds new Square child class to display list
}
...
-------
//Child
public function dragSquare(evt:MouseEvent):void
{
Sprite(parent).selectedSquare = this; //evil doesn't work!
parent.addChild(this);
this.startDrag();
}
i'm receiving this error, but i'm casting parent from displayObjectContainer to a Sprite so i have no idea why it's not working.
1119: Access of possibly undefined
property selectedSquare through a
reference with static type
flash.display:Sprite.
You should cast parent as a Main rather than a Sprite, since a Sprite won't have any references to a "selectedSquare". If Main were to extend MovieClip, this wouldn't be a problem, since MovieClips can have dynamically created references.
Proposed modification to child function:
public function dragSquare(evt:MouseEvent):void
{
(parent as Main).selectedSquare = this;
parent.addChild(this);
this.startDrag();
}
Another reason that this may not work is that you're attempting to use the parent property right before adding the child to the parent's display list.
Sprite(parent).selectedSquare = this;
parent.addChild(this);
That second line is what worries me. In this code, the current object (this) must already be added as a child to the parent object (Main) for the first line to work properly. So, either the current object is not yet a child of the parent object, in which case you need to revise your code.
Or, the second line is unnecessary (because this is already a child of Main, that's why this.parent, or just parent, works as expected).
I believe, though, that your code is probably set up well. You just don't need that second line, as it's completely redundant.
I hope that helps! Let me know if you want me to clarify anything.
(This is, of course, assuming you didn't already know all of this and aren't doing some sort of insane, arcane, weird magic with the redundant addChild call. You never can tell with magicians!)