Skip to main content
Inspiring
October 18, 2011
Question

batch operation in function

  • October 18, 2011
  • 1 reply
  • 700 views

hey folks,

I have a number of mcs labelled h 1-15. I want to have a function that if someone places an mc on it, it'll lock to the h mc centered. So I'm thinking I'll have a function similar to this (see below). Is there a way to run this without having to do 15 of the else ifs?

function dragchange(e:DisplayObject):void{ 

if(h1.hitTestPoint(mouseX, mouseY, true)){

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.2,rotation:0});

} else if (h2.hitTestPoint(mouseX, mouseY, true)){

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.4,rotation:0});

} else if (h3.hitTestPoint(mouseX, mouseY, true)){

          TweenMax.to(me,1,{scaleX:0.2,scaleY:0.1,rotation:0});

} else {

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.4,rotation:0});

me.x =500

me.y=500

}

This topic has been closed for replies.

1 reply

HandVoodoo
Participating Frequently
October 18, 2011

whats calling the dragChange function?

it will probably be much easier to simply check what mc, if any, is located under the mouseX, mouseY coords (atleast thats what i think youre doing)....

since ultimately the operation your doing to the MC is the same, regardless of which MC it is, you may want to just wrap everything up in an array (or typed vector) than perform your operation.

Here is some pseudo code..

Not exactly sure of all your var names.

                         public var myVector:Vector.<MovieClip> = new Vector.<MovieClip>;

                    public function assignToVector():void {

                              //create a pointer to all your 'h' movie clips in the vector

                              for (var q:Number = 1; q < 16; q++) {

                                        myVector.push(this["h" + q.toString()]);

                              }

                    }

 

                    public function dragChange(e:DisplayObject):Boolean {

                              //cycle thru all the movieclips until a hittest is found

                              for each(var clip:MovieClip in myVector) {

                                        //if hittest, do your Tweening

                                        if (clip.hitTestPoint(mouseX, mouseY, true)) {

                                                  TweenMax.to(me, 1, { scaleX:0.4, scaleY:0.2, rotation:0 } );

                                                  me.x = 500;          //what is 'me'? is this listed somewhere else?

                                                  me.y = 500;

                                                  return true; //return true if something is found

                                        }

                              }

                              return false; //return false if nothing is found

                    }

Inspiring
October 19, 2011

hey voodoo, pretty simple coder here. The dragchange function name is just a place holder. Really this function will serve as a lock in place function. So when the user grabs a card and drags it to the specified area, there are "pockets", I want the card they choose to lock into the center of the pocket. So I have 15 pockets designated h1-15. The "me" is the dragged object that is currently active. so my whole code currently looks like this. The lockinplace function is actually the one I meant. Would the code you provided still work?

// listeners

helpmenu.xbutton.addEventListener(MouseEvent.CLICK, xbuttonclick);

answermenu.xanswerbutton.addEventListener(MouseEvent.CLICK, xanswerbuttonclick);

backbutton.addEventListener(MouseEvent.CLICK, backclick);

helpbutton.addEventListener(MouseEvent.CLICK, helpclick);

answerbutton.addEventListener(MouseEvent.CLICK, answerclick);

stage.addEventListener(Event.ENTER_FRAME,_fullScreen);

import fl.transitions.Tween;

import fl.transitions.easing.*;

import fl.transitions.TweenEvent;

import com.greensock.*;

import flash.media.Sound;

import flash.display.StageScaleMode;

import flash.events.Event;

import flash.display.StageDisplayState;

helpmenu.visible=false;

answermenu.visible=false;

//cdcasetop.mouseEnabled = false;

//variable declarations

//var dvdpickupsound:dvdpickup=new dvdpickup

//var dvdputdownsound:dvdputdown=new dvdputdown

for(var i:int=1;i<=15;i++){

this["pc"+i].addEventListener(MouseEvent.MOUSE_DOWN, grabMe);

}

var me:Object;

function grabMe(e:MouseEvent):void{

          me = e.currentTarget;

          me.removeEventListener(MouseEvent.MOUSE_DOWN, grabMe);

          me.startDrag(true);

          //TweenMax.to(me,0.2,{scaleX:1.2,scaleY:1.2,rotation:0});

          addChild(e.currentTarget as DisplayObject);

          //dvdpickupsoundplay();

          stage.addEventListener(MouseEvent.MOUSE_MOVE, dragMe);

          stage.addEventListener(MouseEvent.MOUSE_UP, dropMe);

}

function dropMe(e:MouseEvent):void {

          stage.removeEventListener(MouseEvent.MOUSE_UP, dropMe);

          stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragMe);

          me.stopDrag();

          //TweenMax.to(me,0.2,{scaleX:0.63,scaleY:0.63,rotation:0});

          me.addEventListener(MouseEvent.MOUSE_DOWN, grabMe)

          //dvdputdownsoundplay();

          }

function dragMe(e:MouseEvent):void {

          e.updateAfterEvent();

          dragchange(DisplayObject(e.currentTarget))

}

function dragchange(e:DisplayObject):void{ 

if(table.hitTestPoint(mouseX, mouseY, true)){

          setChildIndex(leftclocktop,numChildren - 1);

          setChildIndex(rightclocktop,numChildren - 1);

          setChildIndex(middleclocktop,numChildren - 1);

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.2,rotation:0});

} else if (h1.hitTestPoint(mouseX, mouseY, true)){

          setChildIndex(leftclocktop,numChildren - 1);

          setChildIndex(rightclocktop,numChildren - 1);

          setChildIndex(middleclocktop,numChildren - 1);

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.4,rotation:0});

} else if (clockbottom.hitTestPoint(mouseX, mouseY, true)){

          setChildIndex(leftclocktop,numChildren - 1);

          setChildIndex(rightclocktop,numChildren - 1);

          setChildIndex(middleclocktop,numChildren - 1);

          TweenMax.to(me,1,{scaleX:0.2,scaleY:0.1,rotation:0});

} else if (clockbottom1.hitTestPoint(mouseX, mouseY, true)){

          setChildIndex(leftclocktop,numChildren - 1);

          setChildIndex(rightclocktop,numChildren - 1);

          setChildIndex(middleclocktop,numChildren - 1);

          TweenMax.to(me,1,{scaleX:0.2,scaleY:0.1,rotation:0});

} else if (clockbottom2.hitTestPoint(mouseX, mouseY, true)){

          setChildIndex(leftclocktop,numChildren - 1);

          setChildIndex(rightclocktop,numChildren - 1);

          setChildIndex(middleclocktop,numChildren - 1);

          TweenMax.to(me,1,{scaleX:0.2,scaleY:0.1,rotation:0});

} else {

          setChildIndex(leftclocktop,numChildren - 1);

          setChildIndex(rightclocktop,numChildren - 1);

          setChildIndex(middleclocktop,numChildren - 1);

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.4,rotation:0});

}

}

function lockinplace(e:DisplayObject):void{     

function dragchange(e:DisplayObject):void{

if(h1.hitTestPoint(mouseX, mouseY, true)){

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.2,rotation:0});

} else if (h2.hitTestPoint(mouseX, mouseY, true)){

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.4,rotation:0});

} else if (h3.hitTestPoint(mouseX, mouseY, true)){

          TweenMax.to(me,1,{scaleX:0.2,scaleY:0.1,rotation:0});

} else {

          TweenMax.to(me,1,{scaleX:0.4,scaleY:0.4,rotation:0});

me.x =500

me.y=500

}

function _fullScreen(e:Event):void

{

   stage.removeEventListener(Event.ENTER_FRAME,_fullScreen);

   stage.scaleMode = StageScaleMode.EXACT_FIT;

   stage.displayState = StageDisplayState.FULL_SCREEN;

}

//nav bar items

function backclick(event:MouseEvent):void{

          answermenu.visible=false;

          helpmenu.visible=false;

}

function helpclick(event:MouseEvent):void{

          helpmenu.visible=true;

          answermenu.visible=false;

          setChildIndex(helpmenu,numChildren - 1)

          TweenMax.to(helpmenu, 3, {y:350, startAt:{y:600}, ease:Elastic.easeOut});

}

function xbuttonclick(event:MouseEvent):void{

          helpmenu.visible=false;

}

function answerclick(event:MouseEvent):void{

          answermenu.visible=true;

          helpmenu.visible=false;

          setChildIndex(answermenu,numChildren - 1)

          TweenMax.to(answermenu, 3, {y:350, startAt:{y:600}, ease:Elastic.easeOut});

}

function xanswerbuttonclick(event:MouseEvent):void{

          answermenu.visible=false;

          helpmenu.visible=false;

}

//sound items

//function mainclicksoundplay():void{

//mainclicksound.play();

//}

HandVoodoo
Participating Frequently
October 19, 2011

It will work. Although I personally never write any code on the timeline (so my classes may seem different), I hate the flash IDE with all of my heart. I do all of my coding in FlashDevelop.

..Anyway.

Everything in your code looks good.

The only thing I would suggest is putting all of the 'same' display objects in an Array or Vector, so you can iterate through them easily. So rather than having a giant if/else if/else statement, you can have one clean for each statement.

The only issue I see is that you seem to be creating all the instances of your MovieClips on the stage? So it may be a bit 'harder' to create the array.

You could create a vector called hitTestVector, which holds all the clips you want to hittest against.

ie

var hitTestVector:Vector.<MovieClip> = new <MovieClip>[MovieClip(table), MovieClip(clockbottom), MovieClip(clockbottom1), MovieClip(clockbottom2)/*..the rest */];

This doesnt create duplicates, just places a pointer to them inside the vector.

Then, when you call a function (within scope of this var, where ever it is), you can iterate through your vector quickly, without a nested if statement.

private function someFunction(e:* = null):void {

          for each(var clip:MovieClip in hitTestVector) {

                    //clip will be an instance of a movie clip you created on the stage earlier.

                    if (clip.hitTestPoint(mouseX, mouseY, true)) {

                              //do your conditional logic here than

                              return; //so you dont continue the logic if found before the last item in the vector.

                    }

}

The most important thing is that you create that initial vector or array with the 'batch' items you want to interate a test through.