Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / Flash Actionscript / January 2006



Tip: Looking for answers? Try searching our database.

Problems with hitTest

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Seidi Jr - 31 Jan 2006 14:10 GMT
I?m trying to create a little game using the hit test function. in the first
part of this game I create a array with some enemies ships using attachMovie -
works fine. I?m having problem with this function:

//verify hitTest against Enemys
    for (n=0; n<enemyArray.length; n++) {
        //test the knight weapon agains enemys
        trace(enemyArray);
        if (this].hitTest(ship.body) == true) {
            wacka = "true 4 Enemy "+enemyArray;
        } else {
            wacka = "false 4 Enemy";
        }
    }

It has been placed inside two areas: inside a:

this.onEnterFrame = function()

and into a:

this.onMouseDown = function()

As result, the hitTest just works with the last enemy MC of the array.
David Stiller - 31 Jan 2006 14:41 GMT
Seidi Jr,

> I?m trying to create a little game using the hit test function.
> in the first part of this game I create a array with some
> enemies ships using attachMovie - works fine.

   Okay.

> I?m having problem with this function:
>
[quoted text clipped - 3 lines]
>  trace(enemyArray);
>  if (this].hitTest(ship.body) == true) {

   Well, here's a problem.  You're using the word "this" with one bracket
on the right side.  Do you mean, simply, "this"?  If so, what does "this"
point to?  If this code is in the main timeline, then "this" refers to the
main timeline (aka _root).  Anything on the Stage at all will always return
true for a hitTest() against the _root.  Is this what you mean to do?

   In addition, what is the for() loop for?  You're not using that counter
variable, n, anywhere.  What are you trying to do, here?

>  wacka = "true 4 Enemy "+enemyArray;

   Here, you're setting a variable, wacka, as a string ("true 4 Enemy")
plus an array, which isn't going to give you a useful array.

> As result, the hitTest just works with the last enemy MC
> of the array.

   You're never stepping through the array.  Here's how arrays work.  An
array is just a container.  It holds whatever you want ... you can put
strings in your array, movie clip references, numbers -- even other arrays.

var myArray:Array = new Array();

   By typing the above, I've just created an array.  Technically speaking,
I have created an instance of the Array class.  This is essentially the same
thing as dragging a movie clip from the Library on to the Stage.  In doing
that, you're creating an instance of the MovieClip class.  What is a class?
In ActionScript, a class defines an object.  If you look up the Array class
entry in the ActionScript Language Reference, you'll see all the
functionality available to all arrays.  Same goes for the MovieClip class,
the Button class, the TextField class, and so on.  Things the object can do
are filed under the Methods heading.  Characteristics the object has are
filed under Properties.  Things the object can react to are filed under
Events.

   In the above code, a variable is established.  This variable is named
myArray.  Effectively, that's our instance name for this Array instance.
The :Array means that our variable is an instance of the Array class.  The
new Array() is our constructor:  it contructs an instance of Array.

   From now on, our variable, myArray, is our "handle" to the Array
instance.  myArray can do anything described in the Array class entry of the
ActionScript Language Reference.  This includes, for example, the
Array.push() method, which adds items to an array.

myArray.push("enemy A");
myArray.push("enemy B");
myArray.push("enemy C");

   Now our array has three items in it, numbered zero through 2.  I see
that's in slot zero (the first slot), you can use trace() and the array
access operator, [].

trace(myArray[0]);
// traces "enemy A" to the Output panel

   Slot one (the second slot) is myArray[1], and so forth.  If you want to
loop through an array with a variable, such as n in your for() loop, you
would substitute n for the numbers.

for (var n=0; n<myArray.length; n++) {
   trace(myArray[n]);
}

   See what's going on?  In your sample code, I don't see that you've even
populated your array (maybe you haven't yet).  But even if you have, I don't
see where you're stepping through your array with your counter variable.

David
stiller (at) quip (dot) net
"Luck is the residue of good design."
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.