CODE:import index.base.game.Direction;
另外注意源代碼,有個地方多次對tank的tower屬性就行引用,并且返回他的x,y或者旋轉(zhuǎn)值,有人就會問了,as3不是不支持類似mc那樣的直接訪問顯示對象,為什么我這兒卻可以?
import index.base.events.DirectionEvent;
import index.base.geom.Dot;
//舞臺屬性設(shè)置
stage.showDefaultContextMenu = false;
stage.align = "TL";
stage.scaleMode = "noScale";
//創(chuàng)建坦克
var tank:Tank = new Tank;
tank.x = tank.y = 250;
addChild(tank);
//創(chuàng)建綁定坦克的點
var dot:Dot = new Dot;
dot.bind(tank);
//坦克移動
var dirTank:Direction = new Direction(stage);
//炮臺轉(zhuǎn)動
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.stageWidth;
if(tank.y > stage.stageHeight) tank.y = stage.stageHeight;
}
//是否可以發(fā)射炮臺,子彈
var isBullet:Boolean = true;
var isShell:Boolean = true;
//炮臺發(fā)射轉(zhuǎn)動
function doTowerFun(e:DirectionEvent):void{
if(e.up && isBullet){
var bullet:Bullet = new Bullet;
bullet.x = tank.x;
bullet.y = tank.y;
bullet.rotation = tank.rotation tank.tower.rotation;
bullet.addEventListener(Event.ENTER_FRAME,bulletFun);
addChild(bullet);
isBullet = false;
setTimeout(function(){isBullet = true},200);
}
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類上,并且對Tank類做了以下編寫:
CODE:package{
光看這個類,也許你還是不明白,是什么原因,為什么會多出來一個towerMc出來,詳細的原因,請自己下載提供的源文件,下載下來看看吧。。不懂跟帖問!
import flash.display.Sprite;
public class Tank extends Sprite{
public function Tank(){
}
public function get tower():Sprite{
return towerMc;
}
}
}
點擊下載源文件
|
新聞熱點
疑難解答