非常不错的一个教程,在Flash中制作一个真实的小球。教程都是利用Action Script实现。在文章最后提供了所有演示效果的源文件。
首先制作一个小球的电影元件,只要画一个圆然后转变为电影剪辑元件就可以。电影剪辑名字叫ball。
然后在第一帧加入下面代码:
power = 0.3;
yspeed = 0;
xspeed = 0;
friction = 0.95;
_root.attachMovie("ball", "ball", 1, {_x:250, _y:175});
ball.onEnterFrame = function() {
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power;
}
if (Key.isDown(Key.UP)) {
yspeed -= power
}
if (Key.isDown(Key.DOWN)) {
yspeed += power
}
xspeed *= friction;
this._y += yspeed;
this._x += xspeed;
};
效果如下:(按键盘方向键可以看到效果)
给小球来个渐变填充,象一个球了!呵呵...
再来个阴影:
下面我们再引入一幅材质图案,库面板如下:
效果如下:
然后利用ActionScript加上遮照。主要是利用mc.setMask()函数设置遮照了!
power = 0.3;
yspeed = 0;
xspeed = 0;
friction = 0.95;
_root.attachMovie("ball", "ball", 1, {_x:250, _y:175});
ball.texture.setMask(ball.ball_itself);
ball.onEnterFrame = function() {
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power;
}
if (Key.isDown(Key.UP)) {
yspeed -= power
}
if (Key.isDown(Key.DOWN)) {
yspeed += power
}
xspeed *= friction;
this._y += yspeed;
this._x += xspeed;
};
效果如下:
然后我们让该球真的滚动起来,滚动的更加真实:
power = 0.3;
yspeed = 0;
xspeed = 0;
friction = 0.95;
_root.attachMovie("ball", "ball", 1, {_x:250, _y:175});
ball.texture.setMask(ball.ball_itself);
ball.onEnterFrame = function() {
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power;
}
if (Key.isDown(Key.UP)) {
yspeed -= power;
}
if (Key.isDown(Key.DOWN)) {
yspeed += power;
}
xspeed *= friction;
yspeed *= friction;
this._y += yspeed;
this._x += xspeed;
this.texture._y += yspeed;
this.texture._x += xspeed;
if (this.texture._x>158) {
this.texture._x -= 188;
}
if (this.texture._x<-158) {
this.texture._x += 188;
}
if (this.texture._y>158) {
this.texture._y -= 188;
}
if (this.texture._y<-158) {
this.texture._y += 188;
}
};
效果如下:
下面是另外一种材质制作的动画:
最后把上面的7个演示动画的源文件提供给大家: 小球动画.rar