首先,我首先要说的是,我对ActionScript 3真的很陌生。 我有一个需要在每一帧上调用的函数。所以我将该功能放入框架中,然后我制作了一个影片剪辑,并为其提供了实例名称' StageFrame'
这是我的代码:
const NUM_BALL:int = 24;
var loadingBall:Vector.<Shape> = new Vector.<Shape (NUM_BALL);
var timeStep:int = 0;
const BALL_HEIGHT:int = 40;
function animateBalls(e:Event):void
{
for (var i:int = 0; i < NUM_BALL; i++ )
{
loadingBall[i].graphics.clear();
loadingBall[i].graphics.beginFill(0x0B5F95);
loadingBall[i].graphics.drawCircle(455+5*i,getY(i,timeStep),2);
}
timeStep++;
}
function getY(i:int, t:int):int
{
return 260 + BALL_HEIGHT/2 * (1 + Math.sin((timeStep * (i/500 + 0.02)) % 2*Math.PI));
}
StageFrame.addEventListener(Event.ENTER_FRAME, animateBalls);
当我运行它时,我收到此错误:
TypeError:错误#1009:无法访问空对象引用的属性或方法。 在Untitled_fla :: MainTimeline / animateBalls()
有人能帮助我吗?
答案 0 :(得分:0)
当然,您会收到Error #1009
错误,因为在将Shape
函数设置为animateBalls()
函数之前,您尚未创建var loadingBall:Vector.<Shape> = new Vector.<Shape> ();
// create your Shape objects and add it to your stageFrame MovieClip
for (var i:int = 0; i < NUM_BALL; i++)
{
var shape:Shape = new Shape();
// add your shape to your loadingBall Vector
loadingBall.push(shape);
// add your shape to your stageFrame
stageFrame.addChild(shape);
// it's better to start with a lowercase letter for an object name, uppercase letter is for Class names
}
function animateBalls(e:Event):void
{
var shape:Shape;
for (var i:int = 0; i < loadingBall.length; i++)
shape = loadingBall[i];
shape.graphics.clear();
shape.graphics.beginFill(0x0B5F95);
shape.graphics.drawCircle(455 + (5*i), getY(i, timeStep), 2);
}
timeStep++;
}
。
所以你可以这样做:
{{1}}
希望可以提供帮助。