AS3 help with Array and .visible
Hi,
I'm very new to flash so forgive me for this (most likely) silly question ![]()
I'm making a drag and drop application using some AS3 from a tutorial i found on the web:
//Array to hold the target instances, the drop instances,
//and the start positions of the drop instances.
var hitArray:Array = new Array(TargetArea1,TargetArea2,TargetArea3);
var dropArray:Array = new Array(RBatt,HAnt,GAnt);
var positionsArray:Array = new Array();
//This adds the mouse down and up listener to the drop instances
//and add the starting x and y positions of the drop instances
//into the array.
for (var i:int = 0; i < dropArray.length; i++) {
dropArray.buttonMode = true;
dropArray.addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray.addEventListener(MouseEvent.MOUSE_UP, mUp);
positionsArray.push({xPos:dropArray.x, yPos:dropArray.y});
}
//This drags the object that has been selected and moves it
//to the top of the display list. This means you can't drag
//this object underneath anything.
function mdown(e:MouseEvent):void {
e.currentTarget.startDrag();
setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}
//This stops the dragging of the selected object when the mouse is
//released. If the object is dropped on the corresponding target
//then it get set to the x and y position of the target. Otherwise
//it returns to the original position.
function mUp(e:MouseEvent):void {
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;
target.stopDrag();
if (target.hitTestObject(hitArray[dropIndex])) {
target.x = hitArray[dropIndex].x;
target.y = hitArray[dropIndex].y;
}else{
target.x = positionsArray[dropIndex].xPos;
target.y = positionsArray[dropIndex].yPos;
}
What Im trying to do now is have certain movie clips become visible and others become invisible
when a movie clip from one of the arrays is dropped on its corresponding target area.
I've had a shot at it:
RBatt.addEventListener (MouseEvent.MOUSE_UP, STA1)
function STA1(e:MouseEvent):void{
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;
if (RBatt.target.hitTestObject(hitArray[dropIndex])) {
MovieClip(parent).RBattAtt.visible = true
MovieClip(parent).RBatt.visible = false
MovieClip(parent).A2.visible = false
MovieClip(parent).T2.visible = true
MovieClip(parent).A3.visible = true
}else{
}
GAnt.addEventListener (MouseEvent.MOUSE_UP, STA2)
function STA2(e:MouseEvent):void{
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;
if (GAnt.target.hitTestObject(hitArray[dropIndex])) {
MovieClip(parent).GAntAtt.visible = true
MovieClip(parent).GAnt.visible = false
MovieClip(parent).A3.visible = false
MovieClip(parent).T3.visible = true
MovieClip(parent).A4.visible = true
}else{
}
HAnt.addEventListener (MouseEvent.MOUSE_UP, STA3)
function STA3(e:MouseEvent):void{
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;
if (HAnt.target.hitTestObject(hitArray[dropIndex])) {
MovieClip(parent).HAntAtt.visible = true
MovieClip(parent).HAnt.visible = false
MovieClip(parent).A4.visible = false
MovieClip(parent).T4.visible = true
}
But im pretty sure that's wrong. I'm quite sure the command im using
to hide/unhide the MC's is correct. However, I think I am adressing it wrong.
Any help appreciated ![]()
Cheers
OZDev92
