2009-11-29 22 views
0

我目前正在爲我的遊戲構建破壞機制。兩個班級處理這個,'受傷'班和'碰撞'班。傷害類將其自身的一個實例傳遞給碰撞類的'hurtCollision'方法,以便碰撞類可以檢測到玩家與傷害實例(這是一個顯示對象)之間的碰撞。當我嘗試但是訪問功能,我得到這個錯誤:爲什麼我在嘗試訪問課程的公共職能時遇到錯誤? (Actionscript 3)

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

這裏有兩類:

碰撞類(你可以按Ctrl F到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(); 
     } 
    } 
} 

}

傷害類:

package 

{ 進口的flash.display.MovieClip; import flash.events.Event;進口碰撞; 進口碰撞;

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

}

編輯:只是爲了讓大家都知道,e.hurtPlayer僅僅是文檔類的方法來控制玩家的健康。

+0

如果您將所有代碼縮進4個空格,它將被正確格式化,fyi。 – rfunduk

回答

4

你的問題就在這裏:

Collision.hurtCollision(this); 

hurtCollision不是一個類的方法,它是一個實例方法。如果你想Collision更像是一個實用工具類(而不是必須創建實例來使用這些方法),那麼你可能想要做public static function ...而不是public function

閱讀更多類方法。

更仔細地閱讀你的代碼,你已經設計了它,使得構造函數接受你在方法中使用的參數。這要麼需要重新考慮(傳遞東西進入方法本身,也許?),或者你可以去實例路線:

new Collision(e).hurtCollision(this); 

...你可能想不僅僅是更長的保持周圍實例當然,如果你使用它的話,那個電話會很多。

+0

感謝您的回答。 將方法更改爲靜態會帶走錯誤,但我使用的所有屬性都變得未定義。所以如果函數是靜態的,我不能訪問該類中的任何屬性?我不確定這是你在第二段中觸及的內容...... – Miles

+0

讓我澄清一下。我需要將傷害類的實例和引擎類的實例都傳遞給hurtCollision方法。我沒有看到我怎麼可能將這些參數從兩個單獨的類傳遞給碰撞類,同時傷害碰撞是靜態的?當它不是靜態的時候,我已經可以使用從引擎類傳入的屬性。是否有另一種解決方法? – Miles

+0

是的,你永遠不會調用構造函數,所以實例變量無法被設置(你的屬性)。就像我在第二段中所說的那樣,您應該能夠將您當前傳遞給構造函數的內容直接傳遞給方法。還有其他方法,但他們變得複雜,最好購買一本AS3書籍或其他東西來涵蓋所有這些案例的基礎。 – rfunduk

相關問題