English 中文(简体)
Why do I get an error when I try to access a public function of a class? (Actionscript 3)
原标题:

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.

最佳回答

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.

问题回答

暂无回答




相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签