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.
Related
I have made a game using Flash CS5 (as3) and I am trying to add a child to the stage behind objects that are already there. For example, I have a bar at the bottom of the screen that is there from the time you start the game and I have falling objects that I want to fall behind it, but instead they fall in front of it because I added them to the stage after. Is there a way of adding the falling objects behind it without having to keep re-adding the bar to the stage? Thanks in advance.
Rather than layering, I'd use adjust the index of each object using addChildAt and setChildIndex.
The following line adds your falling object behind every other DisplayObject on the stage (in this case, you should probably add your bar to the stage first)
stage.addChildAt(fallingObject, 0);
You can create Sprites to act as layers, and add the different objects to them. Here is an example that adds a layer for adding whatever you want behind the bar layer, and THEN adds the bar layer, so it will be on top. It's super rough since you don't have any code to reference:
package {
import flash.display.*;
public class Test extends MovieClip {
var barLayer:Sprite;
var objectLayer:Sprite;
public function Test() {
var objectLayer = new Sprite();
//add the object layer to your main Class
this.addChild(objectLayer);
//now you can add movie clips or sprites to objectLayer whenever you like
//then create the bar layer
var barLayer = new Sprite();
//add your bar Display Object here
var bar = new MovieClip();//draw the bar or reference the library object you've created
barLayer.addChild(bar);
//now add the barLayer
this.addChild(barLayer);
}
}
}
I'm trying to learn Actionscript 3. I need to shift my origin from the top left corner to the center of the stage. I found this question answered on stackoverflow :
How to change the coordinate origin in Flash's stage with Actionscript?
The suggestion is, "Create a MovieClip or Sprite and add that to the stage as your root object (instead of adding to the Stage) at stage.width/2, stage.height/2."
What does this mean? What is a root object? How do I add a MC as a root object to the stage?
When you launch a swf you are basically launching a MovieClip. (I'm probably going to get ripped a new one for this analogy). So when you write this
var myMC:MovieClip = new MovieClip();
addChild(myMC);
You are adding a MovieClip to your movies root/stage. Since it is not possible to truly alter the origin of the root/stage, that post is suggesting is that you do the next best thing. By creating another MovieClip and adding to your root/stage like this
var fauxRoot:MovieClip = new MovieClip();
fauxRoot.y = stage.stageHeight/2;
fauxRoot.x = stage.stageWidth/2;
addChild(fauxRoot);
Now that you have centered the fauxRoot MovieClip in your root/stage, you can add all your elements to the fauxRoot instead of your root/stage. Since the fauxRoot is centered in the main root/stage it's 0,0 will be in the center. An Example of adding a button might be
var uiButton:Button = new Button();
uiButton.x = uiButton.width/2;
uiButton.y = uiButton.height/2;
fauxRoot.addChild(uiButton);
The button should now be centered in the middle of your screen. Hope this helps a little.
Is it possible to display a mask object using AS3?
I have a MovieClip called myMC, then I mask myMC with the MovieClip called myMask. MOVIE_CLIP and MASK are library MovieClips.
var myMC:MovieClip = new MOVIE_CLIP();
var myMask:MovieClip = new MASK();
myMC.mask = myMask;
Of course, myMC won't show.
What I want is that myMC is only displayed in myMask, and not outside it, with myMask reamining visible.
To be quite honest with you, this isn't possible AFAIK. A mask, by definition, is invisible. What you would need is to have the mask be the SHAPE you want, and then have an additional MovieClip that shows whatever visual elements you would have shown on the mask.
Case and point, if you wanted to use a glass-pane graphic as your mask, you would need to have a glass-pane graphic, the mask with the same dimensions, and then the MovieClip you're masking underneath that mask.
I hope that helps.
i have a bunch of movieclips on the stage and some are nested into others, i would like to have 1 of them to be put on top of everything else on the stage when i start dragging it. Anyone know how to get a nested movieclip on top of parents etc?
You could have a top layer & a bottom layer , whenever you want to add a child on top of everything else, simply add the child to the top layer. This way you don't need to figure what the parent position is , just keep this layer on top for that specific purpose.
var topLayer:Sprite = new Sprite();
var bottomLayer:Sprite = new Sprite();
var childMc:MovieClip; // the mc you want to have on top
//somewhere in your code...
childMc.addEventListener(MouseEvent.MOUSE_DOWN , childMcMouseDownListener );
addChild( bottomLayer );
addChild( topLayer );
//add everything to the bottom layer
//leave the topLayer empty until you add the child mc
//add the following code in your MouseDown Listener
function childMcMouseDownListener(event:MouseEvent ):void
{
topLayer.addChild( event.currentTarget );
}
top of everything else on stage:
stage.addChild(target);
warning from AS3 doc :
The command stage.addChild() can cause problems with a published SWF
file, including security problems and conflicts with other loaded SWF
files. There is only one Stage within a Flash runtime instance, no
matter how many SWF files you load into the runtime. So, generally,
objects should not be added to the Stage, directly, at all. The only
object the Stage should contain is the root object. Create a
DisplayObjectContainer to contain all of the items on the display
list. Then, if necessary, add that DisplayObjectContainer instance to
the Stage.
Suppose, Container is your parent movieClip and it has many children then Now your target and currentTarget property depends on which object the listener is attached and how much you have nested your movieclips. in my case the Container is your parent movieClip having two or more children.
Container.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
public function pickUp(param1:MouseEvent) : void
{
var objectToDrag = param1.target;
Container.setChildIndex(param1.target,Container.numChildren-1);
objectToDrag.startDrag(true);
}
Pretty basic question here, but its still got me a little confused..
I have an object(navigation menu bar) that I want to change the colors on with code, so in an updateColor function, I get the bounds of the object (which is a drawing shape contained in a movieclip) and redraw a new shape on top of it with the new color, but I've noticed that the last shape still exists behind this redraw.
I tried using obj.graphics.clear(); before the redraw but that didn't get rid of the original shape. Is there another command that I'm overlooking?
Unless you drew the object you wish to remove within the same graphics object, clearing won't work. You need to remove the DisplayObject.
Depending on the number of children you can do:
obj.removeChildAt(0);
This also removes movieclips / buttons you placed on the stage manually.
If you have a reference to the DisplayObject you wish to remove you can simply do
obj.removeChild(backgroundClip);
Note that you can also change the color of a DisplayObject directly:
import flash.geom.ColorTransform;
...
public var test:MovieClip; //instance on stage
...
var cf:ColorTransform = test.transform.colorTransform;
cf.color = 0xff0000;
test.transform.colorTransform = cf;
while(this.numChildren)
{
this.removeChildAt(0);
}
Will clear child object on this MovieClip,
if it's clearing too much, then put the shape drawing in a subclip, and clear the subclip.