Flash AS3教程:小游戏开发实战尝试

2019-10-08 17:16:28王振洲

}
if(e.down && isShell){
var shell:Shell = new Shell;
shell.x = tank.x;
shell.y = tank.y;
shell.rotation = tank.rotation;
shell.addEventListener(Event.ENTER_FRAME,shellFun);
addChild(shell);

isShell = false;
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 Shell;
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 = null;
d = null;
}

//子弹
function bulletFun(e:Event):void{
var tmp:Bullet = e.currentTarget as Bullet;
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 = null;
d = null;
}另外注意源代码,有个地方多次对tank的tower属性就行引用,并且返回他的x,y或者旋转值,有人就会问了,as3不是不支持类似mc那样的直接访问显示对象,为什么我这儿却可以?
愿意是我把素材绑定在Tank类上,并且对Tank类做了以下编写:

CODE:
package{

import flash.display.Sprite;

public class Tank extends Sprite{

public function Tank(){

}

public function get tower():Sprite{
return towerMc;
}
}
}
光看这个类,也许你还是不明白,是什么原因,为什么会多出来一个towerMc出来,详细的原因,请自己下载提供的源文件,下载下来看看吧。。不懂跟帖问!
点击下载源文件