everyone! I wonder is it bad practice.
In this case I pass the container to TicTacToe class,TicTacToe pass the container to MainMenu class,MainMenu class pass the container to Engine and etc. Engine pass to GameOver class and GameOver pass to TicTacToe the same sprite on play again.And that all display objects are added to this Sprite container.
Main(){
var container:Sprite = new Sprite();
var game:TicTacToe(container);
addChild(game);
}
TicTacToe(containerPar:Sprite){
this.container = containerPar;
MainMenu(this.container);
}
Is this a good practice?
The other way that I think is like tell of almost all my classes like Engine and MainMenu to extend sprite and I think is not a good idea.
Is there any other practices to add display objects to DisplayObjectContainer wich is added to stage ?
I want to know because i think it is very important and there is not much information on the internet.Thanks to everyone!
The syntax seems a bit messed up, it doesn't show too well what you are trying to achieve.
There are many ways you can reference a Sprite, or anything else, from everywhere.
You can use a singleton class, or a static variable for example.
Lets say you have a general Game class. In the class itself you can have a static variable named "container" that references the sprite you want. You can retrieve it with a getter or with a static method. So that you could, anywhere:
import Game;
var container:Sprite = Game.container;
// or
var container:Sprite = Game.getContainer();
this makes overall better OOP and cleaner code.
I hope this helps.
Related
I am writing a class that extends mx.core.UIComponent. In the constructor of this class, I would like to add a Button (spark.components.Button). However, creating a new Button object (such as var b:Button = new Button();) and then doing this.addChild(b) doesn't work -- no button is appearing. Is there any reason why a Button object can't be added as a child?
Edit: Just to add, when I print out the children, it says that the Button object is a child, but the Button doesn't show up.
You need to add the button in the createChildren method not in the constructor.
protected var button:Button;
override protected function createChildren():void {
super.createChildren();
button = new Button();
addChild(button);
}
Depending on what you are trying to do you may also need to implement the measure and updateDisplayList methods.
Using UIComponent directly, you aren't getting the layout of the child handled for you; you would need to set the position and size of your child components manually (see docs).
Consider using a Group (or Canvas, if you're still on MX components) to handle child layout. You can still add rendering in updateDisplayList, just make sure you call super.updateDisplayList() as well.
You need to read up on the differences between ActionScript components and Flex components. When using a Flex component, you must use container.addElement( element ), not container.addChild( child ). As far as I am aware, the only classes with this functionality descend from Group (at least in the Spark framework, the one I am most familiar with). So extending UIComponent will not allow you to add a Button, although a UIComponent can be added to a Flex container (which I do not believe a normal Sprite can do)
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.
Here is the code to make an MC the exact size in even proportions of whatever your choice. I choose 777 for this example.
my_mc.height = 777; // Can be anything you want.
my_mc.scaleX = my_mc.scaleY; /// This makes it the same proportions.
Now the questions is how can I tween this?
I'm afraid you have to tween both x and y scale values.
Other scenarios can be tricky.
Another alternative would be to tween an arbitary property of the movieclip, and letting a frame-based function (called on ENTER_FRAME event) that reads this variable and updates both scale values.
OR, making a custom movieclip class and let its internals handle a custom scaleXY property.
function tweenThis(newHeight:uint):void{
var oldHeight:uint=my_mc.height;
var difValue:Number=newHeight/oldHeight;
var newWidth:uint=my_mc.width*difValue;
new Gtween(my_mc,1,{width:newWidth,height:newHeight});
}
I need to display a child on layer 2. How would I, using AS3, dynamically create a child on frame 2?
"Layer 2" isn't specific enough. What layer of what display object container?
Here is a good article on display list programming that should prove enlightening.
To answer your question, you would identify the parent displayObject and call its addChild method, with your targeted child displayobject as the parameter. If your parent displayObject is the containing class (a class that extends DisplayObject, a Sprite, for example), you can just call addChild() or this.addChild(). To add the child at a layer other than the topmost, you can use addChildAt().
var someclip:Sprite = new Sprite();
var someOtherClip:Sprite = new Sprite();
var yetAnotherClip:Sprite = new Sprite();
var someLibrayClip:LibraryClip = new LibraryClip();
this.addChild(someClip);
this.addChildAt(someOtherClip,0);
someOtherClip.addChild(yetAnotherClip);
someOtherClip.addChildAt(someLibrayClip,0);
etc...
Note that the display list is a stack like an array, and in its case, may not contain empty indexes. If you want something in layer 2, there must also be items in 0 and 1.
Hope that helps -
Layers only exist in the Flash IDE. They are not part of Flash Player's display list system. So you can't specify what layer a child goes into. Use addChild() or addChildAt() to add children to containers.
Create a symbol in the library with nothing in it. I normally call these empty.
Place an instance of this symbol on stage where you need it. Give the instance a name. For sake of this answer, it is empty_mc.
Then in AS do
var whatever : Whatever = new Whatever();
empty_mc.addChild(whatever);
at the appropriate place.
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
}