您在这里 首页 > 交流社区
社区首页
 


标题: [Flash] Flash利用材质和遮照创建真实的小球动画
店小一





UID 10001
精华 0
积分 0
帖子 2233
积分 11165 分
水晶石 0 颗
学徒 0 级
阅读权限 10
注册 2008-7-15
发表于 2008-8-6 16:12  资料  个人空间  短消息  加为好友 
Flash利用材质和遮照创建真实的小球动画

非常不错的一个教程,在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;
    }
};
  效果如下:
  下面是另外一种材质制作的动画。

顶部