求flash小游戏制作教程代码和教程

flash as3实现打字小游戏代码
】 浏览:219次
&  游戏效果  新建Fla,在第一帧F9添加代码即可。&代码如下&var
arr:Array = new Array();
var time:uint=0;
var angle:Number=0;
[SWF(width=800, height=465,backgroundColor=0xffffff,
frameRate=24)]
this.addEventListener(Event.ENTER_FRAME,function(event){
&angle+=0.1;
if(time&=13){
&&&&&&&&&& var tf:TextField = addChild(new TextField()) as
&&&& zfun = function(){time = 0,tf.htmlText ='&font
size=&'+String(Math.round(Math.random()*35)+15)+'&
color=&#'+String(Math.random()*)+'&&'+
String.fromCharCode(int(Math.random()*26+65).toString())+'&/font&',tf.x =
Math.random()*-760+780,tf.y = -10,tf.name =
(Math.random()*-10+5).toString(),arr.unshift(tf)}.apply();&&&
&&&&& for(var i:Number =
arr.length-1;i&=0;i--){
&&&& zfun =function(){arr[i].y+=3,arr[i].x+=
Math.sin(angle)*int(arr[i].name)}.apply();
&&&&&&&&&&
if(arr[i].y&=stage.stageHeight){
&&&&&&&&&&&&&&&&&&&&&&& zfun
=function(){removeChild(arr[i]),arr.splice(i,1)}.apply();
&&&&&&&&&&
stage.addEventListener(KeyboardEvent.KEY_DOWN,function(event){&
&&& for(var i:Number =
arr.length-1;i&=0;i--){
&&&&&&&&&&&
if(arr[i].text==String.fromCharCode(event.keyCode)){
&&&&&&&&&&&&&&&&&&&&&&&&
zfun =function(){removeChild(arr[i]),arr.splice(i,1)}.apply();
&&&&&&&&&&& }
您看到此篇文章时的感受是:
【】【】【】
【】【】【】前面都是理论的讲解,这篇来一个实战,做一个类似坦克游戏的程序。
这几天也写了一些类了,是驴子还是骡子,拿出来遛一遛就知道了,先看这个上面这个flash动画!一个类似坦克游戏的demo程序使用Direction类来进行方向控制使用Dot类来计算距离用上Direction类和Dot类之后,这个demo程序变得异常简单额。。也没什么好说,主要透过这个例子,让大家类熟悉Direction类和Dot类的使用方法不懂的可以在后面跟帖提问,高手如果看到什么有错误的地方,请指正出来,多谢指教下面是fla的源代码:
import index.base.game.Dimport index.base.events.DirectionEimport index.base.geom.D//舞台属性设置stage.showDefaultContextMenu =stage.align = "TL";stage.scaleMode = "noScale";//创建坦克var tank:Tank = new Ttank.x = tank.y = 250;addChild(tank);//创建绑定坦克的点var dot:Dot = new Ddot.bind(tank);//坦克移动var dirTank:Direction = new Direction(stage);//炮台转动var dirTower:Direction = new Direction(stage,true,87,83,65,68);//坦克炮台事件dirTank.addEventListener(DirectionEvent.DO,doTankFun);dirTower.addEventListener(DirectionEvent.DO,doTowerFun);//坦克移动function doTankFun(e:DirectionEvent):void{& & & & if(e.up){& & & & & & & & dot.go(2,true);& & & & }& & & & if(e.down){& & & & & & & & dot.go(-2,true);& & & & }& & & & if(e.left){& & & & & & & & tank.rotation -= 2;& & & & }& & & & if(e.right){& & & & & & & & tank.rotation += 2;& & & & }& & & & if(tank.x & 0) tank.x = 0;& & & & if(tank.y & 0) tank.y = 0;& & & & if(tank.x & stage.stageWidth) tank.x = stage.stageW& & & & if(tank.y & stage.stageHeight) tank.y = stage.stageH}//是否可以发射炮台,子弹var isBullet:Boolean =var isShell:Boolean =//炮台发射转动function doTowerFun(e:DirectionEvent):void{& & & & if(e.up && isBullet){& & & & & & & & var bullet:Bullet = new B& & & & & & & & bullet.x = tank.x;& & & & & & & & bullet.y = tank.y;& & & & & & & & bullet.rotation = tank.rotation + tank.tower.& & & & & & & & bullet.addEventListener(Event.ENTER_FRAME,bulletFun);& & & & & & & & addChild(bullet);& & & & & & & & & & & & & & & & isBullet =& & & & & & & & setTimeout(function(){isBullet = true},200);& & & & }& & & & if(e.down && isShell){& & & & & & & & var shell:Shell = new S& & & & & & & & shell.x = tank.x;& & & & & & & & shell.y = tank.y;& & & & & & & & shell.rotation = tank.& & & & & & & & shell.addEventListener(Event.ENTER_FRAME,shellFun);& & & & & & & & addChild(shell);& & & & & & & & & & & & & & & & isShell =& & & & & & & & setTimeout(function(){isShell = true},500);& & & & }& & & & if(e.left){& & & & & & & & tank.tower.rotation -= 5;& & & & }& & & & if(e.right){& & & & & & & & tank.tower.rotation += 5;& & & & }}//炮台function shellFun(e:Event):void{& & & & var tmp:Shell = e.currentTarget as S& & & & var d:Dot = new Dot(tmp.x,tmp.y,tmp.rotation);& & & & d.bind(tmp);& & & & d.go(4,true);& & & & if(tmp.x & 0 || tmp.x & stage.stageWidth || tmp.y & 0 || tmp.y & stage.stageHeight){& & & & & & & & removeChild(tmp);& & & & & & & & tmp.removeEventListener(Event.ENTER_FRAME,shellFun);& & & & }& & & & & & & & tmp =& & & & d =}//子弹function bulletFun(e:Event):void{& & & & var tmp:Bullet = e.currentTarget as B& & & & var d:Dot = new Dot(tmp.x,tmp.y,tmp.rotation);& & & & d.bind(tmp);& & & & d.go(5,true);& & & & if(tmp.x & 0 || tmp.x & stage.stageWidth || tmp.y & 0 || tmp.y & stage.stageHeight){& & & & & & & & removeChild(tmp);& & & & & & & & tmp.removeEventListener(Event.ENTER_FRAME,bulletFun);& & & & }& & & & & & & & tmp =& & & & d =}另外注意源代码,有个地方多次对tank的tower属性就行引用,并且返回他的x,y或者旋转值,有人就会问了,as3不是不支持类似mc那样的直接访问显示对象,为什么我这儿却可以?愿意是我把素材绑定在Tank类上,并且对Tank类做了以下编写:
package{& & & & & & & & import flash.display.S& & & & & & & & public class Tank extends Sprite{& & & & & & & & & & & & & & & & public function Tank(){& & & & & & & & & & & & & & & & & & & & }& & & & & & & & & & & & & & & & public function get tower():Sprite{& & & & & & & & & & & & return towerMc;& & & & & & & & }& & & & }}光看这个类,也许你还是不明白,是什么原因,为什么会多出来一个towerMc出来,详细的原因,请自己下载提供的源文件,下载下来看看吧。。不懂跟帖问!
··········| 您所在的位置: >
> 用Flash制作一个类似弹弓的小游戏教程用Flash制作一个类似弹弓的小游戏教程更新:&&&&编辑:寒蕾&&&&来源:&&&&人气:加载中...&&&&字号:|标签:&&&&&&&&&&&&
  利用于的戏的教程,主要由Actionscript实现,代码大家由浅入深来理解。  思路是准备三个小球元件,然后使其中可以拖动,使之沿着一条线,计算一下的角度,最后实现的一个戏。  启动首先我们绘制两个元件,非常简单的。绘制一个小球然后转变为影片剪辑元件,同样法子再建议一个不同颜色的小球的影片剪辑。  然后在主场景中直接输入如下代码:attachMovie(&sling&, &sling_1&, _root.getNextHighestDepth(), {_x:20, _y:200});
attachMovie(&sling&, &sling_2&, _root.getNextHighestDepth(), {_x:480, _y:200});
attachMovie(&ball&, &ball&, _root.getNextHighestDepth(), {_x:250, _y:100});
_root.createEmptyMovieClip(&elastic&, _root.getNextHighestDepth());
ball.onPress = function() {
startDrag(this);
ball.onRelease = function() {
stopDrag();
};  效果如下:
  然后我们通过修改上面的代码,,在两个小球之间绘制一条线,可以随中间小球任意移动。attachMovie(&sling&, &sling_1&, _root.getNextHighestDepth(), {_x:20, _y:200});
attachMovie(&sling&, &sling_2&, _root.getNextHighestDepth(), {_x:480, _y:200});
attachMovie(&ball&, &ball&, _root.getNextHighestDepth(), {_x:250, _y:100});
_root.createEmptyMovieClip(&elastic&, _root.getNextHighestDepth());
ball.onPress = function() {
  startDrag(this);
ball.onRelease = function() {
  stopDrag();
elastic.onEnterFrame = function() {
  this.clear();
  this.lineStyle(2, 0x009900);
  this.moveTo(sling_1._x, sling_1._y);
  if (ball._y&182) {
    dist_x = ball._x-sling_1._x;
    dist_y = ball._y-sling_1._y;
    distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
    elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18);
    angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling);
    this.lineTo(sling_1._x+elastic_length*Math.cos(angle), sling_1._y+elastic_length*Math.sin(angle));
  } else {
    this.lineTo(sling_2._x, sling_2._y);
  this.moveTo(sling_2._x, sling_2._y);
  if (ball._y&182) {
    dist_x = ball._x-sling_2._x;
    dist_y = ball._y-sling_2._y;
    distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
    elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18);
    angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling)*-1;
    this.lineTo(sling_2._x+elastic_length*Math.cos(angle), sling_2._y+elastic_length*Math.sin(angle));
  } else {
    this.lineTo(sling_2._x, sling_2._y);
};  效果。
  在这里我们可以很轻松的计算出小球在线上的角度来。  稍微做一下改动。attachMovie(&sling&, &sling_1&, _root.getNextHighestDepth(), {_x:20, _y:200});
attachMovie(&sling&, &sling_2&, _root.getNextHighestDepth(), {_x:480, _y:200});
attachMovie(&ball&, &ball&, _root.getNextHighestDepth(), {_x:250, _y:100});
_root.createEmptyMovieClip(&elastic&, _root.getNextHighestDepth());
xspeed = 0;
yspeed = 0;
ball.onPress = function() {
startDrag(this);
ball.onRelease = function() {
stopDrag();
ball.onEnterFrame = function() {
if (fire) {
 this._x += (xspeed*0.001);
 this._y += (yspeed*0.001);
elastic.onEnterFrame = function() {
this.clear();
this.lineStyle(2, 0x009900);
this.moveTo(sling_1._x, sling_1._y);
if (ball._y&182) {
 dist_x = ball._x-sling_1._x;
 dist_y = ball._y-sling_1._y;
 distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
 elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18);
 angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling);
 saved_angle =
 this.lineTo(sling_1._x+elastic_length*Math.cos(angle), sling_1._y+elastic_length*Math.sin(angle));
 this.lineTo(ball._x,ball._y);
 this.lineTo(sling_2._x, sling_2._y);
this.moveTo(sling_2._x, sling_2._y);
if (ball._y&182) {
 dist_x = ball._x-sling_2._x;
 dist_y = ball._y-sling_2._y;
 distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
 elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18);
 angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling)*-1;
 this.lineTo(sling_2._x+elastic_length*Math.cos(angle), sling_2._y+elastic_length*Math.sin(angle));
 this.lineTo(ball._x,ball._y);
 this.lineTo(sling_2._x, sling_2._y);
本文示例代码或素材
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
分类选择您可能在找这些

参考资料

 

随机推荐