Mailing List
Home
Forum Home
Flash Pro
Subjects
Subject: RE: Firework Effect
Web Service Results
Flash Interface with 10mb xml file
AW: [Flashcoders] Switch/Case vs If/else
AW: [Flashcoders] Switch/Case vs If/else
Flash MX 2004 Sucks
Reading and displaying RSS feeds in Flash MX
Flash and QuickTime VR
Textfield prototype question
XML to Object help
Order of events per frame
MX2004 Dataset itemClassName
memory management removeMovieClip /
Event Dispatcher between classes
Help: MX 2004 How to script a print button to print the entire sli
ScrollPane component doesn 't auto update
setInterval bug identified and fixed
setInterval bug identified and fixed
Listener Object 's best practice
 
hitTest question

hitTest question

2004-03-18       - By Jesse Warden

 Back
Reply:     1     2     3     4     5     6  

Well, with another language, you'd have options, but with Flash, I only see
2, but I know games have been discussed on Flashcoders, so maybe you can get
some answers from the archives.

In the meantime, old school way is your array method:

mc_array = [one_mc, two_mc, three_mc];

function didHit(mc)
{
   var i = mc_array.length;
   while(i--){
       if(mc.hitTest(mc_array[i])){
           return mc_array[i];
       }
   }
   return false;
}

var r = didHit(mc);
if(r != false){
   // you hit r
   trace(mc + " hit the mc: " + r);
}

Or, you could do a tilebased approach; you have a grid of tiles.  Each mc
occupies a tile, and only one tile at a time. That way, you can simply check
the value of an multidimensional array.

if(md_array[3][3] == 1){
   // occupied
}else if(md_array[3][3] == 0){
   // open, move away!
}


   

-- --Original Message-- --
From: Jason Merrique [mailto:j.merrique@(protected)]
Sent: Thursday, March 18, 2004 07:14 AM
To: flashnewbie@(protected)
Subject: RE: [Flashnewbie] hitTest question


Oops, forgot something:

As a workaround, I had planned to put all the relevant mc's into an array,
and then cycle through the array doing a hitTest with each mc. I'm sure
there's a more efficient way though....

Cheers

Json



 __ __  

From: Jason Merrique [mailto:j.merrique@(protected)]
Sent: 18 March 2004 12:12
To: flashnewbie@(protected)
Subject: [Flashnewbie] hitTest question


Hi Chaps,

I've got a quick question:

MovieClip.hitTest()

Does the target need to be specific? i.e. Is it possible to have this return
true if MovieClip collides with *anything*?

The reason for asking is that I've created a movie in which lots of objects
are moving around (randomly) and I don't want them to overlap.

Thanks in advance!

Jason


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=US-ASCII">
<TITLE>Message</TITLE>

<STYLE type=text/css>BODY {
  FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans
-serif
}
#disclaimer {
  FONT-SIZE: 8pt
}
</STYLE>

<META content="MSHTML 6.00.2800.1400" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=211203513-18032004>Well, with another language, you'd have
options, but with Flash, I only see 2, but I know games have been discussed on
Flashcoders, so maybe you can get some answers from the archives.</SPAN></DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004>In the meantime, old school way is your
array method:</SPAN></DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004>mc_array&nbsp;= [one_mc, two_mc,
three_mc];</SPAN></DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004>function didHit(mc)</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>{</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; var i&nbsp;=
mc_array.length;</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; while(i--){</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if(mc.hitTest(mc_array[i])){</SPAN></DIV>
<DIV><SPAN
class=211203513-18032004>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;
return mc_array[i];</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; }</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; return
false;</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>}</SPAN></DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004>var r = didHit(mc);</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>if(r != false){</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; // you hit r</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; trace(mc + " hit the mc:
" + r);</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>}</SPAN></DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004>Or, you could do a tilebased approach; you
have a grid of tiles.&nbsp; Each mc occupies a tile, and only one tile at a
time. That way, you can simply check the value of an multidimensional
array.</SPAN></DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004>if(md_array[3][3] == 1){</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; // occupied</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>}else if(md_array[3][3] == 0){</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; // open, move
away!</SPAN></DIV>
<DIV><SPAN class=211203513-18032004>}</SPAN></DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004></SPAN>&nbsp;</DIV>
<DIV><SPAN class=211203513-18032004>&nbsp;&nbsp;&nbsp; </SPAN></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
 <DIV></DIV>
 <DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left><FONT
 face=Tahoma>-- --Original Message-- --<BR><B>From:</B> Jason Merrique
 [mailto:j.merrique@(protected)] <BR><B>Sent:</B> Thursday, March 18, 2004
 07:14 AM<BR><B>To:</B> flashnewbie@(protected)<BR><B>Subject:</B>
 RE: [Flashnewbie] hitTest question<BR><BR></FONT></DIV>
 <DIV dir=ltr align=left><SPAN class=094251212-18032004>Oops, forgot
 something:</SPAN></DIV>
 <DIV dir=ltr align=left><SPAN class=094251212-18032004></SPAN>&nbsp;</DIV>
 <DIV dir=ltr align=left><SPAN class=094251212-18032004>As a workaround, I had
 planned to put all the relevant mc's into an array, and then cycle through
the
 array doing a hitTest with each mc. I'm sure there's a more efficient way
 though....</SPAN></DIV>
 <DIV dir=ltr align=left><SPAN class=094251212-18032004></SPAN>&nbsp;</DIV>
 <DIV dir=ltr align=left><SPAN class=094251212-18032004>Cheers</SPAN></DIV>
 <DIV dir=ltr align=left><SPAN class=094251212-18032004></SPAN>&nbsp;</DIV>
 <DIV dir=ltr align=left><SPAN class=094251212-18032004>Json</SPAN></DIV>
 <DIV>&nbsp;</DIV><BR>
 <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
   <DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
   <HR tabIndex=-1>
   <FONT face=Tahoma><B>From:</B> Jason Merrique
   [mailto:j.merrique@(protected)] <BR><B>Sent:</B> 18 March 2004
   12:12<BR><B>To:</B> flashnewbie@(protected)<BR><B>Subject:</B>
   [Flashnewbie] hitTest question<BR></FONT><BR></DIV>
   <DIV></DIV>
   <DIV><SPAN class=330570512-18032004>Hi Chaps,</SPAN></DIV>
   <DIV><SPAN class=330570512-18032004></SPAN>&nbsp;</DIV>
   <DIV><SPAN class=330570512-18032004>I've got a quick question:</SPAN></DIV>
   <DIV><SPAN class=330570512-18032004></SPAN>&nbsp;</DIV>
   <DIV><SPAN class=330570512-18032004><SPAN class=heading1>MovieClip.hitTest(
)
   </SPAN></SPAN></DIV>
   <DIV><SPAN class=330570512-18032004><SPAN
   class=heading1></SPAN></SPAN>&nbsp;</DIV>
   <DIV><SPAN class=330570512-18032004><SPAN class=heading1>Does the target
   need to be specific? i.e. Is it possible to have this return true if
   MovieClip collides with *anything*?</SPAN></SPAN></DIV>
   <DIV><SPAN class=330570512-18032004><SPAN
   class=heading1></SPAN></SPAN>&nbsp;</DIV>
   <DIV><SPAN class=330570512-18032004><SPAN class=heading1>The reason for
   asking is that I've created a movie in which lots of objects are moving
   around (randomly) and I don't want them to overlap.</SPAN></SPAN></DIV>
   <DIV><SPAN class=330570512-18032004><SPAN
   class=heading1></SPAN></SPAN>&nbsp;</DIV>
   <DIV><SPAN class=330570512-18032004><SPAN class=heading1>Thanks in
   advance!</SPAN></SPAN></DIV>
   <DIV><SPAN class=330570512-18032004><SPAN
   class=heading1></SPAN></SPAN>&nbsp;</DIV>
   <DIV><SPAN class=330570512-18032004><SPAN
   class=heading1>Jason</SPAN></SPAN></DIV></BLOCKQUOTE></BLOCKQUOTE></BODY><
/HTML>