0

Im working on a school project and I need to get collision between an array of particles and a box, here is my current code:

for(var i : int = 0; i < particles.length; i++)
{
    particles[i].update();

    if(Particle.hitTestPoint(Square))
    {
        particle = particles.shift();
        particle.destroy();
    }
}

I get an error telling me that im trying to call an undefined method (hittestpoint) through a reference with static type class.

Any help would be greatly appreciated, thank!

Here is the code for the Particle.

package

{ import flash.display.*;

public class Particle
{
    public var clip : DisplayObject;

    public var xVel : Number = 0;
    public var yVel : Number = 0;

    public var drag : Number = 1;

    public var gravity : Number = 0.0;
    public var shrink : Number = 1;
    public var fade : Number = 0;
    public function Particle(symbolclass : Class, target : DisplayObjectContainer, xpos : Number, ypos : Number)
    {
        clip = new symbolclass();

        target.addChild(clip);

        clip.x= xpos;
        clip.y= ypos;

    }       
    public function update() : void
    {
        clip.x += xVel;
        clip.y += yVel;

        xVel *= drag;
        yVel *= drag;


        yVel+=gravity;

        clip.scaleX *= shrink;
        clip.scaleY *= shrink;

        clip.alpha -= fade;
    }
    public function destroy() :void
    {
        clip.parent.removeChild(clip);
    }

}

}

I used this tutorial for the particles, and my goal is to add collision to them

  • 1
    In the first line in the loop, you refer to `particles[i]`, but then you refer to `Particle` or `particle`. Why don't you just try to be more consistent, and see what happens? – Sam DeHaan May 21 '12 at 14:57
  • Thanks for the suggestion, I made the change and it comes up with an error saying property hittestpoint not found on Particle – FieryAvocado May 21 '12 at 15:05

3 Answers3

1

Edit: Updated my answer now that Particle code was posted.

Your Particle class doesn't define hitTestPoint. In addition to that, you are calling a static method which I don't think you want here... you want an instance method.

Make sure you define hitTestPoint in your Particle class. Then, instead of Particle.hitTestPoint, try particle.hitTestPoint (note upper case, which refers to the class is changed to lower-case which refers to the instance.

Try this:

for(var i : int = 0; i < particles.length; i++)
{
    var particle:Particle = particles[i];
    particles.update();

    if(particle.hitTestPoint(Square))
    {
        particle = particles.shift();
        particle.destroy();
    }
}
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • That definitely makes sense, I tried it with the changes you suggested and paying close attention to the capitalization, still getting an error saying "call to an undefined method" source is (particle.hitTestPoint(Square)). – FieryAvocado May 22 '12 at 15:33
  • @FieryAvocado Well, you posted your `Particle` class, which is great... but there is one problem... `Particle` does not define a function called `hitTestPoint`. That is your problem. Editing my response. – Brian Genisio May 22 '12 at 17:58
  • Thank you! I was able to get the program to work with your help! – FieryAvocado May 23 '12 at 15:47
  • @FieryAvocado Then make sure to mark the answer correct, please. – Brian Genisio May 23 '12 at 15:59
1

The Particle class must extend MovieClip to have the hitTestPoint method. Try changing the class to:

import flash.display.MovieClip;
public class Particle extends MovieClip
{
    //code here

and import specific classes in your code (see MovieClip class as that's the one you are using and it's methods) for better results as good practice.

  • 1
    Both answers are correct. Note, that the `Particle` class doesn't have to extend `MovieClip` it can simply extend `DisplayObject` (that's where the `hitTestPoint()` method is defined). Finally, when calling `hitTestPoint()` you should pass in the proper parameters, the method signature is: `hitTestPoint(x:Number, y:Number, shapeFlag:Boolean = false):Boolean` – Sunil D. May 22 '12 at 17:08
  • CheeseWarlock is also right: he needs to test object collision if "square" is an actual square object. Also you should pass at least x, y parameters when using hitTestPoint() method. – Kretzu Cata May 22 '12 at 17:30
1

I'm not sure what particle refers to in that code since it's not defined in what you posted, but particles[i] is the particle you want to test.

Your Particle class doesn't have a hitTestPoint function- but it contains a DisplayObject that does. So with those two things in mind, you can call hit tests using particles[i].clip.hitTestPoint.

Next problem: you're trying to pass the class Square to the hitTest rather than an instance. And still, hitTestPoint takes a single point as its argument, not an object like what a square would represent. So you will probably want to use hitTestObject instead.

CheeseWarlock
  • 1,361
  • 1
  • 10
  • 11