Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

New HitTest method of Actionscript 3.0

This is a discussion on New HitTest method of Actionscript 3.0 within the Tutorials forums, part of the Flash English category; The good old hitTest " method has been removed, or better, it"s been split into 2 separate methods called hitTestObject ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  5 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 08-10-07, 16:06
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
New HitTest method of Actionscript 3.0

The good old hitTest " method has been removed, or better, it"s been split into 2 separate methods called hitTestObject and hitTestPoint.
The use of these methods isn"t much different from the old hitTest.
Let"s see how they work"
The Document Class I"ve created is this:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class ProvaNuovoHitTest extends MovieClip
	{
		public function ProvaNuovoHitTest()
		{
			primoCaso();
			secondoCaso();
		}
		
		private function primoCaso():void
		{
			black_mc.addEventListener(Event.ENTER_FRAME,controllaMouse);
		}
		
		private function controllaMouse(e:Event):void
		{
			if(black_mc.hitTestPoint(mouseX,mouseY,true))
				debug_0_txt.text='si';
			else
				debug_0_txt.text='no';
		}
		
		private function secondoCaso():void
		{
			circle_1_mc.buttonMode=true;
			circle_1_mc.addEventListener(MouseEvent.MOUSE_DOWN,sposta);
			circle_1_mc.addEventListener(MouseEvent.MOUSE_UP,ferma);
		}
		
		private function sposta(m:MouseEvent):void
		{
			circle_1_mc.startDrag(true);
			circle_1_mc.addEventListener(Event.ENTER_FRAME,controllaContatto);
		}
		
		private function controllaContatto(e:Event):void
		{
			if(circle_1_mc.hitTestObject(circle_0_mc))
				debug_1_txt.text='si';
			else
				debug_1_txt.text='no';
		}
		
		private function ferma(m:MouseEvent):void
		{
			circle_1_mc.stopDrag();
			circle_1_mc.removeEventListener(Event.ENTER_FRAME,controllaContatto);
		}
	}
}
Here is the result:







Let"s analyze the code:

In the primoCaso ( method ) the script checks if the clip hits the mouse with the event ENTER_FRAME.
In this case I use the method hitTestPoint with its 3 parameters:
-x of the point
-y of the point
-forma: it"s a Boolean value ( true/false ) with which you can decide if you want to apply hitTestPoint only to the real shape of the object or to the containing frame.
I suggest you try to change that value to better understand the difference J

In the secondoCaso ( method ) I control if the red MovieClip ( moveable ) hits the blue one using the hitTestObject method with its only parameter ( the actual object with which we"re checking the collision ).
With the hitTestObject method, you can"t choose to apply it to the containing frame. In order to do so, advanced trigonometry algorithms and complicated Actionscript is required ( we"ll see it later ).

Later !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:39..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 24-01-08, 08:08
Junior Member
 
Join Date: Jan 2008
Posts: 1
Rep Power: 0
esviridc is on a distinguished road
Re: New HitTest method of Actionscript 3.0

hey!
i'm a new member, sorta a beginner at all of this. I was able to get the other dragging tutorials to work, but I am not sure as to how to actually transfer this code into my actions. the "private function" and the italian is throwing me off... please help.

by the way: this is an amazing website! much more helpful than anything i have come across as of yet. thank you for doing this for people!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 25-01-08, 09:20
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: New HitTest method of Actionscript 3.0

Hi and welcome

Do you mean transfer the code into timeline ?
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-03-08, 23:06
Junior Member
 
Join Date: Dec 2007
Posts: 1
Rep Power: 0
anujsharma181 is on a distinguished road
Re: New HitTest method of Actionscript 3.0

Hi
I am trying to drag and drop multiple instances of UILoader on stage. My goal is if user puts UILoader partially on top of other UILoader , it should just stay on top of it. If user puts on top of UILoader more than the central point of other UILoader, the other UILoader will change the color of its border and on mouse release gets replaced by the placed UILoader.
I am successful in getting all this but I am getting inconsistent results as an output. Sometimes I have to put exactly on top of other UILoader to replace it and sometimes I just touch the UILoader and it shows red border and gets replaced. I used the logic of calculating the center of rectangle(my UILoader container) and use hitPoint method, and as soon as dragged UILoader hits the calculated center of UILoader, it gets replaced.
Please help me out in where I am messing things up.Any better idea than this will also be highly appreciated.
( Following is the function which is telling my UILoader to replace of stopDrag.)
Thanks a lot
Anuj
/
Code:
*************************CODE***************/
function dropUILoader(e:MouseEvent):void
        {
           
            if(e.target    is UILoader)
            {
                var myUILoader:UILoader = e.target as UILoader;

            if (mouseY<746)
            {
                myUILoader.stopDrag();
                //Putting video on top of already existed videos
                var trackChild:Number=container.getChildIndex(myUILoad  er);
                var childContainer:DisplayObject= container.getChildAt(trackChild);
                for (var z:Number=0; z<=container.numChildren-1; z++)
                {
                    var restChild:DisplayObject=container.getChildAt(z);
                   
                    if (childContainer!=restChild)
                    {
                        if(restChild is UILoader)
                        {
                            var cRC:UILoader=restChild as UILoader;                               
                            var regcRCx:Number=cRC.x;
                            var regcRCy:Number=cRC.y;
                           
                            var regcRCWidth:Number=cRC.width;
                            var regcRCHeight:Number=cRC.height;
                                               
if(childContainer.hitTestPoint(((regcRCx+regcRCWid th)/2),(regcRCy+regcRCHeight)/2)==true) 
                            {
                                trace("Totally Hit");   
                                container.removeChild(cRC);
                            }   
                            else
                            {
                                trace("Partially Hit");   
                            }
                        }
                    }
                }
            }               
            else
            {
                myUILoader.unload();
                container.removeChild(myUILoader);
            }
            }
        }

Last edited by Flep; 13-03-08 at 04:29.. Reason: added code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3.0 perlinNoise method Flep Tutorials 6 05-12-08 10:05
Dynamically adding a method to an Object with Actionscript 3.0 Flep Tutorials 0 14-10-08 06:26
HitTest con Flash CS3 Flep Articoli e tutorials 3 21-04-08 09:33
BitmapData - draw method of Actionscript 3.0 Flep Tutorials 0 09-10-07 19:43
curveTo Method with Actionscript 3.0 Flep Tutorials 0 29-09-07 10:18


All times are GMT. The time now is 17:41.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap