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

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 ! :-)

Related

Possible to mask an animated Movie Clip? Flash as3 cs4

I have a simple animation set up - A background I have painted in P/Shop and a Sheep.
The sheep is an animated movie clip - whose head turns and body moves.
What I need to happen is that when the sheep walks under a tree - he is in complete shadow - until he walks out from under the tree.
So far I managed to put the sheep under a shadow layer.. mask the shadow and use a rectangle which is aprox sheep size - it then moves along a tween teh same as the sheep.. It is quite clunky though and will take a fair amount of work reshape the mask shape each time to cover the exact sheep.. even then I won't be able to follow the animation exactly.
So, is there a way to have the animated sheep be a movie clip and a mask - so that he goes into full shadow everytime he passes under a tree.
Or is there another way to achieve this? Thx
The problem is cacheAsBitmap property,
you must assign maskclip and your maskedclip property to cacheAsBitmap = true
For example:
maskmc.cacheAsBitmap = true;
myMaskedClip.cacheAsBitmap = true;
myMaskedClip.mask = maskmc;
I added sample fla file that you can check it here : FLA
Can you put your sheep on a layer in between the shadow and the background? Probably not because you want the sheep in front of the tree -
Maybe you need to put a shadow element inside the sheep movie clip, and use a script to fade it in or out depending on hit test with the tree.
You could add a duplicate copy of the sheep to be used as a mask, although a shadow would probably need softer edges than this kind of mask will give you.

AS3 - Finding an objects x and y position relative to the stage

I'm new to ActionScript 3 and I have a character which you can control, the screen scrolls right along the stage and he can fire missiles.
The problem I'm getting is the missiles are created via these co-ords:
bullet.x = hero.mc.x;
bullet.y = hero.mc.y
These work fine untill the screen has scrolled to the right. I assume it's because the bullets are being spawned as a result of them using the canvas x,y and not the stages x,y
So i'm wondering how to find out the x and y of my hero in relative to the canvas so i can spawn the missiles on top of him!
Thanks, and if you need any more information let me know, I'm new to all this. Thank you.
You can do that with localToGlobal and globalToLocal. Your solution would be something like:
bulletPos = bullet.parent.localToGlobal(new Point(bullet.x, bullet.y));
Beware, though, as those are last resort functions. Normally, you'd have all your elements using the same 'layer', so comparisons are easier and faster.

How to detect if there is a drawing/graphic drawn on a movie clip in AS3?

I am a newbie to AS3. I have a movie clip.I want to know if there is a drawing/graphic drawn over movie clip at all?
Simple, check the width and height of the MovieClip. Assuming you mean the MovieClip itself contains the drawing/graphic.
After seeing Fygo's comment
I did some testing and found out that the scale properties of the MovieClip will change depending on whether or not it has any graphics.
Meaning if you set the width and height to zero, the corresponding scaleX and scaleY will also be set to zero. So what you can do is check the scale AND the dimensions of the MovieClip. If both scales are 1:1 and both dimensions are 0:0, that means you didn't mess with the dimensions and it truly is graphic-less.
trace (awd.scaleX, awd.scaleY, awd.width, awd.height);
//If you get 1 1 0 0 as the output, the MovieClip is empty
Use readGraphicsData(). I assume that if it's empty it means there is nothing drawn there :)
It's not perfect though, read the reference
Maybe you just mean collision detection? To detect if two movieClips are touching...?
For that you need either:
HitTestObject - (checks if two objects touching by their box boundaries) - Link:
or HitTestPoint - (read description carefully) - Link:
A good tutorial explaining both methods is here: - Link:
example code:
if ( MC_one.hitTestObject(MC_two) )
{
trace("MovieClip One is touching/over MovieClip Two");
//add code needed to happen when touching/over. example below
//MC_two.gotoAndStop(2); //example tells touched MC_two to change frame to 2
}

As3 sprite rotationX and rotationY

I have a bit problem with rotationX and rotationY.
It's cool if i just do a roationX and rotaionY below
_eventParent.rotationY =_differentX;
_eventParent.rotationX =_differentY;
However once i have assign a mouse move to the _eventParent. The roationX and roationY change perspectively while the mouse is moving. so instead the item remain the same size. it increase and decrease size prospectively. any idea why is it doing this? is there a possibility to stop this behavior?
Thanks
Please find the image below.
Perspective allows part of your shape to look closer to you than other parts. The problem is that perspective has a center, or "vanishing point" and by default, it is fixed. As you move your shape farther away from the vanishing point, the perspective changes, causing your shape to widen or narrow.
You can fix this by updating the vanishing point so that it is always at the same coordinates as your shape. Since the shape will always be at the vanishing point, the perspective shouldn't change.
To do this, create a perspectiveProjection for your shape:
_eventParent.transform.perspectiveProjection = new PerspectiveProjection();
PerspectiveProjection is located in the flash.geom package, so don't forget to import it.
Then whenever you update your shape's position, update it's vanishing point:
_eventParent.transform.perspectiveProjection.projectionCenter =
new Point(_eventParent.x, _eventParent.y);
You might need to offset the vanishing point by a set number of pixels to get the perspective looking the way you want it to.
Correct me if I misunderstood your question. Your question is that if you apply rotation to the movieClip object, then why does the size appear to be changing?
For simplification, Let's not apply rotation on both X and Y axis. Let's take a rectangular movie clip and onMouseMove we do ++myMovieClip.rotationX;
Now, this statement is going to apply rotation on the object about the X-axis and one would get a perspective of the movie clip flipping across X -axis and this flipping will show as change in size of the object.
The same applies to rotating across y-axis.

as3: Making an animation

I am playing with animation in AS3 and flex4, and I've come into a problem. My application is a game board (like a chess board), where each field is a border container added to some position.
Also I am adding a child element (shape), to this container on mouse click. What I want to achieve is to be able to move shapes smoothly from one field to another. But it appears that the shape goes behind the neighbor field this way http://screencast.com/t/iZ3DCdobs.
I believe this happens because shape is a child of specific border container, and to make it visible over every other container, I would need to use layers somehow....
I would be happy if anybody could suggest a solution
Yes you're right on that. You should add the movable objects to a different layer.
As there are no typical layers in AS, you could try to drop the fields in one sprite and any other objects to a different an than place them on each other, so that when you will move a object it won't go behind other objects.
If you place both sprites in the same position you will still have accurate x,y positions between movable objects and fields.
You have two options:
First one is to have different layers for your DisplayObjects: as an example, the bottom layer would hold all the boards, and the upper layer would hold all the pieces.
Second option is to manipulate the index of the objects with swapChildren(), swapChildrenAt(), and setChildIndex(). So to bring a MovieClip to the topmost front, you would do MovieClip(parent).setChildIndex(this, 0);
If the situation is that always the shape object gets hidden behind the next ( right side ) grid container, the I suggest you create your grid in reverse.
Suppose you are creating a chess grid. that is a 8x8 grid. Normally you would create your grid using 2 for loops, looping from 0 to 8 with say the x and y points starting at 0,0 for the first grid and going on till the end. What I suggest you to do is to create from 8,8 to 0,0.
Display objects in flash are stacked on top of each other based on their child index.
For example: If you create two objects. Rectangle and Circle as follows
var rect:Rectangle = new Rectangle();
this.addChild(rect);
var circ:Circle = new Circle();
this.addChild(circ);
The circle will always be on top of the rectangle in this scenario because the circle was added after the rectangle to the display list.
So if you reverse the order of creation of your grid, the right grid cell will be added to the display list first and so the grid cells to the left will always be on top of the right ones. Hence, the problem that you are facing will not occur.