2010-05-17 84 views
0

基本上我正在通過一本叫做..Foundation Actionscript 3.0動畫的書,讓事情移動。1136:參數數量不正確。預計0. AS3 Flash Cs4

我現在在第9章 - 碰撞檢測。在我的代碼的兩行我得到1135錯誤,讓我知道我有不正確的參數數量。有人可以幫我解釋爲什麼這可能是?

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    public class Bubbles extends Sprite 
    { 
     private var balls:Array; 
     private var numBalls:Number = 10; 
     private var centerBall:Ball; 
     private var bounce:Number = -1; 
     private var spring:Number = 0.2; 

     public function Bubbles() 
     { 
      init(); 
     } 

     private function init():void 
     { 
      balls = new Array(); 
      centerBall = new Ball(100, 0xcccccc); 
      addChild(centerBall); 
      centerBall.x = stage.stageWidth/2; 
      centerBall.y = stage.stageHeight/2; 
      for(var i:uint = 0; i < numBalls; i++) 
      { 
       var ball:Ball = new Ball(Math.random() * 
       40 + 5, 
       Math.random() * 0xffffff); 
       ball.x = Math.random() * stage.stageWidth; 
       ball.y = Math.random() * stage.stageHeight; 
       ball.vx = Math.random() * 6 - 3; 
       ball.vy = Math.random() * 6 - 3; 
       addChild(ball); 
       balls.push(ball); 
      } 
      addEventListener(Event.ENTER_FRAME, onEnterFrame); 
     } 

     private function onEnterFrame(event:Event):void 
     { 
      for(var i:uint = 0; i < numBalls; i++) 
      { 
       var ball:Ball = balls[i]; 
       move(ball); 
       var dx:Number = ball.x - centerBall.x; 
       var dy:Number = ball.y - centerBall.y; 
       var dist:Number = Math.sqrt(dx * dx + dy * dy); 
       var minDist:Number = ball.radius + centerBall.radius; 
       if(dist < minDist) 
       { 
        var angle:Number = Math.atan2(dy, dx); 
        var tx:Number = centerBall.x + 
        Math.cos(angle) * minDist; 
        var ty:Number = centerBall.y + 
        Math.sin(angle) * minDist; 
        ball.vx += (tx - ball.x) * spring; 
        ball.vy += (ty - ball.y) * spring; 
       } 
      } 
     } 
     // Having Trouble Here: 
     private function move(ball:Ball):void 
     { 
      ball.x += ball.vx; 
      ball.y += ball.vy; 
      if(ball.x + ball.radius > stage.stageWidth) 
      { 
       ball.x = stage.stageWidth - ball.radius; 
       ball.vx *= bounce; 
      } 
      else if(ball.x - ball.radius < 0) 
      { 
       ball.x = ball.radius; 
       ball.vx *= bounce; 
      } 
      // Having Trouble Here: 
      if(ball.y + ball.radius > stage.stageHeight) 
      { 
       ball.y = stage.stageHeight - ball.radius; 
       ball.vy *= bounce; 
      } 
      else if(ball.y - ball.radius < 0) 
      { 
       ball.y = ball.radius; 
       ball.vy *= bounce; 
      } 
     } 
    } 
} 

我已經指出了我遇到的問題。

+0

當我創建了自己的球類來測試一下上面的代碼,它不拋出任何編譯錯誤。上面的星號線似乎也不是我可能的錯誤來源。也許你可能想編輯你的問題並添加Ball類的代碼? – anonymous 2010-05-17 01:39:39

+0

同意。編譯器可能會抱怨這些行,但麻煩的原因是其他地方.. – fenomas 2010-05-17 01:45:07

+1

我敢打賭,「球」的構造函數沒有任何參數。 – Amarghosh 2010-05-17 06:44:25

回答

0

以下行是罪魁禍首。

centerBall = new Ball(100, 0xcccccc); 
var ball:Ball = new Ball(Math.random() * 40 + 5, Math.random() * 0xffffff); 

您試圖將參數傳遞給Ball的構造函數,該構造函數接受零參數。將它們更改爲

centerBall = new Ball(); 
var ball:Ball = new Ball(); 

它將工作。如果您確實想要傳遞初始化詳細信息,請創建一個Ball.as文件並將其分配給舞臺上的影片剪輯。現在聲明Ball類的構造函數來接受這兩個參數,並在構造函數中初始化變量(半徑和顏色)。


Ball.as看起來像

package 
{ 
    public class Ball extends MovieClip 
    { 
    public function Ball(r:Number, col:uint) 
    { 
     this.radius = r; 
     this.color = col; 
    } 
    } 
} 
+0

Ball.as代碼是什麼?加上ive已經將這段代碼Bubbles.as分配給舞臺上的影片剪輯,我將如何解決這個問題? – charmaine 2010-05-18 09:34:34

+0

編輯球符號並將Ball指定爲該動作的類名稱。 – Amarghosh 2010-05-18 10:58:16

+0

package { \t import flash.display.Sprite; \t public class Ball extends Sprite \t { \t \t private var radius:Number; \t \t private var color:uint; \t \t public var vx:Number = 0; \t \t public var vy:Number = 0; \t \t \t 公共\t功能球(半徑:總數= 40,顏色:UINT =爲0xFF0000){ \t \t \t this.radius =半徑; \t \t \t這個。顏色=顏色; \t \t \t init(); \t \t} \t \t公共功能的init():無效{ \t \t \t graphics.beginFill(顏色); \t \t \t graphics.drawCircle(0,0,radius); \t \t \t graphics.endFill(); \t \t} \t} } – charmaine 2010-05-18 12:12:01