Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-12-2009, 04:47 AM   #1
ThreeDollarBill
Registered User
 
Join Date: Jul 2009
Posts: 46
Default using "hitTestPoint" inside a class, won't work

basically, there is a box on stage with an instance name of "box" whose class is called "BoxHitTest"... when i rollover the box, its supposed to trace out: "hitting"... but its not working.

the timeline doesnt contain any code.. the only code is inside the BoxHitTest class...
ActionScript Code:
package {     import flash.display.*;     import flash.events.*;         public class BoxHitTest extends MovieClip     {         public function BoxHitTest()         {             addEventListener(Event.ENTER_FRAME, runEnterFrame);         }                 function runEnterFrame(event:Event):void         {             if(this.hitTestPoint(mouseX, mouseY))             {                 trace("hitting");             }         }     } }
what's wrong with my code?

if i type the code on the main timeline below, then it works..
ActionScript Code:
stage.addEventListener(Event.ENTER_FRAME, runEnterFrame); function runEnterFrame(event:Event):void {     if(box.hitTestPoint(mouseX, mouseY))     {         trace("hitting");     } }

so how should i use hitTestPoint inside a class to make it work properly?
ThreeDollarBill is offline   Reply With Quote
Old 08-12-2009, 05:40 AM   #2
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

ActionScript Code:
package {     import flash.display.*;     import flash.events.*;         public class BoxHitTest extends MovieClip     {         public function BoxHitTest()         {             addEventListener(Event.ENTER_FRAME, runEnterFrame);         }                 function runEnterFrame(event:Event):void         {             if(this.hitTestPoint(parent.mouseX, parent.mouseY))             {                 trace("hitting");             }         }     } }
Notice the addition of "parent" in the mouseX and mouseY properties above. The hitTestPoint function only works with global coordinates. Within your class, mouseX and mouseY (without "parent" in front of them) would be properties of the BoxHitTest class itself, and as such, they are local (not global) coordinates.

When you used the same properties in your timeline code, mouseX and mouseY were properties of the main timeline, and as such, they are global coordinates, and that's why they worked.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 08-12-2009, 06:26 AM   #3
ThreeDollarBill
Registered User
 
Join Date: Jul 2009
Posts: 46
Default

ahhh... thanks!!
ThreeDollarBill is offline   Reply With Quote
Old 08-12-2009, 02:23 PM   #4
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,682
Send a message via Skype™ to henke37
Default

I recomend taking the coordinates directly from the stage instead of from the parent for the simple reason that the parent are not always the stage.
henke37 is online now   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 03:42 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.