1136:参数数量不正确。预期为0. AS3 Flash Cs4(第2轮)

时间:2010-05-19 22:59:36

标签: flash actionscript-3 actionscript

(之前我已经问了这个问题,但我不认为我对我的问题很直接,所以它没有得到解决所以这里再次发生!)

我正在编写一本名为 Foundation Actionscript 3.0动画的书,让事情变得

我现在在第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; 
} 
} 
} 
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; 
} 
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; 
} 
} 
} 
} 

我认为这是由于Ball.as不存在,在阅读教程时我认为这意味着我必须在舞台上创建一个球的影片剪辑,然后将其导出为动作脚本类名是Ball,但是当我翻阅书时,我看到已经存在Ball.as,说明我可能需要在本书后面再次使用它,这个读:

package 
{ 
import flash.display.Sprite; 
public class Ball extends Sprite 
{ 
private var radius:Number; 
private var color:uint; 
public var vx:Number=0; 
public var vy:Number=0; 
public function Ball(radius:Number=40, color:uint=0xff0000) 
{ 
this.radius=radius; 
this.color=color; init(); 
} 
public function init():void 
{
 graphics.beginFill(color); 
graphics.drawCircle(0, 0, radius); 
graphics.endFill();
 }
 }
 }

这设法阻止了所有出现的错误,但它没有传递来自Bubbles.as的任何效果,它只是在舞台中央吹出一个红球。为了有利于Bubbles.as,我将如何更改此代码?

请帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

它对我来说运行正常,但是我需要对你提供的代码进行一些调整 - 我注意到你从Bubbles类中试图访问Ball的属性设置为私有(半径,颜色),所以要么将它们公开,要么为此创建适当的getter / setter方法。

public var radius:Number;
public var color:uint;

或者如果你想使用getter / setter函数

private var _radius:Number;
private var _color:uint;

public function get radius():Number
{
   return _radius;
}
public function set radius(val:Number):void
{
   _radius = val;
}

public function get color():uint
{
   return _color;
}
public function set color(val:uint):void
{
   _color= val;
}

在进行了更改后,我只将Bubbles设置为我的FLA文档根目录,然后将其命名为bam!