Skip to main content
OZDev92
Participating Frequently
July 25, 2011
Question

AS3 help with Array and .visible

  • July 25, 2011
  • 1 reply
  • 4895 views

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

This topic has been closed for replies.

1 reply

kglad
Community Expert
July 25, 2011

are you trying to see if objects in dropArray are dropped onto corresponding objects in hitArray?  ie, rBatt dropped onto TargetArea1 etc.

if yes, compare the dropped object's dropTarget index and the dropped object's index:

function mUp(e:MouseEvent):void {

         var dropIndex:int = dropArray.indexOf(e.currentTarget);

if(dropIndex==hitArray.indexOf(e.currentTarget.dropTarget)){

// do whatever

}

OZDev92
OZDev92Author
Participating Frequently
July 25, 2011

Thanks for your reply. Yes, that's what i want to do, but the actions i want to perform are different for each set of corresponding objects.

Wouldn't this run the same set of actions for each drop object rather than a different set for each one?

Or have i missed something

kglad
Community Expert
July 25, 2011

if you want to make all the hitArray and dropArray objects (that were not dropped and hit not visible), use:

function mUp(e:MouseEvent):void {

         var dropIndex:int = dropArray.indexOf(e.currentTarget);

if(dropIndex==hitArray.indexOf(e.currentTarget.dropTarget)){

for(var i:int=0;i<dropArray.length;i++){

dropArray.visible=false;

hitArray.visible=false;

}

e.currentTarget.dropTarget.visible=true;

e.currentTarget.visible=true;

}

}