摘要载入中…    请稍等…















内容载入中…    请稍等…

学习Flash物体弹跳游戏

2009-1-6 13:10:42   来源:  

   

 ·Flash壮阔的碧海蓝天精美动画 ·CAD,3Dmax实例视频教程软件下载

  也不知道该如何起这篇文章的名字,如果您觉得我的文章的名字起的不好,在转载过程中你可以自己改个好的名字别忘了告诉我下啊!希望本文对开发Flash游戏入门的人有用。

  这个游戏就是,首先设置一个地图,然后出来一个人(或别的东西都可以),从空中落下来,然后我们利用键盘的方向键可以控制左右移动,当想向上移动的时候,使用空格键可以跳高。简单研究,希望大家喜欢。转载必须注明出处和作者,如果加上本站连接,那就感谢你了。

  开始给大家讲解如何制作。(每一步都有演示动画,在文章末尾还给出了所有演示动画的源文件!

  建立Flash文档,Ctrl+J修改影片属性。

  然后建立一个影片剪辑元件,作为地图来使用,名字为terrain,形状如下图。

  然后再建立两个按钮,一个是go,另一个是reset。最后建立一个char影片剪辑元件(看你的绘画工夫了,建立什么都可以)。

  然后分别把三类元件(terrain,char,2个按钮)都放到场景中来。然后给char影片剪辑元件起实例名walker,地图terrain的实例名为terrain。然后选择第一帧,按F9打开动作面板,输入代码。

  别忘了地图terrain的实例名为terrain。同样方法,不给截图了。

第一帧加入下面代码:

go=false;//主要是一个变量赋初始值

go按钮:

on (release) {
 go = true;
}//开始运动

reset按钮:

on (release) {
 go = false;
 walker._x = 107;
 walker._y = 49;
}//回到初始状态

  然后char影片剪辑的action如下:

onClipEvent (load) {
 gravity = 0.2;
 yspeed = 0;
}
onClipEvent (enterFrame) {
 if (_root.go) {
  yspeed += gravity;
  while (_root.terrain.hitTest(_x, _y+_height/2, true)) {
   _y--;
   yspeed = 0;
  }
  _y += yspeed;
 }
}

  效果如下(只要点GO按钮,物体就会掉下,点reset就会回到初始状态):

/edit/UploadFile/749/200865234929852.swf

  大家通过看上面的代码可以看到是利用速度总是增加的,所以物体接触地面时显得总是动。

  改良上面代码:

onClipEvent (load) {
    gravity = 0.2;
    yspeed = 0;
}
onClipEvent (enterFrame) {
    if (_root.go) {
        yspeed += gravity;
        while (_root.terrain.hitTest(_x, _y+_height/2, true)) {
            _y--;
            yspeed = 0;
        }
        if (!_root.terrain.hitTest(_x, _y+_height/2+1, true)) {
            _y += yspeed;
        } else {
            yspeed = 0;
        }
    }
}

  效果(通过yspeed = 0;使接触到地面的时候速度变为0):

/edit/UploadFile/749/200865235253580.swf

  通过上面两个步骤我们就制作好了这个游戏的基础部分。下面我们开始制作游戏使用键盘来控制移动。

  用键盘来左右方向键来控制物体左右移动。

onClipEvent (load) {
    gravity = 0.2;
    yspeed = 0;
    xspeed = 1;
}
onClipEvent (enterFrame) {
    if (_root.go) {
        if (Key.isDown(Key.LEFT)) {
            _x -= xspeed;
        }
        if (Key.isDown(Key.RIGHT)) {
            _x += xspeed;
        }
        yspeed += gravity;
        while (_root.terrain.hitTest(_x, _y+_height/2, true)) {
            _y--;
            yspeed = 0;
        }
        if (!_root.terrain.hitTest(_x, _y+_height/2+1, true)) {
            _y += yspeed;
        } else {
            yspeed = 0;
        }
    }
}

  效果如下:

/edit/UploadFile/749/200865235346759.swf

  上面效果,大家通过用键盘方向键移动,如果物体到了地图的边缘,可以移动到外面去。下面我们继续改良代码,把物体的移动范围做一个限制。

onClipEvent (load) {
 gravity = 0.2;
 yspeed = 0;
 xspeed = 1;
}
onClipEvent (enterFrame) {
 if (_root.go) {
  if (Key.isDown(Key.LEFT)) {
   if (!_root.terrain.hitTest(_x-_width/2, _y+_height/4, true)) {
    _x -= xspeed;
   }
  }
  if (Key.isDown(Key.RIGHT)) {
   if (!_root.terrain.hitTest(_x+_width/2, _y+_height/4, true)) {
    _x += xspeed;
   }
  }
  yspeed += gravity;
  while (_root.terrain.hitTest(_x, _y+_height/2, true)) {
   _y--;
   yspeed = 0;
  }
  if (!_root.terrain.hitTest(_x, _y+_height/2+1, true)) {
   _y += yspeed;
  } else {
   yspeed = 0;
  }
 }
}

  上面代码控制了左右两边的界限。效果如下(这时就不会移动到外面去了吧!):

/edit/UploadFile/749/200865235419200.swf

  下面我们开始改良代码使该物体能被键盘的空格键控制跳动。

onClipEvent (load) {
 gravity = 0.2;
 yspeed = 0;
 xspeed = 1;
}
onClipEvent (enterFrame) {
 if (_root.go) {
  if (Key.isDown(Key.LEFT)) {
   if (!_root.terrain.hitTest(_x-_width/2, _y+_height/4, true)) {
    _x -= xspeed;
   }
  }
  if (Key.isDown(Key.RIGHT)) {
   if (!_root.terrain.hitTest(_x+_width/2, _y+_height/4, true)) {
    _x += xspeed;
   }
  }
  if (Key.isDown(Key.SPACE)) {
   yspeed = -5;
  }
  yspeed += gravity;
  while (_root.terrain.hitTest(_x, _y+_height/2, true)) {
   _y--;
   yspeed = 0;
  }
  if ((!_root.terrain.hitTest(_x, _y+_height/2+1, true)) or (yspeed<0)) {
   _y += yspeed;
  } else {
   yspeed = 0;
  }
 }
}

  效果如下(物体移动后按空格键试试!!):

/edit/UploadFile/749/200865235514972.swf

  大家通过测试上面影片,按空格键会不会跳呢??而且跳的还很高啊!!如果你总按空格键还可以跳出屏幕呢!哈哈!!!所以下面大家也该知道干什么了!!给跳的范围做一个限制,你非常聪明的。

onClipEvent (load) {
 gravity = 0.2;
 yspeed = 0;
 xspeed = 1;
 jumping = 0;
}
onClipEvent (enterFrame) {
 if (_root.go) {
  if (Key.isDown(Key.LEFT)) {
   if (!_root.terrain.hitTest(_x-_width/2, _y+_height/4, true)) {
    _x -= xspeed;
   }
  }
  if (Key.isDown(Key.RIGHT)) {
   if (!_root.terrain.hitTest(_x+_width/2, _y+_height/4, true)) {
    _x += xspeed;
   }
  }
  if ((Key.isDown(Key.SPACE)) and (!jumping)) {
   yspeed = -5;
   jumping = 1;
  }
  yspeed += gravity;
  while (_root.terrain.hitTest(_x, _y+_height/2, true)) {
   _y--;
   yspeed = 0;
   jumping = 0;
  }
  if ((!_root.terrain.hitTest(_x, _y+_height/2+1, true)) or (yspeed<0)) {
   _y += yspeed;
  } else {
   yspeed = 0;
   jumping = 0;
  }
 }
}

  最终演示效果:

/edit/UploadFile/749/200865235614836.swf

  最终一个小游戏就做好了!!不难吧!!希望大家支持我!支持本站。谢谢!差点忘了!!以上所有演示动画的源文件提供给大家!!!

  点击这里下载源文件(还记得解压密码吗??www.webjx.com)

 [1]