Why does the main document extend sprite in AS3? - actionscript-3

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.

Related

as3 - how to define instance child from base class?

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.

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
{
...
}

Bring Timeline animation to top

I have a movie that adds a MovieClip to it's stage in the constructor, i Also have an animation on the timeline that plays on certain events. Everything is working, except the movie I need the movie on the timeline to be the top layer, it is on the bottom currently.
public Class BallCollision extends MovieClip{
public function BallCollision(){
mcBall = new MovieClip();
stage.addChild(mcBall);
//Adds stuff to the movie clip
}
}
You can do one of the following:
Create a container on the timeline called 'container' and then add mcBall to that instead. This container will be on a layer underneath the existing animation.
Place all of the existing animation into a MovieClip and give it an instance name like animation. Whenever you add something to the stage, also use stage.addChild(animation) to bring it back to the top.
Obviously option 1 is preferable, but I've offered option 2 for the sake of free knowledge.

Putting a movieclip as child for another movieclip

So I have the MovieClip mc1 which added to the stage (addChild) is put on top of all the layers. I actually need another movieclip to be on top of all the scene so I've created an empty movieclip with the instance name fbp, that is set on the appropriate layer, in which I want to put my mc1 movieclip. How can I get that fbp movieclip and then add mc1 as child?
fbp.addChild(mc1). However, you might need to make a Sprite instead of MovieClip for fbp. Not sure if it works for movieclips.

How to add/get Sprite to/from UIComponent

How to add Sprite to BorderContainer (UIComponent)?
var sprite:Sprite = new Sprite();
sprite.graphics.lineStyle(10,0);
sprite.graphics.moveTo(40,40);
sprite.graphics.lineTo(60,60);
mybordercontainer.addChild(sprite);
//mybrodercontainer is id of BorderContainer created in mxml
This code doesnt work. I cant see Sprite on my BorderContainer. How can I add Sprites on UIComponents, so I can see them? I tried this and it kinda worked:
var comp:UIComponent = new UIComponent();
comp.addChild(sprite);
myborderconteiner.addElement(comp);
But I dont think, that this is a right way to add Sprites to UIComponents. Is there another method to do that?
Second problem:
When I have few Sprites added to my UIComponent (lines/circles/images or others) how can I receive an object Sprite from that UIComponent, which is containing all Sprites added before to that UIComponent?
I need to create Bitmap from that Sprite and do some things.
I hope I make myself clear
Use the SpriteVisualElement as container. That should serve nicely as a container or Sprite substitute. You could also draw in the UIComponent itself.