2013-02-02 41 views
0

你好,大家好我有一個問題,不知道如何解決它:(誰能告訴我,我該怎麼辦呢的Flash AS3:構造函數必須是實例方法

構造函數必須是實例方法

因此,這裏是我的代碼:

package 
{ 
    import com.coreyoneil.collision.CollisionList; 
    import flash.events.Event; 
    import flash.display.Sprite;  

    public class terrain extends Sprite 
    { 
     private var wheel:Ball; 
     private var collisionList:CollisionList; 
     private var speed:Number; 

     private const GRAVITY:Number = .75; 
     private const FRICTION:Number = .98; 
     private const IMMOVABLE:Number = 10000; 


     public function terrain():void 
     { 
      if(stage == null) 
      { 
       addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true); 
       addEventListener(Event.REMOVED_FROM_STAGE, clean, false, 0, true); 
      } 
      else 
      { 
       init(); 
      } 
     } 

     private function init(e:Event = null):void 
     { 
      collisionList = new CollisionList(terrain); 

      wheel = new wheel(10); 
      wheel.mass = IMMOVABLE * 2; 
      addChild(wheel); 
      collisionList.addItem(wheel); 
      wheel.x = 30; 
      wheel.y = 10; 

      speed = 0; 

      terrain.graphics.lineStyle(15); 

      addEventListener(Event.ENTER_FRAME, updateScene); 
     } 


     private function updateScene(e:Event):void 
     {   
      var collisions:Array = collisionList.checkCollisions(); 

      if(collisions.length) 
      { 
       var collision:Object = collisions[0]; 
       var angle:Number = collision.angle; 
       var overlap:int = collision.overlapping.length; 

       var sin:Number = Math.sin(angle); 
       var cos:Number = Math.cos(angle); 

       var vx0:Number = wheel.vx * cos + wheel.vy * sin; 
       var vy0:Number = wheel.vy * cos - wheel.vx * sin; 

       // Unlike the other examples, here I'm choosing to calculate the amount 
       // of bounce based on the objects' masses, with a default mass of 10000 (IMMOVABLE) 
       // being used for the drawing the wheel is colliding with. As such, the only 
       // real variable in play here is the current vector of the wheel. 
       vx0 = ((wheel.mass - IMMOVABLE) * vx0)/(wheel.mass + IMMOVABLE); 
       wheel.vx = vx0 * cos - vy0 * sin; 
       wheel.vy = vy0 * cos + vx0 * sin; 

       wheel.vx -= cos * overlap /wheel.radius; 
       wheel.vy -= sin * overlap/wheel.radius; 

       wheel.vx += speed; 
      } 
      trace("down"); 
      wheel.vy += GRAVITY; 
      wheel.vy *= FRICTION; 
      wheel.vx *= FRICTION; 

      wheel.x += wheel.vx; 
      wheel.y += wheel.vy; 

      if(wheel.x > stage.stageWidth) wheel.x = stage.stageWidth; 
      if(wheel.x < 0) wheel.x = 0;          
      if(wheel.y > stage.stageHeight - (wheel.height >> 1)) 
      { 
       wheel.y = 10; 
       wheel.x = 30; 
       wheel.vx = wheel.vy = 0; 
      } 

     } 

     private function clean(e:Event):void 
     { 
      removeEventListener(Event.ENTER_FRAME, updateScene); 
     } 




    } 
    } 

裏有一些代碼註釋..只要忽略它我已經使用例如

+0

你的問題是什麼?你有什麼問題? – poepje

+0

它不需要在構造函數中進行if(stage == null)檢查,因爲此時會創建一個新對象,在其構建完成之前無法將其添加到階段。因此stage屬性將在構造函數中始終爲null。只需添加聽衆就足夠了。其他部分永遠不會被評估。 –

回答

3
collisionList = new CollisionList(terrain); 
terrain.graphics.lineStyle(15); 

這是錯誤1026,如果構造函數是靜態的,私有的或在您的情況下用作標識符,則也會引發此錯誤。可以使用this.graphics而不是terrain.graphics,或者只使用graphics.etc(刪除terrain),並將'this'作爲'CollisionList'的參數。
(不相關:最好以大寫字母「Terrain」開頭)

+0

感謝您的建議,但如果我使用這個,我仍然得到第3行相同的錯誤(我開始導入東西) – Andrey

+0

噢是還有另一個:「collisionList = new CollisionList(terrain);」,也將其更改爲新CollisionList(本)。基本上你只需要使用一個類名作爲標識符,如果它有一個靜態屬性或方法,並且來自另一個類,否則它的'this'或者類中沒有。 – chadiik

相關問題