Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-23-2011, 08:46 PM   #1
kdrysdale
Member
 
Join Date: Oct 2009
Posts: 65
Default [AS3] Hit Test and MC name in Variable

Hi All,

please can someone help me with this?

I'm creating a simple drag/drop face game, the drag/drop on the mc's is powered by an external .as file, the following code is on my main timeline.

Ok, the issue I have (apart from my hideous coding) are the first two event listeners and the way I'm setting those first three variables.

The first EventListener and function is getting the name of the current clicked movie clip and storing it in the variable currentClip.

I then want to use the mc name stored in the currentClip variable to use in the hit test on the next EventListener/function down.

I get the following error:

TypeError: Error #1006: value is not a function.
at dragDropTest2_fla::MainTimeline/myFunction().

It works if I just use the MC name but not when I insert the variable which I thought would hold the MC name.

Any ideas, I'm struggling!

cheers

ActionScript Code:
var eyesArray:Array = new Array("e_blank1_mc","e_blank2_mc","e_eyes1_mc", "e_eyes2_mc", "e_eyes3_mc"); var mouthArray:Array = new Array("m_blank_mc","m_mouth1_mc", "m_mouth2_mc", "m_mouth2_mc"); var whichArray = eyesArray; var whichPart = eyesArea_mc; var currentClip = e_eyes1_mc; stage.addEventListener(MouseEvent.MOUSE_DOWN, findTarget);         function findTarget(e:MouseEvent):void         {        currentClip = e.target.name;         } stage.addEventListener(Event.ENTER_FRAME,myFunction); function myFunction(event:Event) {     if(currentClip.hitTestObject(whichPart)) {          trace("hit")        } } function assignTargets():void {     currentClip.target = whichPart; } assignTargets(); stage.addEventListener(MouseEvent.MOUSE_UP, whatArray);         function whatArray(e:MouseEvent):void         {             switch (e.target.name.charAt(0)){ case "e": trace("eyesArray chosen"); whichArray = eyesArray; whichPart = eyesArea_mc; break; case "t": trace("teethArray chosen"); whichArray = mouthArray; whichPart = mouth_mc; break; } for (var i=0; i<whichArray.length; i++)             {                 if( whichArray[i].toString() == e.target.name)                 {                     whichPart.gotoAndStop(i);                 }                 }         }
kdrysdale is offline   Reply With Quote
Old 05-23-2011, 08:58 PM   #2
benjio21
Senior Member
 
Join Date: Jan 2011
Location: uk
Posts: 502
Send a message via Skype™ to benjio21
Default

Use this instead in the myfunction.

ActionScript Code:
MovieClip(whichpart)

Ben
__________________
as3 is like marmite ("you either hate it or love it");
benjio21 is offline   Reply With Quote
Old 05-23-2011, 09:19 PM   #3
kdrysdale
Member
 
Join Date: Oct 2009
Posts: 65
Default

Hi cheers for the reply, I've tried that but unsure of the syntax to use from your suggestion I just keep getting the same error.
kdrysdale is offline   Reply With Quote
Old 05-23-2011, 09:23 PM   #4
benjio21
Senior Member
 
Join Date: Jan 2011
Location: uk
Posts: 502
Send a message via Skype™ to benjio21
Default

ActionScript Code:
* * if(currentClip.hitTestObject(MovieClip(whichPart))) {
__________________
as3 is like marmite ("you either hate it or love it");
benjio21 is offline   Reply With Quote
Old 05-23-2011, 09:34 PM   #5
kdrysdale
Member
 
Join Date: Oct 2009
Posts: 65
Default

Yep, I've tried exactly that and I get the same error. If I use the actual movieclip name for for 'currentClip' which is e_eyes1_mc everything works, the 'whichPart' seems to be working ok with the MC name stored.

cheers
kdrysdale is offline   Reply With Quote
Old 05-23-2011, 09:37 PM   #6
benjio21
Senior Member
 
Join Date: Jan 2011
Location: uk
Posts: 502
Send a message via Skype™ to benjio21
Default

here a lil example i just done to test n works fine

ActionScript Code:
import flash.events.Event; import flash.display.MovieClip; import flash.events.MouseEvent; var currentClip = mc1; var whichPart = mc2; stage.addEventListener(Event.ENTER_FRAME, myfunction); MovieClip(currentClip).addEventListener(MouseEvent.MOUSE_DOWN, move1); MovieClip(currentClip).addEventListener(MouseEvent.MOUSE_UP, move2); function move1(e:MouseEvent):void {     currentClip.startDrag(); } function move2(e:MouseEvent):void {     currentClip.stopDrag(); } function myfunction(e:Event):void {     if(MovieClip(currentClip).hitTestObject(MovieClip(whichPart)))        {            trace("hit");        }                                         }
__________________
as3 is like marmite ("you either hate it or love it");
benjio21 is offline   Reply With Quote
Old 05-24-2011, 08:30 PM   #7
kdrysdale
Member
 
Join Date: Oct 2009
Posts: 65
Default

Hi, thanks for that, I've tried it and it works what you are suggesting but I think I'm trying to do something slightly different.

What I was trying to do was get the current movie clip that is clicked using e.target.name and hold it in the variable 'currentClip' this way I can use the hit test with whatever target I click on the stage.

I've simplified things below, if I try and use the var 'currentClip' in the if statement I get the following error:

TypeError: Error #1034: Type Coercion failed: cannot convert "eyes_mc" to flash.display.MovieClip.at dragDropTestDebug_fla::MainTimeline/myFunction()

if I use just the movieClip name eyes_mc then it works, should the var currentClip not hold eyes_mc from my code below. The 'currentClip' value should change when I click a new object using e.target.name from my mouse down listener?


ActionScript Code:
var whichPart = eyesArea_mc; var currentClip = eyes_mc; stage.addEventListener(Event.ENTER_FRAME,myFunction); function myFunction(e:Event) {  if(MovieClip(currentClip).hitTestObject(MovieClip(whichPart))) {     trace("hit");  } } stage.addEventListener(MouseEvent.MOUSE_DOWN, findTarget);         function findTarget(e:MouseEvent):void         {        currentClip = e.target.name;         trace(e.target.name);         }
kdrysdale is offline   Reply With Quote
Old 05-25-2011, 02:36 PM   #8
benjio21
Senior Member
 
Join Date: Jan 2011
Location: uk
Posts: 502
Send a message via Skype™ to benjio21
Default

hay mate. sorry must of got the wrong idea. ok here we go here a lil example i just nocked up.
each mc is linked with the DragDrop class. the mcs instance names are mc1 - mc5. and are all on the stage.
have a play any probs give us a shout.

DragDrop class
ActionScript Code:
package {     import flash.display.*;     import flash.events.MouseEvent;     public class DragDrop extends Sprite     {         var origX:Number;         var origY:Number;         var target:DisplayObject;         public function DragDrop()         {             origX = x;             origY = y;             addEventListener(MouseEvent.MOUSE_DOWN, drag);                     }         function drop(event:MouseEvent):void         {             stage.removeEventListener(MouseEvent.MOUSE_UP, drop);             stopDrag();                         if(hitTestObject(target))             {                 visible = false;                 target.alpha = 1;                 Object(parent).match();             }                         x = origX;             y = origY;                     }         function drag(event:MouseEvent):void         {             stage.addEventListener(MouseEvent.MOUSE_UP, drop);             startDrag();         }     } }

Main class
ActionScript Code:
package {     import flash.display.*;     import flash.events.*;     public class Main extends MovieClip     {         private var drops:Array;         private var numOfMatches:uint = 0;         private var speed:Number = 25;         public function Main()         {             drops = [mc1,mc2,mc3,mc4,mc5];             var currentObject:DragDrop;             for (var i:uint = 0; i < drops.length; i++)             {                 currentObject = drops[i];                 currentObject.target = getChildByName(currentObject.name + "_target");             }         }         public function match():void         {             numOfMatches++;             trace("matched");         }     } }

Ben
__________________
as3 is like marmite ("you either hate it or love it");
benjio21 is offline   Reply With Quote
Old 05-25-2011, 02:44 PM   #9
kdrysdale
Member
 
Join Date: Oct 2009
Posts: 65
Default

That's Awesome Ben, thanks!!!!!! Looks like it does everything I need and more...
kdrysdale is offline   Reply With Quote
Old 05-25-2011, 02:52 PM   #10
benjio21
Senior Member
 
Join Date: Jan 2011
Location: uk
Posts: 502
Send a message via Skype™ to benjio21
Default

no probs mate.

Ben
__________________
as3 is like marmite ("you either hate it or love it");
benjio21 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:24 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.