Okay so I am trying to get a stage movie clip instance to align (x,y) to the x, y cords of a movie clip instance that is inside of another movie clip instance (a little confusing).
stageMC.x = targetMC.subTargetMC.x;
stageMC.y = targetMC.subTargetMC.y;
So on an event (mouse click for example), we want the x, y cords of 'stageMC' instance to align with the x, y cords of 'subTargetMC' which itself sits inside of movie clip called 'targetMC'.
The movie clip 'subTargetMC' which sits inside of the primary 'targetMC' is an instance copy of a library MC and has its instance name as indicated.
I did not find a solution in the forum nor anywhere else online. I most likely will end up resolving this myself (as I usually do) but wanted to use StackOverflow as a help resource.
Thanks
Use localToGlobal() to convert the coordinates.
var globalCoordinates:Point = targetMC.subTargetMC.localToGlobal(new Point());
stageMC.x = globalCoordinates.x;
stageMC.y = globalCoordinates.y;
Related
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.
I have an application where user have to draw a line on the canvas without overlapping it. Is there a way to test the overlapping? I have googled already but found result with circles and rectangle overlapping. My case is different. Here user will draw lines on canvas without overlap the line itself. May be I am missing something so any guidance is appreciated. Thanks
I take it you mean the user draws a line with some sort of pen tool, using the mouse.
Here's what I would do:
First, hold the path of the line drawn in a BitmapData object.
var lineBitmapData:BitmapData = new BitmapData(display.width,display.height,true,0x00000000);
This creates a transparent bitmap object with the user's line on it.
On each frame (or timer event, if you use timer) do the following:
1.capture the current mouse position and put it into a Point object.
var currentMousePosition:Point = new Point(mouse.x,mouse.y);
you will also need a point representing the upper-left corner of your bitmapData.
var pt1:Point = new Point(1,1);
2.perform collision detection between the current mouse position and the lineBitmapData
var result:Boolean = lineBitmapData.hitTest(pt1, 0xFF, currentMousePosition);
the second parameter in the hitTest method is the threshhold value. Basically, this needs to be set to the minimum alpha value that you want to count as a hit.
3.check the result of the hitTest. If it's false, this means what the user is about to draw this frame does not intersect what was already drawn. In this case, you add the bit that was drawn during the last frame to the lineBitmapData.
If the hitTest returns true, however, that means the user is about to make his line intersect, so your program needs to stop the drawing (or whatever behavior you want).
if(result){
myPenTool.stopDrawing();}else{
var drawnLastFrame:BitmapData = myPenTool.drawSingleFrameLine();
lineBitmapData.draw(drawnLastFrame);}
4.Update what the user sees on the screen with the new lineBitmapData
To anybody who may care to help:
I am looking to create an animation (perhaps frame by frame) that corresponds with the coordinates of an object. Specifically, I want to have a draggable object's coordinates (locked to the x-axis) indicate where the playhead of a specific movie clip should be.
In other words, let's say that I have a 100px wide stage and I want each px location of an object on that stage to correspond to a particular frame of a movieclip.
In concept, I feel that it should be as easy as loading an objects coordinates into a variable, then passing that variable on with a simple math equation, adjusting it for movieclip length... but right about then my brain gets fried.
Finding out how to lock a draggable object to the x-axis has been pretty easy, but from there I'm stumped. I'm not particularly well versed in AS3 but I do like to think I understand the concepts.
Thank you in advance.
Try the following:
import flash.events.Event;
//the min (left-most) coord your draggable mc can be dragged
var minX:int=0;
//the max (right-most) coord your draggable mc can be dragged
var maxX:int=100;
var frameTo:uint;
//enterframe listener to check drag_mc x position continuously
addEventListener(Event.ENTER_FRAME, enterframe_handler);
function enterframe_handler(e:Event):void
{
//drag_mc is your draggable movieclip, anim_mc is the animation
//drag_mc.x should always be between minX and maxX: (minX <= drag_mc.x <= maxX)
//(drag_mc.x/(maxX - minX) gives us the "percentage" (from 0 to 1)
//multiply by the animation's total frames lenght,
// and add 1 (because frame numbers begin at 1)
frameTo = 1 + Math.floor((drag_mc.x/(maxX - minX))* anim_mc.totalFrames);
//set animation to target frame!
anim_mc.gotoAndStop(frameTo);
}
Here's what I'm aiming for. I'm querying the Rotten Tomatoes API for Upcoming Movies. For each movie returned, I'm creating an instance of MovieIcon (MC). I'm then adding this MC as a child of a Container MovieClip that's already on the scene. Each time, I'm incrementing the xPosition of each MovieIcon MC such that, they're positioned next to each other.
My container MC has a mask applied to it, therefore any child objects that are positioned beyond the size of the mask, they're are hidden from view.
How can I dynamically add a tween/easing animation between all these MovieIcon MC's so that when I hover over the Container MC, it 'scrolls' left or right, depending on the mouse motion?
Thanks in advance.
First I would recommend using a tweening library.
TweenLite and Tweener are good options
http://www.greensock.com/tweenlite/
http://code.google.com/p/tweener/
Both of these include docs that will help you get everything set up in your project.
Then you should be able to add a ROLL_OVER event to each of your MovieIcon MC's
MovieIcon.addEventListener(MouseEvent.ROLL_OVER, handleRollOver);
Inside your handler you can use the event.target property to get a handle on the over MovieIcon.
Assuming your using TweenLite you can go and add your tween to that target
private function handleRollOver(e:MouseEvent):void{
TweenLite.to(e.target, duration, {x: new x value, any other prop: any other val})
}
I have a MovieClip, that is representing a character in my game. Id like to "create bullets" shooting out from the tip of my characters gun. Problem is that when my character turns around, also the point rotates around the MovieClips pivot.
Is it possible to anyhow easily track this point, so that I could dynamically create new objects at the same location.
I tried to add a new MC as a child to my character, with the initial position at the guntip. In some systems child-objects "follow" their parents around, but it didnt seem to work here.
Is there any other "native" way of doing this, or do I just have to have a Polar-coordinates representation of the point relative to character-MovieClips origin, and add the MC rotation to theta, so that I can calculate the X and Y coordinates?
Try localToGlobal() and globalToLocal() methods to transform coordinates from your character movieclip to its parent.
Set up the movie clip with the gun (I'm assuming it's at the end of an arm?) so that the gun tip is straight across from the pivot point.
Then pass the method that fires the bullet three parameters: the x and y position of the gun MC, and its current angle.
The code for your bullets initial position might look something like this:
public function CreateBullet(x,y:Number, degree:Number)
{
// set start position
this.x = x + ARMLENGTH*Math.cos((degree/180)*Math.PI);
this.y = y + ARMLENGTH*Math.sin((degree/180)*Math.PI);
}
Where ARMLENGTH is the distance from the pivot point to the end of the gun.
Two caveats, Flash can do weird things with angles, so you might have to make an if statement in CreateBullet() with inverted degrees if the player if facing backwards. Also, if you have the gun MC as a child of your character, you might have to make a Point where the pivot point is then do a localToGlobal on it. There's a good reference for that here.