AS3 addchild to a movieclip which keeps the motion of the animation - actionscript-3

I am really new to flash and AS3 so I do not even know how to ask this question properly.
I have a character (MovieClip) which has an animation, what I want to do is add an item to this character, for example in the "head" layer, and this item should follow the "head" layer.
Thus means the item should be slightly above head in each frame, and the head is constantly changing its position.
What I've got so far is a static item (never moves with the head layer), adding it with "addchildat" to my movieclip.
var running : anim_running = new anim_running (); // running movieclip
var cono : i_1 = new i_1 (); // cone head item
running.addChildAt (cono, 10); // adding the cone to the proper layer
cono.x = 20;
cono.y = -20; // positioning the cone on top of the head
with this piece of code this is what it looks like (I can not post images yet)
http://oi45.tinypic.com/2qx6bls.jpg - this is a frame where the cone is properly positioned
http://oi47.tinypic.com/34g6bub.jpg - but in the next frame the cone won't follow the head layer
Sorry if this is a really noob question. I searched all over google before asking.
Many thanks in advance!

Probably the simplest way to do this (with not too much tricky code), would be to create a "container" movieclip inside the head movieclip where you can add in new hat movieclips.
Then just add the hat to the container. Make sure you have your instance names set up so you can reference them in code. Here is an example:
running.head.hatContainer.addChild(cono);

Ok, so if I'm understanding this correctly you'd want to add a container movie clip that has both the head and the cone inside of it. Then you'll animate the container as opposed to the head. That way you can add items to the container relative to the other items as if it were static and the animation keeps playing.
Rough example:
var container:MovieClip = new MovieClip();
addChild(container);
container.addChild(head);
container.addChild(cone);
TweenLite.to(container, 3, {x:container.x + 50}); /*head and cone move relative to each other because their parent is being animated */

Related

Actionscript 3 - how to add MovieClip to the stage as root object?

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.

exception, information=ReferenceError: Error #1056: Cannot create property ground on Main. (FLASH As3)

I have a deep problem which I have encountered.
I am adding a movie clip inside to another movie clip and flash doesn't like this.
What I'm doing is dynamically adding a movieclip to a movieclip that hasn't been added dynamically.
movieclip 1
private var tim:player = new player();
//inside main
tim.addChild(ground);
now ground is an instance name that hasn't been added via code. It's on the stage in flash and it's a movievlip with the instance name ground.
What can I do to fix this please.
Basically I wasn't really understanding As3,
From reading As3 101 - Display Lists I was able to understand that all things like sprites, shapes and movieclips are added to a display list.
In order to have added tim to a parent, that parent would have to be present.
This means that the parent would need to be added to the display list.
addChild(container);
You can see that container is now part of the display list.
This means I can now:
container.addChild(character);
Add the character to the container, and this now becomes a display object container
I can now trace what's in the container.
trace(container.numChildren);
In this container there are 3 childrens, my ground, the player and the enemy :D

Getting graphic/movie clip x,y position from within another movieclip?

This should be fairly simple I'd think, I'm just not that familiar with actionscript haha.
I have a game where I have the background moving behind a character that stays in one position on screen. I'm relatively new to actionscript 3 but I'm wanting to have text boxes pop up whenever the player presses a key over certain objects passing in the background.
So, basically the background itself is a movie clip, and I have other graphics and movie clips within the background mc.
I was thinking of getting the player.x and y position and then "comparing" that position (>= and <=, etc.) with the graphic/movie clip in the background. But I just don't know how to obtain the x and y coordinate of the graphics/movie clips in the background mc.
You could try to target your movie clips in the background by getting their coordinates, then removing their parent's position (the background container).
Something like :
var finalXPosition:int = targetMovieClip.x - backgroundContainer.x;
var finalYPosition:int = targetMovieClip.y - backgroundContainer.y;
By substracting the target movieclip parent's position to its position, you gain the final position in the parent's scope coordinates.
It should work for you as soon as your character and your background container are situated at the same level of the display list.
Here is a quick diagram of what I try to explain (please forgive my inaptitude to draw nice and explicit drawings ^^)
Usually, when I stumble upon such a case, I try to make a quick and even dirty drawing, starting with what I want, then breaking down every useful data I have to achieve that result, you should keep that method in mind and try it the next time ! :-)

Actionscript 3: make certain objects in child A appear above child B while others appear under child B

Imagine I have a background and I want to show the background under the player object. This can be done with ease:
var player:Player = new Player();
addChild(player);
var background:Background = new Background();
addChildAt(background, 0);
However, imagine in this background I have transparent clouds which have to appear above the ship and non-transparent stars which need to appear under the ship. The above code would simply make all background objects go under the ship. Any tips?
Make a foreground layer that is rendered after the Player object. That is the easiest way to accomplish this effect.
i.e.
var foreground:* = ...;
addChildAt(foreground, 2);
I'd imagine you're going to have multiple objects that you want to appear between the foreground and background layers, so I would actually also recommend creating an "active" layer, which is the actual parent of your "player" object.
So the object hierarchy looks akin to this:
Scene
Background
Rolling hills
Active
Player Sprite
Enemies
Obstacles
Foreground
Clouds

Deleting a shape via code

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.