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

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
}

Related

AS3 Swap depths in Flash IDE without destroying movieclips

I have two movieclips, one is on top, one is on bottom. The top one is set to the second frame. I move it to the z axis bottom on the next frame, and for the remaining frames it plays all frames like it has been reset without any code (like I didn't tell it gotoAndStop(2)).
Image example: http://i.imgur.com/mQ8f5uT.gif (hard to tell by the frame rate, but the dark green box starts playing as if I didn't set it's frame once I move it below)
I know it doesn't have that issue when I code the depth swapping with setChildIndex, but the animations are lost.
If you're wondering, it's a simplified issue of a larger moving character sprite animation that I would like to swap depths of movie clips in the animation without resetting the movie clips underneath the depth change. So I'm not really looking for a work around per se, unless there's a well design solution and not a band aid "hack."
I think I didn't understand your question fully. A MovieClip will keep playing or keep being stopped no matter what you do with it, even if it was removed from the scene. I made a simple example which shows that MovieClip state is not affected by depth change.
The working SWF is here to test: http://zdg.ru/tmp/updown.swf
And the code is:
import flash.events.MouseEvent;
var is_playing:Boolean = true;
btn_toggle.addEventListener(MouseEvent.CLICK, doToggle);
btn_updown.addEventListener(MouseEvent.CLICK, doUpdown);
function doToggle(evt:MouseEvent) {
if (is_playing) mv_test.stop(); else mv_test.play();
is_playing = !is_playing;
}
function doUpdown(evt:MouseEvent) {
var tmp:int = getChildIndex(mv_block);
setChildIndex(mv_block, getChildIndex(mv_test));
setChildIndex(mv_test, tmp);
}

As3 Calculate rotationX and rotationY value

I wonder if there is a formula that i can calculate the rotationX and rotationY value out of a sprite display object.
For example the sprite below. If a user set up all the points and draw out this shape. Is there a way i can find out rotationX and rotationY which can archive the same result?
I would like user to defined the container and whatever sprite drag into this container get auto rotationX and rotationY so the perspective stay the same.
I have found the solution by reading through the following link:
http://zehfernando.com/2010/the-best-drawplane-distortimage-method-ever/

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

as3 addChild into scaled movie clip & keep same scale and position

ATTEMPTING:
Loading a movie clip (NoScale_mc) into a scaled movie clip (Scaled_mc).
ISSUE:
When I load the movie clip NoScale_mc into Scaled_mc it obviously scales too.
QUESTION:
How can I keep the NoScale_mc in THE EXACT SAME POSITION and THE EXACT SAME SCALE but yet still load it into the Scaled_mc using the addChild() method?
You could do a little reverse tirckery with math to try to accomplish this. So get the scaled values of the parent DisplayObject and use those values to inverse-scale the child DisplayObject.
For instance, the parent DisplayObject is scaled to: scaleX = 1.45 and scaleY = 4.6. So you can set the child DisplayObject to: scaleX = 1/1.45 and scaleY = 1/4.6.
This may produce odd results though, and will most-likely end up being a headache to maintain. You're probably better off adding the child DisplayObject to the stage on top of the parent, like Marty Wallace said. If you want to keep it looking aligned with the parent DisplayObject, then just set both of their x and y positions to the same thing (or with an offset, if that is what is desired).

as3 dynamic index change for added objects on stage

I have added some backgrounds on stage and then on top of that adding another background and all these are movieclips.
At some time i have to remove the backgrounds and then it should be added but here problem am facing is the background become coming front.
so is there any function like send to back or bring to frond based on the movie clip names.
You want to experiment with :
setChildIndex(object, z-value)
This set the depth of the object on the stage.
swapChildren (object1, object2)
This exchange the position of two objects on the stage.
setChildIndex
swapChildren
swapChildrenAt
addChildAt
Use addChildAt(index);
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#addChildAt()