views:

41

answers:

1

Hi,

I'm currently building the damage mechanic for my game. Two classes handle this, the 'hurt' class, and the 'collision' class. The hurt class passes an instance of itself to the 'hurtCollision' method of the collision class, so that the collision class can detect a collision between the player and that instance of hurt (which is a display object). When I try to access that function however, I get this error:

1061: Call to a possibly undefined method hurtCollision through a reference with static type Class.

Here are the two classes:

The collision class (you can just ctrl f to hurtCollision):

package

{

import flash.display.MovieClip;

public class Collision extends MovieClip
{
 private var e:*;
 static public var _playerX:*;
 static public var _playerY:*;
 private var HIT_BOTTOM_1X:Number;
 private var HIT_BOTTOM_2X:Number;
 private var HIT_BOTTOM_3X:Number;
 private var HIT_TOP_1X:Number;
 private var HIT_TOP_2X:Number;
 private var HIT_TOP_3X:Number;
 private var HIT_R_1X:Number;
 private var HIT_R_2X:Number;
 private var HIT_R_3X:Number;
 private var HIT_L_1X:Number;
 private var HIT_L_2X:Number;
 private var HIT_L_3X:Number;
 private var HIT_BOTTOM_1Y:Number;
 private var HIT_BOTTOM_2Y:Number;
 private var HIT_BOTTOM_3Y:Number;
 private var HIT_TOP_1Y:Number;
 private var HIT_TOP_2Y:Number;
 private var HIT_TOP_3Y:Number;
 private var HIT_R_1Y:Number;
 private var HIT_R_2Y:Number;
 private var HIT_R_3Y:Number;
 private var HIT_L_1Y:Number;
 private var HIT_L_2Y:Number;
 private var HIT_L_3Y:Number;

 public function Collision(enginePass:*)
 {       
 e = enginePass;
 }
 public function detectCollisions(object:*):void
 {
  _playerX = e._player.x;
  _playerY = e._player.y;
  HIT_BOTTOM_1X = object.x - object.width/2;
  HIT_BOTTOM_2X = object.x;
  HIT_BOTTOM_3X = object.x + object.width/2;
  HIT_TOP_1X = object.x - object.width/2;
  HIT_TOP_2X = object.x;
  HIT_TOP_3X = object.x + object.width/2;
  HIT_R_1X = object.x + object.width/2;
  HIT_R_2X = object.x + object.width/2;
  HIT_R_3X = object.x + object.width/2;
  HIT_L_1X = object.x - object.width/2;
  HIT_L_2X = object.x - object.width/2;
  HIT_L_3X = object.x - object.width/2;

  HIT_BOTTOM_1Y = object.y + object.height/2;
  HIT_BOTTOM_2Y = object.y + object.height/2;
  HIT_BOTTOM_3Y = object.y + object.height/2;
  HIT_TOP_1Y = object.y - object.height/2;
  HIT_TOP_2Y = object.y - object.height/2;
  HIT_TOP_3Y = object.y - object.height/2;
  HIT_R_1Y = object.y + object.height/2 - object.height/4;
  HIT_R_2Y = object.y;
  HIT_R_3Y = object.y - object.height/2 + object.height/4;
  HIT_L_1Y = object.y + object.height/2 - object.height/4;
  HIT_L_2Y = object.y;
  HIT_L_3Y = object.y - object.height/2 - object.height/4;

  if(e._ground.hitTestPoint(HIT_BOTTOM_1X,HIT_BOTTOM_1Y,true) || e._ground.hitTestPoint(HIT_BOTTOM_2X,HIT_BOTTOM_2Y,true) 
  || e._ground.hitTestPoint(HIT_BOTTOM_3X,HIT_BOTTOM_3Y,true))
  {
   e._touchGround = true;
   if(e._vy < 0)
   {
    e._vy = 0;
   }
  }
  if(e._ground.hitTestPoint(HIT_TOP_1X,HIT_TOP_1Y,true) || e._ground.hitTestPoint(HIT_TOP_2X,HIT_TOP_2Y,true) 
  || e._ground.hitTestPoint(HIT_TOP_3X,HIT_TOP_3Y,true))
  {
   e._jump = false;
   e._vy = -(e._vy) - 5;
  }

   if(e._ground.hitTestPoint(HIT_R_1X,HIT_R_1Y,true) || e._ground.hitTestPoint(HIT_R_2X,HIT_R_2Y,true) 
  || e._ground.hitTestPoint(HIT_R_3X,HIT_R_3Y,true))
  {
   if(e._vx > 0)
   {
    e._vy += e._vx;
   }
   if(e._ground.hitTestPoint(HIT_TOP_3X, HIT_TOP_3Y, true))
   {
    if(e._vx > 0)
    {
    e._vx = -(e._vx) *2;
    }
   }
  }
  if(e._ground.hitTestPoint(HIT_L_1X,HIT_L_1Y,true) || e._ground.hitTestPoint(HIT_L_2X,HIT_L_2Y,true) 
  || e._ground.hitTestPoint(HIT_L_3X,HIT_L_3Y,true))
  {
   if(e._vx < 0)
   {
    e._vy += Math.abs(e._vx);
   }
   if(e._ground.hitTestPoint(HIT_TOP_1X, HIT_TOP_1Y, true))
   {
    if(e._vx < 0)
    {
    e._vx = -(e._vx) *2;
    }
   }
  }
  if(e._ground.hitTestPoint(HIT_TOP_3X, HIT_TOP_3Y, true) && e._ground.hitTestPoint(HIT_BOTTOM_3X, HIT_BOTTOM_3Y, true))
  {
   e._vy += 20;
  }
  if(e._ground.hitTestPoint(HIT_TOP_1X, HIT_TOP_1Y, true) && e._ground.hitTestPoint(HIT_BOTTOM_1X, HIT_BOTTOM_1Y, true))
  {
   e._vy += 20;
  }
  if(e._ground.hitTestPoint(e._player.x,e._player.y, true))
  {
   e._vy = -(e._vy);
   e._vx = -(e._vx);
  }
  else
  {
   if(!(e._ground.hitTestPoint(HIT_BOTTOM_1X,HIT_BOTTOM_1Y,true) || e._ground.hitTestPoint(HIT_BOTTOM_2X,HIT_BOTTOM_2Y,true) 
  || e._ground.hitTestPoint(HIT_BOTTOM_3X,HIT_BOTTOM_3Y,true)))
   {
   e._vy -= 1;
   e._touchGround = false;
   }
  }
 }

 public function hurtCollision(hurtObject:*)
 {
  if(hurtObject.hitTestPoint(HIT_BOTTOM_1X,HIT_BOTTOM_1Y,true) || hurtObject.hitTestPoint(HIT_BOTTOM_2X,HIT_BOTTOM_2Y,true) 
  || hurtObject.hitTestPoint(HIT_BOTTOM_3X,HIT_BOTTOM_3Y,true))
  {
   e.hurtPlayer();
  }
  if(hurtObject.hitTestPoint(HIT_TOP_1X,HIT_TOP_1Y,true) || hurtObject.hitTestPoint(HIT_TOP_2X,HIT_TOP_2Y,true) 
  || hurtObject.hitTestPoint(HIT_TOP_3X,HIT_TOP_3Y,true))
  {
   e.hurtPlayer();
  }

   if(hurtObject.hitTestPoint(HIT_R_1X,HIT_R_1Y,true) || hurtObject.hitTestPoint(HIT_R_2X,HIT_R_2Y,true) 
  || hurtObject.hitTestPoint(HIT_R_3X,HIT_R_3Y,true))
  {
   e.hurtPlayer();
  }
  if(hurtObject.hitTestPoint(HIT_L_1X,HIT_L_1Y,true) || hurtObject.hitTestPoint(HIT_L_2X,HIT_L_2Y,true) 
  || hurtObject.hitTestPoint(HIT_L_3X,HIT_L_3Y,true))
  {
   e.hurtPlayer();
  }
 }
}

}

The hurt class:

package

{ import flash.display.MovieClip; import flash.events.Event; import Collision;

public class Hurt extends MovieClip
{
 public function Hurt() 
 {
  addEventListener(Event.ENTER_FRAME, enterFrame);
 }
 private function enterFrame(e:Event)
 {
  Collision.hurtCollision(this);
 }
}

}

EDIT: Just to let you all know, e.hurtPlayer is just the document class method that controls the player's health.

+4  A: 

Your problem lies here:

Collision.hurtCollision(this);

hurtCollision isn't a class method, it's an instance method. If you want Collision to be more like a utility class (as opposed to having to create instances to use the methods) then you probably want to do public static function ... instead of just public function.

Read up on class methods for more.

Reading your code more closely you've designed it in such a way that the constructor takes a parameter that you use in the methods. This will either need to be rethought (pass that thing into the methods themselves, maybe?) or you can just go the instance route:

new Collision( e ).hurtCollision( this );

... You might want to keep the instance around longer than just for the one call if you use it a lot, of course.

thenduks
Thanks for the answer. Changing the method to static takes away the error, but all of the properties that I'm using become undefined. So if the function is static, I can't access any of the properties in that class? I'm not sure if this is what you're touching on in your second paragraph...
Miles
Let me clarify a bit. I need to pass both the instance of the hurt class, and the instance of the engine class into the hurtCollision method. I don't see how I could possibly pass these parameters in to the collision class from two separate classes while hurtCollision is static? When it wasn't static, I could already use the properties that had been passed from the engine class. Is there another workaround?
Miles
Yea, well, you aren't ever calling the constructor so there's no way for the instance variables to get set (your properties). Like I said in the second paragraph you should be able to just pass what you're currently passing to your constructor to the methods directly. There are other ways but they get complex, best to buy an AS3 book or something to cover all the bases in that case.
thenduks
Oh I see, you mean the internal properties, `private var BLAH`, etc... Just make those static too!
thenduks
Ah! Works like a charm, thank you so much.
Miles