AS3 Calling a child's index during a for each - actionscript-3

I have a function that uses a mouse click event to create a child of a symbol at the mouse's coordinates. The instance is then pushed to an array.
I am now trying to make a new function that allows you to drag/drop a child when the mouse is within 10 pixels of it. When the child is dropped, I want it to rotate to face the next child in the array. I am currently communicating with each child in the array using a for each loop.
The bit I'm having trouble with is getting the index value of the child being dropped and setting it to a variable.
For reference, ins_trailPoint is the variable that creates the child and tPoint is the name of the array containing the children. child is the variable trying to acquire the index value of the dropped child.
function movePoint(Event:MouseEvent):void {
for each (var ins_trailPoint in tPoint) {
var child:int = event.target.parent.getChildIndex(event.target);
var px:Number = mouseX - ins_trailPoint.x;
var py:Number = mouseY - ins_trailPoint.y;
var dist = Math.sqrt(px*px + py*py);
if (dist<10) {
stage.removeEventListener(MouseEvent.CLICK, addPoint);
stage.addEventListener(MouseEvent.MOUSE_UP, dropPoint);
ins_trailPoint.startDrag();
function dropPoint(event:MouseEvent):void {
ins_trailPoint.stopDrag();
var dx:Number = tPoint[child+1].x - ins_trailPoint.x;
var dy:Number = tPoint[child+1].y - ins_trailPoint.y;
var radians:Number = Math.atan2(dy,dx);
ins_trailPoint.rotation = radians * 180/Math.PI;

The parameter of movePoint() is called Event with a capital letter, but you try tu use event in lower case.

Related

Adding a new child movieclip

I am trying to get a cube movieclip to create another instance of itself and then add it to the stage at the coordinates right next to the previous one. However I can't get it to keep adding more children/ can't figure out how to access the childs coords. I attempted to add a new movieclip:mc every iteration of the for loop with the vertical and horizontal counting back by 50 every time and then,on a different thought process, i tried adding a new child every loop but I don't know how to access that childs properties. Any help would be appreciated. Thank you.
var countV:int=600;
var countH:int=600;
stage.addChild(havenAlpha);
var moveHOrV:Boolean=true;
var mc:MovieClip = new haven_Expand();
for(var i:int=0; i<=5; i++){
if(moveHOrV == false){
stage.addChild(mc);
countH=countH-50;
mc.x= countH;
moveHOrV=true;
}else if(moveHOrV == true){
stage.addChild(mc);
countV=countV-50;
mc.y=countV;
moveHOrV=false;
}
trace(countV,countH,moveHOrV,i);
stage.addChild(new haven_Expand())
stage.addChildAt(new haven_Expand(),countH);
}
In most Object oriented languages, variables are just pointers to the actual objects. So changing what is stored in a variable, doesn't actually make what was stored there previously go away.
To that end, you can just make one var to store each new object created every iteration. Like so:
stage.addChild(havenAlpha);
//this var will store the clip from the preivous iteration, but we'll start it off with the first item
var prevClip:MovieClip = new haven_Expand();
prevClip.x = 600;
prevClip.y = 600;
stage.addChild(prevClip);
var curClip:MovieClip; //used in the loop below to hold the current iterations item
for(var i:int=0; i<=5; i++){
curClip = new haven_Expand();//create new clip
stage.addChild(curClip); //add it to the stage
if(i % 2){ // % (modulous) gives you the remainder of the division, so this effectively will be false (0) every other time
curClip.x = prevClip.x - curClip.width; //place this new clip just to the left of the last iteration's clip
curClip.y = prevClip.y;
}else if(moveHOrV == true){
curClip.y = prevClip.y - curClip.height; //place this new clip just on top of the last iteration's clip
curClip.x = prevClip.x;
}
prevClip = curClip;
}
I'm not sure though if the x/y math is what you desire. This will make them distribute in a diagonal fashion. Are you actually wanting horizontal rows? I can update to show you that if desired.

as3 accessing Children from dynamicly added moiveclip

I am adding moiveClips and putting their position accordingly. I am placing all the dynamically added clips in one new movie clip and then centering that on stage. As a test, I see I can control the alpha in the main container but I can not access the children. How can I assess each flagButton separately? There would be several in this loop example below.
var flagButton:MovieClip;
var flagArray:Array = new Array;
var flagDisplayButtons:MovieClip = new MovieClip();
function displayFlagButtons()
{
for( var i = 0; flagArray.length > i; i++)
{
var flagButton:MovieClip = new roundButton();
flagButton.x = (flagButton.width + 10) * i;
flagButton.y = stage.stageHeight - flagButton.height;
addChild(flagButton);
flagDisplayButtons.addChild(flagButton);
}
addChild(flagDisplayButtons);
// Works
flagDisplayButtons.x = stage.stageWidth/2 - flagDisplayButtons.width/2;
// Does not see flagButton
flagDisplayButtons.flagButton.alpha = .5;
}
GETS ERROR:
TypeError: Error #1010: A term is undefined and has no properties.
In your example code, you have created a bunch of button instances, and one by one you have added them to the display list of flagDisplayButtons. They are on that display list in the order that you placed them.
first instance is at index 0
second instance is at index 1
third instance is at index 2
and so on.
So if you want to access the third button you added, you could do this :
var flagButton:RoundButton = flagDisplayButtons.getChildAt(2) as RoundButton;
Now that you have created variable to reference that button, you can do any of these :
flagButton.alpha = .5;
flagButton.x = 100;
flagButton.y = 200;
You need to use getChildAt or getChildByName to get the children of a DisplayObjectContainer
IE:
flagDisplayButtons.getChildAt(0).alpha = 0.5;

as3 marquee select drag multiple child objects

Can anyone tell me how to achieve marquee selection effect with AS3 to select multiple movieclips by drawing a dynamic rectangle around them and then drag and drop them anywhere?
Don't use startDrag() if you need multiple objects to be draggable, since it only allows one object to be dragged at a time. Instead, listen for mouse events and do the moving manually:
var oldX:int;
var oldY:int;
var dragging:Boolean = false;
function onMouseDown(evt:MouseEvent):void {
dragging = true;
oldX = evt.stageX;
oldY = evt.stageY;
}
function onMouseMove(evt:MouseEvent):void {
if (!dragging) return;
var dX:int = evt.stageX - oldX;
var dY:int = evt.stageY - oldY;
for (int i = 0; i < selectedClips.length; i++) {
var clip:DisplayObject = selectedClips[i];
clip.x += dX;
clip.y += dY;
}
oldX = evt.stageX;
oldY = evt.stageY;
}
function onMouseUp(evt:MouseEvent):void {
dragging = false;
}
This code assumes that:
Your array of selected objects is called selectedClips.
Your array of selected objects all inherit from DisplayObject.
You have added event listeners on all draggable objects for the MOUSE_DOWN, MOUSE_MOVE, and MOUSE_UP mouse events which call these functions.
If any of those three conditions are not met, update my code or your code to work properly. Also, if you need to do any additional handling when the objects are dropped, you can use the mouse up handler to add custom code.

AS#3 target ignoring parent movieClip

I changed the question as its seems to be a problem with target not registering children mc/ or nested MovieClips.
var box:Box = new Box();
ground.push(box);
levelPlane.addEventListener(MouseEvent.MOUSE_DOWN, onOver);
box.x = box.width /2* (x + y);
box.y = box.height/2 * (x - y);
levelPlane.addChild(box);
function onOver(e:MouseEvent):void{
var tree1:Tree1 = new Tree1();
addChild(tree1)
trace(e.target.x);
tree.x = e.target.x;
}
How Do i target the movieclips(BOX) inside of the main MovieClip(levelPlane)?
imagine i have nested 10 boxes inside a MovieClip Called "levelPlane" i want to click on any of the boxes to add another Mc on the box i clicked x,y location.
If I understand correctly, you are trying to place the newly created movie clip on top of the other, but they are not within the same coordinate space. The target's coordinates must be translated to tree1's coordinate space, for both of them to have the same position:
var tree1:Tree1 = new Tree1();
addChild(tree1);
var global:Point = e.target.parent.localToGlobal(new Point (e.target.x, e.target.y));
var local:Point = globalToLocal(global);
tree1.x = local.x;
tree1.y = local.y;
The Event.currentTarget property refers to the current object processing the event, i.e., the listener object. If you want reference to the object which dispatched the event, use e.target

AS3 Can't access MC on Stage

I have 54 MC's on stage, generated dynamically. Now I want to get their x and y positions when rolling over but I am having problems getting the path correct e.g.
function copyFlightCellData():void {
var r; var s;
var cellData:Array = new Array ();
for (r = 0; r < 54; r++){
//var copyCellData = new MovieClip();
cellData[r] = Object(root).mc85.name; //["mc"+r+r];
trace("$$$$$$$$$$$$$$$$$$$$$" + cellData[r]);
}
}
I used the list objects in debug and they are listed in _level0 e.g.
Movie Clip: Frame=1 Target="_level0.mc85"
Not sure why I can't access their properties.
This is the code that created the MC's
// Create copies of flightCell for board grid
var my_mc = new flightCell();
my_mc.name = "mc" + i + j;
trace("^^^^^^^^^^^^^^****************" + my_mc.name);
addChild(my_mc);
Answer is pretty simple, use the DisplayObjectContainer object's, in this case root, getChildByName() method, for example:
var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
addChild(sprite1);
trace((root as DisplayObjectContainer).getChildByName("sprite1").name); // output : sprite1
It's probably a better idea to store the movieclips you have on your stage in an array to begin with.
To access it by name you have to assign a name to them when you create them.
mc85.name = "mc85";
As an alternative that I recommend, you can use getChildAt(index) : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#getChildAt()
Also, I highly recomend you to create an empty movieclip or sprite and add all of this mcs to them instead of the root.