5.按快捷键Ctrl+L打开“库”面板。右键点击“库”中的“飞鸟”元件,单击“链接”命令打开“链接属性”面板,如图4所示,并如图5所示设置。使用“链接属性”面板定义影片剪辑元件后,可以直接利用代码调用元件,而不必把元件拖拽到场景中。
图4
图5
6.回到“场景1”,点选“图层 1”的第1帧,按F9键打开“动作”面板,输入如下代码:
// 飞鸟动画 damp = .95; numbirds = 30; for (i=0; i<numbirds; i++) { bird = attachMovie("bird", "bird"+i, i); bird._x = Math.random()*350+20; bird._y = Math.random()*300+20; bird.vx = Math.random()*10-5; bird.vy = Math.random()*10-5; bird.k = Math.random()*.0001+.0003; bird.gotoAndPlay(Math.round(Math.random()*20)); } onEnterFrame = function () { var totx = 0; var toty = 0; for (i=0; i<numbirds; i++) { bird = _root["bird"+i]; totx += bird._x; toty += bird._y; } avgx = totx/numbirds; avgy = toty/numbirds; for (i=0; i<numbirds; i++) { bird = _root["bird"+i]; bird.vx += (avgx-bird._x)*bird.k; bird.vy += (avgy-bird._y)*bird.k; bird.vx += Math.random()-.5; bird.vy += Math.random()-.5; bird.vx *= damp; bird.vy *= damp; targAngle = Math.atan2(bird.vy, bird.vx)*180/Math.PI; diff = targAngle-bird._rotation; if (diff<-180) { diff += 360; } if (diff>180) { diff -= 360; } bird._rotation += diff*.2; bird._x += bird.vx; bird._y += bird.vy; } }
7.为了使该动画更有趣些,我们在以上的代码下,再添加如下代码:
// 鼠标动作 function drawToPoint(){ for(i=0;i<numbirds;i++){ bird=_root["bird"+i]; bird.vx+=(_xmouse-bird._x)*bird.k*100; bird.vy+=(_ymouse-bird._y)*bird.k*100; } } onMouseDown = drawToPoint;
添加这段代码后,按快捷键Ctrl+Enter测试。 当鼠标点击动画,所有的“鸟儿”就会向鼠标点击的方向飞去,很酷。整个效果到这里就全部设计好了。
|