基本上我正在写一本名为“基础动作脚本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;
}
}
}
// 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 :(得分: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;
}
}
}