How to move an object with actionscript from with in itself? - actionscript-3

I want to be able to change the x value of an object from with in its own timeline.
The object has been created with actionscript so it does not have an instance name.
So I'm just wondering if there is a command like self or something so I can move an object.
Eg
self.x = 0;

You can do it with
this.x = 0;
or just
x = 0;
But if your display objects tree is deeply nested, these values will be relative to the object they've been added to. So if you need relative coordinates - just move it with this.x or x, and if you need global ones - use localToGlobal method

Related

Libgdx - How to move object forward by direction?

Example: I have 3 object with 3 rotation so how can i move object forward by object's direction like that.
http://i.stack.imgur.com/pTWDf.png
If you are using sprites to hold to values of each object, set the x/y += direction you want the objects to travel. Otherwise, if you are drawing the image through a spritebatch you can use batch.draw(texture, x, y) and set the x and y coordinates that way.

Actionscript 3: Align MC to MC Inside MC Cords

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;

Actionscript 3: translating coordinates from object's 3D space to another's?

I feel like this has probably been asked/answered here, and if so, I apologize for the bandwidth, but I don't see any explanation.
How does one translate from one object's coordinate space to another in Flash AS3? I can take a point in an object and translate it to global coordinates using local3DToGlobal() and then to another object's local using globalToLocal3D() -- but is there a direct way?
Thus, if I wanted one object to be able to say to another: 'move your top left corner to my top left corner', even through the two objects are in different z-spaces, rotated 3-dimensionally, etc.
I assume it is in the matrix3D matrix manipulations —
Matrix multiplication? TransformVector()? deltaTransformVector()?
I have been poring over the API but would really appreciate a concrete example.
Thanks!
One approach would be getRelativeMatrix3D(), called from the transform property of a display object, as in: transform.getRelativeMatrix3d(root).position.
Returns a Matrix3D object, which can transform the space of a
specified display object in relation to the current display object's
space. You can use the getRelativeMatrix3D() method to move one
three-dimensional display object relative to another three-dimensional
display object.
From Adobe's Performing complex 3D transformations, there is an example using Matrix3D objects for reordering display, in which faces of a box are reordered to ensure that layering of 3D display objects corresponds to the relative depths after rotations have been applied:
var faces:Array;
for (var i:uint = 0; i < 6; i++)
{
faces[i].z = faces[i].child.transform.getRelativeMatrix3D(root).position.z;
this.removeChild(faces[i].child);
}
faces.sortOn("z", Array.NUMERIC | Array.DESCENDING);
for (i = 0; i < 6; i++)
{
this.addChild(faces[i].child);
}

Masking Many Objects With 1 Mask Using `getChildAt(i).mask` Not Working

I am attempting to apply a mask to all objects on the stage except for a couple. There are a lot of different objects, and the amount of them will change in the future, so I want the masking to be done dynamically.
I wrote this code:
var i;
for (i = 0; i < this.numChildren; i++) {
if (this.getChildAt(i).name!="stage_kelp_bg" && this.getChildAt(i).name!="magnifier_mask") {
this.getChildAt(i).mask = this.magnifier_mask;
}
}
The above code is inside the document class's constructor method. Simply stating something like:
this.stage_kelp.mask = this.magnifier_mask;
works flawlessly, but only for that one object. Any idea what's wrong?
No errors are thrown, the objects just simply don't get masked.
Further research shows me that I cannot apply 1 mask to multiple objects. I have to have a mask for each object, or put all the objects into one container and mask that container.
Apparently you can use a layer to mask multiple objects on the timeline, but you can't do it programmatically without adding all the objects to one container. Unfortunately I can't do this without re-coding the entire application, so I will be using the timeline to mask things.
I would suggest you to better move all the movieclips to be masked in a single movieclip. This would be easier, if it's feasible in your case.
How about for each
for (var mc:movieClicp in this){
mc.mask=mask_}

localToGlobal/globalToLocal AS3 confusion

I want to move a display object from one container to another, but have it appear in the same place on screen.
I thought I'd understood this years ago, but the following does not work:
function moveToNewContainer(obj:DisplayObject, newParent:DisplayObjectContainer):void {
var pos:Point = new Point(obj.x, obj.y);
var currentParent:DisplayObjectContainer = obj.parent;
pos = currentParent.localToGlobal(pos);
currentParent.removeChild(obj);
newParent.addChild(obj);
pos = newParent.globalToLocal(pos);
obj.x = pos.x;
obj.y = pos.y;
}
This doesn't position the object in the same place as I would have expected.
Does anyone know what I am doing wrong, please?
Thanks,
James
Using localToGlobal/globalToLocal and setting the x and y properties like you showed calculates the correct position for the object in its new parent, but does not adjust for other aspects of the transformation such as scaling or rotation. In other words, the object's registration point will indeed remain in the same place, but the object may be rotated, scaled, or sheared differently.
The solution to your problem will need to take into account the transform.concatenatedMatrix properties of the old and new parents--you'll need to multiply the object's transformation matrix by one and then by the inverse of the other, or something along those lines. Leave a comment if you need help working out the math.
There is nothing wrong with your code, provided that both containers have no transformations applied. If your clips are scaled, rotated, etc.. you need to handle that in addition to the coordinate space transformations that localToGlobal and globalToLocal do.
You have to check if your containers are actually placed on stage. If your new container isn't added as a child to stage, function globalToLocal fails, just because it doesnt know how to correctly calculate that data.