由浅入深学习Flash制作物体弹跳游戏_Flash教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:用FLASH遮罩效果做图片切换效果
本教程是关于FLASH应用遮罩效果制作好看的图片切换效果。该教程选用FLASH遮罩中最简单的一种作为例子,当然你可以用自己的想象力来做出更多更好的图片动画。希

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

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

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

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

由浅入深学习Flash制作物体弹跳游戏

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

由浅入深学习Flash制作物体弹跳游戏

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

由浅入深学习Flash制作物体弹跳游戏

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

由浅入深学习Flash制作物体弹跳游戏

  别忘了地图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就会回到初始状态):

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

  改良上面代码:

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):

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

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

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;
}
}
}

  效果如下:

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

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;
}
}
}

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

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

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;
}
}
}

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

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

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;
}
}
}

  最终演示效果:

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

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

分享:Flash AS轻松制作旋转可控立方体特效
可以控制立方体的旋转:cubeCenter=newpoint3d(0,0,0);geom.addCube(cubeCenter,80,0xFFF2CC);geom.addCube(cubeCenter,20,0xFFF2CC);geom.addSolid

/所属分类:Flash教程/更新时间:2008-03-05
相关Flash教程