国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 辦公 > Flash > 正文

Flash AS3教程:小游戲開發實戰嘗試

2020-07-17 13:17:37
字體:
來源:轉載
供稿:網友
前面講解了Flash AS3教程:Direction類和Dot類,前面都是理論的講解,這篇來一個實戰,做一個類似坦克游戲的程序。
http://m.survivalescaperooms.com/files/media/dirtanke.swf
這幾天也寫了一些類了,是驢子還是騾子,拿出來遛一遛就知道了,先看這個上面這個flash動畫!

一個類似坦克游戲的demo程序
使用Direction類來進行方向控制
使用Dot類來計算距離
用上Direction類和Dot類之后,這個demo程序變得異常簡單額。。
也沒什么好說,主要透過這個例子,讓大家類熟悉Direction類和Dot類的使用方法
不懂的可以在后面跟帖提問,高手如果看到什么有錯誤的地方,請指正出來,多謝指教

下面是fla的源代碼:

CODE:
import index.base.game.Direction;
import index.base.events.DirectionEvent;
import index.base.geom.Dot;

//舞臺屬性設置
stage.showDefaultContextMenu = false;
stage.align = "TL";
stage.scaleMode = "noScale";

//創建坦克
var tank:Tank = new Tank;
tank.x = tank.y = 250;
addChild(tank);

//創建綁定坦克的點
var dot:Dot = new Dot;
dot.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.stageWidth;
if(tank.y > stage.stageHeight) tank.y = stage.stageHeight;
}

//是否可以發射炮臺,子彈
var isBullet:Boolean = true;
var isShell:Boolean = true;
//炮臺發射轉動
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的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出來,詳細的原因,請自己下載提供的源文件,下載下來看看吧。。不懂跟帖問!
點擊下載源文件

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彰武县| 新津县| 台湾省| 盐源县| 扎赉特旗| 安新县| 顺昌县| 平罗县| 上杭县| 北川| 舞钢市| 富宁县| 云浮市| 佛冈县| 威海市| 雷州市| 莱西市| 乌拉特后旗| 怀集县| 茌平县| 集安市| 东莞市| 福泉市| 顺义区| 汤阴县| 晋江市| 湘潭县| 广东省| 乌鲁木齐县| 平和县| 河曲县| 马公市| 西城区| 万荣县| 柯坪县| 溆浦县| 长武县| 长子县| 信丰县| 高密市| 义马市|