二维动画课程设计教学内容.docx
《二维动画课程设计教学内容.docx》由会员分享,可在线阅读,更多相关《二维动画课程设计教学内容.docx(28页珍藏版)》请在冰豆网上搜索。
二维动画课程设计教学内容
二维动画课程设计
《二维动画课程设计》
课程设计报告
课程名称:
二维动画课程设计
实验项目名称:
基于AS3.0的游戏设计
院系:
物理与电子信息学院
学号:
090804006
姓名:
廖伟民
班级:
09数字媒体技术
日期:
2011年1月
实验地点:
课程设计成绩:
实验指导教师签字:
基于AS3.0的游戏设计
一、序言
1课程设计目标
二维动画课程设计旨在教授学生基本的设计原理和思路,并能发挥学生的个人创意能力,运用Flash软件和一些常用辅助软件,设计制作flash动画作品,并特别强调学生的创新能力和动手能力。
本课程设计要求学生在接受课堂理论知识学习之后,能熟练利用Flash软件制作相关的动画.
2、设计主题
1.基于AS3.0制作一个打飞机小游戏。
2.主要运用FlashCS4软件和PhotoShopCS3图片处理软件。
3、游戏简介
这是利用flashcs4基于AS3.0代码设计的一款简单的打飞机游戏,这款游戏借鉴了经典的打鸭子游戏的一些做法,再结合自己的一些想法设计出来的。
用AdobeFlashPlayer打开游戏后,按开始键,我们会看到:
敌机在空中来回飞行,按左右上下箭头键控制飞机炮在空中左右上下移动,按下空格键发射子弹,击中敌机后,敌机爆炸。
当被敌机碰到后,飞机爆炸,游戏结束,或是当击落的敌机达到100架后,游戏结束。
界面元素如下
游戏界面中的三个文本框分别为:
得分:
记录击中的敌机数;
剩余子弹:
记录子弹的数量;
级别:
记录游戏的难度等级,总9级,级别越高难度越大。
二、游戏结构
1、游戏运行过程图:
2、全部文件及作用图:
游戏文件
AirRaid.fla 主文件,程序界面及元件,绑定了文档类AirRaid.as。
AirRaid.as 文档类,程序的入口,控制整个程序的逻辑顺序,逻辑结构关系。
AAGun.as 绑定库中的火炮的类 ,控制火炮
AirPlane.as 绑定库中的敌机的类 ,控制飞机
Bullet.as 绑定库中的子弹的类 ,控制子弹
3、游戏中各图层名称及作用
游戏中的主文件AirRaid.fla文件,分为7个图层4帧,帧上有代码,绑定了文档类
AirRaid.as。
1、背景2层:
一下雨时候的海面的上空。
2、背景层:
在第二帧游戏开始时显示下雨效果。
3、成绩显示层:
得分:
动态文本,实例名showScore;
剩余子弹:
动态文本,实例名showShots;
级别:
动态文本,实例名称showjb。
4、按钮及提示层:
第1帧:
开始按钮及说明,按钮实例名:
startButton。
第3帧:
重新开始按钮及结束提示,按钮实例名playAgainButton。
第4帧:
重新开始按钮及结束提示,按钮实例名playAgain。
5、as(代码)层:
4帧每帧都有代码,具体代码将在下面解释。
6、背景音乐层:
在游戏开始时播放背景音乐。
7、标签层:
第1帧:
标签名称intro;第2帧:
标签名称play;
第3帧:
标签名称gameover;第4帧:
标签名称gamewin。
三、设计步骤
1、制作游戏必须的素材和准备工作
首先按前面游戏结构中说明的那样,建好五个文件分别为:
AirRaid.fla、AirRaid.as、
AAGun.as、AirPlane.as、Bullet.as。
AirRaid.fla文件与文档类AirRaid.as绑定,在AirRaid.fla文件
中新建9个影片剪辑分别为:
AAgun:
在里面制作飞机炮并将类名命名为AAgun,与外面的AAgun.as绑定。
Airplane:
1-5帧每帧一个飞机图形。
第6-9帧爆炸图形并添加爆炸声音,有标签:
explode
第10帧有代码parent.removeChild(this);stop();(解释:
parent是父容器
this是自身removeChild是移除。
这句代码的意思是通知父容器把它从显示界面移除。
就是说当子弹击中敌机,敌机播放爆炸效果。
播放到第10帧时,就通知它的父容器把它从屏幕上移除。
),类名为Airplane。
bullet:
绘制子弹,类名为bullet。
yudi:
制作下雨动画;先制作好一个雨滴,将类名命名为yd_mc,在代码层写
varbl;
varsj:
Timer=newTimer(Math.random()*500+500,10);
sj.addEventListener(TimerEvent.TIMER,sjcd);
sj.start();
functionsjcd(event:
TimerEvent){
for(varb=0;b<5;b++){
varyd:
yd_mc=newyd_mc();
addChildAt(yd,bl);
yd.x=Math.random()*550;
yd.y=Math.random()*100;
bl++;
}
}
begin:
制作游戏开始时的说明,在按钮及提示层第一帧拖到主场景,并命名为p3_mc。
fail:
制作游戏失败时的说明,在按钮及提示层第三帧拖到主场景,并命名为p1_mc。
win:
制作游戏胜利时的说明。
在按钮及提示层第四帧拖到主场景,并命名为p2_mc。
dfj:
制作打飞机游戏的图片。
在按钮及提示层第一帧拖到主场景,并命名为p4_mc。
sy:
导入背景音乐,在背景音乐层第二帧拖到主场景。
然后再制作一个按钮命名为BasicButton,到这里只要再把需要的背景图片和声音导入,制作游戏所需要的素材全部制作好了。
2、游戏中各图层中的一些代码的书写和解释
在前面游戏结构中已经说明了主场景中各图层的作用和关系,现在就游戏中各图层中的一些代码的书写进行说明并解释。
(1)、as(代码)层的每帧代码及解释:
第1帧代码:
importflash.events.MouseEvent;
stop();
startButton.addEventListener(MouseEvent.CLICK,clickStart);
functionclickStart(event:
MouseEvent){
gotoAndStop("play");
}
解释:
给startButton注册了鼠标点击事件,用clickStart方法来实现,点击startButton
按钮就把影片转到第2帧上游戏的进行界面。
第2帧代码:
stop();
startAirRaid();
解释:
调用文档类的startAirRaid方法,执行这个方法,就会生成敌机,火炮,接受用户
的键盘操作事件来生成子弹等一系列操作。
第3帧代码:
playAgainButton.addEventListener(MouseEvent.CLICK,clickPlayAgain);
functionclickPlayAgain(event:
MouseEvent){
gotoAndStop("play");
}
解释:
给playAgainButton注册了鼠标点击事件,用clickPlayAgain方法实现,点击
playAgainButton按钮跳转到第2帧。
第4帧代码:
playAgain.addEventListener(MouseEvent.CLICK,clickPlay);
functionclickPlay(event:
MouseEvent){
gotoAndStop("play");
}
解释:
给playAgain注册了鼠标点击事件,用clickPlayAgain方法实现,点击跳转playAgain
按钮到第2帧。
(2)、按钮及提示层的各帧代码及解释:
第1帧代码
importfl.transitions.*;
importfl.transitions.easing.*;
TransitionManager.start(p3_mc,{type:
Fade,direction:
Transition.IN,duration:
3,easing:
None.easeNone});
TransitionManager.start(p4_mc,{type:
PixelDissolve,direction:
Transition.IN,duration:
6,easing:
None.easeNone,xSections:
100,ySections:
50});
解释:
用淡入的效果显示p3_mc影片剪辑看,用像素溶解的效果显示p4_mc影片剪辑。
第3帧代码
importfl.transitions.*;
importfl.transitions.easing.*;
TransitionManager.start(p1_mc,{type:
PixelDissolve,direction:
Transition.IN,duration:
2,easing:
None.easeNone,xSections:
100,ySections:
50});
解释:
用像素溶解的效果显示p1_mc影片剪辑。
第4帧代码
importfl.transitions.*;
importfl.transitions.easing.*;
TransitionManager.start(p2_mc,{type:
Zoom,direction:
Transition.IN,duration:
2,easing:
Elastic.easeOut});
解释:
用弹性缩放的效果显示p2_mc影片剪辑。
(3)、游戏制作中各脚本类的编写和作用
编写AirRaid.as类
AirRaid类是游戏的控制器,绑定AirRaid.fla。
负责生成界面上的飞机、飞机炮、子弹。
显
示得分、子弹的剩余数量和级别。
注册了键盘按下,释放侦听器,接收用户的键盘操作。
注
册了进入帧事件侦听器,进行碰撞检测。
定义一个飞机计时器,随机生成飞机。
检测剩余子
弹的数目,当子弹数为零或飞机炮敌机相撞时游戏结束,移除界面上的飞机,火炮,跳转到
游戏失败界面,当击落的敌机数为100时游戏结束,移除界面上的飞机,火炮,跳转到游
戏成功界面。
(代码如下)
package{
importflash.display.MovieClip;
importflash.display.SimpleButton;
importflash.events.KeyboardEvent;
importflash.utils.Timer;
importflash.events.TimerEvent;
importflash.text.TextField;
importflash.events.Event;
publicclassAirRaidextendsMovieClip{
privatevarspeed:
Number;
privatevaraagun:
AAGun;//飞机炮
privatevarairplanes:
Array;//飞机数组
privatevarbullets:
Array;//子弹数组
publicvarleftArrow,rightArrow,upArrow,downArrow:
Boolean;
privatevarnextPlane:
Timer;//不定时生成飞机的计时器
privatevarshotsLeft:
int;//得分文本
privatevarshotsHit:
int;//子弹数文本
privatevarjibie:
int;//得分文本
publicfunctionstartAirRaid(){
//初始化得分数、子弹数和级别
jibie=1;
shotsLeft=300;
shotsHit=0;
showGameScore();
//生成飞机炮加入到舞台上
aagun=newAAGun();
addChild(aagun);
//生成飞机、子弹数组
airplanes=newArray();
bullets=newArray();
//键盘按下、释放事件侦听器
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
//进入帧事件侦听器,检测子弹击中飞机和敌机飞机炮的碰撞检测。
addEventListener(Event.ENTER_FRAME,checkForHits);
addEventListener(Event.ENTER_FRAME,checkForHit);
//生成下一架飞机
setNextPlane();
}
//根据级别的不同不定时生成飞机
publicfunctionsetNextPlane(){
if((shotsHit<=10)){
//1000毫秒至2000毫秒之间生成一架飞机
nextPlane=newTimer(1000+Math.random()*1000,1);
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>10&&shotsHit<=20){
jibie=2;
//800毫秒至1600毫秒之间生成一架飞机
nextPlane=newTimer(800+Math.random()*800,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>20&&shotsHit<=30){
jibie=3;
//600毫秒至1200毫秒之间生成一架飞机
nextPlane=newTimer(600+Math.random()*600,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>30&&shotsHit<=40){
jibie=4;
//400毫秒至800毫秒之间生成一架飞机
nextPlane=newTimer(400+Math.random()*400,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>40&&shotsHit<=50){
jibie=5;
//200毫秒至400毫秒之间生成一架飞机
nextPlane=newTimer(200+Math.random()*200,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>50&&shotsHit<=60){
jibie=5;
//100毫秒至200毫秒之间生成一架飞机
nextPlane=newTimer(100+Math.random()*100,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>60&&shotsHit<=70){
jibie=6;
//90毫秒至180毫秒之间生成一架飞机
nextPlane=newTimer(90+Math.random()*90,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>70&&shotsHit<=80){
jibie=7;
//80毫秒至160毫秒之间生成一架飞机
nextPlane=newTimer(80+Math.random()*80,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>80&&shotsHit<=90){
jibie=8;
//70毫秒至140毫秒之间生成一架飞机
nextPlane=newTimer(70+Math.random()*70,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}elseif(shotsHit>90&&shotsHit<=100){
jibie=9;
//60毫秒至120毫秒之间生成一架飞机
nextPlane=newTimer(70+Math.random()*70,1);
showGameScore();
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}
}
publicfunctionnewPlane(event:
TimerEvent){
//随机的边、速度和高度
if(Math.random()>.5){
varside:
String="left";
}else{
side="right";
}
varaltitude:
Number=Math.random()*50+20;
//根据级别的不同,敌机的飞行速度的设定
if(jibie<=2){
speed=Math.random()*150+150;
}elseif(jibie>2&&jibie<=4){
speed=Math.random()*175+175;
}elseif(jibie>4&&jibie<=6){
speed=Math.random()*200+200;
}elseif(jibie>6){
speed=Math.random()*225+225;
}
//生成飞机
varp:
Airplane=newAirplane(side,speed,altitude);
addChild(p);
airplanes.push(p);
setNextPlane();
}
//敌机和飞机炮的碰撞检测
publicfunctioncheckForHit(event:
Event){
for(varairplaneNum:
int=airplanes.length-1;airplaneNum>=0;airplaneNum--){
if(aagun.hitTestObject(airplanes[airplaneNum])){
airplanes[airplaneNum].planeHit();
endGame();
}
}
}
//子弹和敌机的碰撞检测
publicfunctioncheckForHits(event:
Event){
for(varbulletNum:
int=bullets.length-1;bulletNum>=0;bulletNum--){
for(varairplaneNum:
int=airplanes.length-1;airplaneNum>=0;airplaneNum--){
if(bullets[bulletNum].hitTestObject(airplanes[airplaneNum])){
airplanes[airplaneNum].planeHit();
bullets[bulletNum].deleteBullet();
shotsHit++;
showGameScore();
break;
}
}
}
//当击落的敌机数位100时,游戏成功,结束
if((shotsHit==100)){
winGame();
}
//当子弹为零时,游戏失败,结束
if((shotsHit!
=100)&&(shotsLeft==0)){
endGame();
}
}
//按下键盘
publicfunctionkeyDownFunction(event:
KeyboardEvent){
if(event.keyCode==37){
leftArrow=true;
}elseif(event.keyCode==39){
rightArrow=true;
}elseif(event.keyCode==38){
upArrow=true;