在遊戲中, Hero 行走過程中如果遇到障礙物,Hero 則無法在繼續前進,需要繞過障礙物,才
可在向前邁進,在程式中需要使用碰撞偵測技術,在 Hero 行走過程,會依照行走方向,對物件
加上 X or Y 座標值,在程式中會提前去偵測,當物件加上某個座標數值後是否物件會與障礙物重
疊,如果重疊的話,程式就不會在加上這個前進座標的數值。
程式畫面:
碰撞偵測:程式會以 Hero 座標加上前進座標,對映出地圖陣列上的位置,判斷是否可行走
(程式設定地圖陣列數值為 0 才可行走 ),在來決定Hero 物件可以加上的前進座標數值。
原始碼:
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.*;
import flash.geom.Point;
import flash.ui.Keyboard;
import flash.utils.Timer;
import uk.co.bigroom.input.KeyPoll;
/**
* ...
* @author
*/
[SWF(width=190,height=190,backgroundColor="#000000")]
public class Game extends Sprite
{
[Embed(source = '../Lib/Tile1.PNG')]
private var Tile1:Class;
[Embed(source = '../Lib/Tile2.PNG')]
private var Tile2:Class;
[Embed(source = '../Lib/Tile3.PNG')]
private var Tile3:Class;
[Embed(source = '../Lib/hero.PNG')]
private var Hero:Class;
private var hero:Bitmap = new Hero();
private var speed:int = 4;
private var time:Timer;
private var point:Point = new Point(20, 20);
private var key:KeyPoll;
//地圖陣列
private var Map:Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
];
public function Game()
{
buildMap();
hero.x = 20;
hero.y = 20;
addChild(hero);
key = new KeyPoll( this.stage );
//加入 Timer 事件
time = new Timer(80);
time.addEventListener(TimerEvent.TIMER, GameLoop);
time.start();
}
public function GameLoop(e:TimerEvent):void {
var num:int = 0;
var numX:int =0;
var numY:int = 0;
var temp:int = 0;
if (key.isDown(Keyboard.LEFT)) {
numX = (hero.x-speed) / 19;
numY = hero.y / 19;
temp = (hero.y + hero.height-1) / 19;
//碰撞偵測,同時檢測 hero 左上方與左下方是否可以通過
if (Map[numY][numX] == 0 && Map[temp][numX]==0) {
hero.x -=speed ;
}else{
hero.x = (numX+1) * 19;
}
}else if (key.isDown(Keyboard.RIGHT)) {
numX = (hero.x+speed+hero.height) / 19;
numY = hero.y / 19;
temp = (hero.y + hero.height-1) / 19;
//碰撞偵測,同時檢測 hero 右上方與右下方是否可以通過
if (Map[numY][numX] == 0 && Map[temp][numX]==0 ) {
hero.x +=speed ;
}else{
hero.x = (numX * 19)-hero.width;
}
}else if (key.isDown(Keyboard.UP)) {
numX = hero.x / 19;
numY = (hero.y - speed) / 19;
temp = (hero.x + hero.width - 1) / 19;
//碰撞偵測,同時檢測 hero 左上方與右上方是否可以通過
if (Map[numY][numX] == 0 && Map[numY][temp]==0 ) {
hero.y -=speed ;
}else{
hero.y = (numY+1) * 19;
}
}else if (key.isDown(Keyboard.DOWN)) {
numX = hero.x / 19;
numY = (hero.y + speed + hero.height) / 19;
temp = (hero.x + hero.width-1) / 19;
//碰撞偵測,同時檢測 hero 左下方與右下方是否可以通過
if (Map[numY][numX] == 0 && Map[numY][temp]==0) {
hero.y +=speed ;
}else{
hero.y = (numY * 19)-10;
}
}
}
//建立地圖
public function buildMap():void {
var tempTile:Bitmap;
for (var i:int = 0; i < 10; i++ ) {
for (var j:int = 0; j < 10; j++ ) {
if (Map[i][j] == 0) {
tempTile = new Tile1();
tempTile.x=j*19
tempTile.y = i * 19
addChild(tempTile)
}else if (Map[i][j] == 1){
tempTile = new Tile2();
tempTile.x=j*19
tempTile.y = i * 19
addChild(tempTile)
}else if (Map[i][j] == 2){
tempTile = new Tile3();
tempTile.x=j*19
tempTile.y = i * 19
addChild(tempTile)
}
}
}
}
}
}
註:程式加入 KeyPoll 方式,讓行走時更為順暢(2010/11/20)
http://code.google.com/p/bigroom/wiki/KeyPoll
檔案下載:HitTest ( FlashDevelop AS3 Project )
全站熱搜
留言列表