程式中使用 ↑↓ 上下鍵進行爬繩的動作,在判斷操作過程中,程式很簡單的判斷遊戲 Hero 物件
是否與繩索接觸, 才確定爬繩動作(遊戲物件上下移動操作)是否生效。
Flash 程式:(← 左 →右 ↑↓ 上下爬繩 空白鍵:跳躍)
程式碼:
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;
[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/Tile4.PNG')]
private var Tile4:Class; //繩索
[Embed(source = '../Lib/hero.PNG')]
private var Hero:Class;
private var hero:Bitmap = new Hero();
private var speed:int = 6;
private var time:Timer;
private var key:KeyPoll;
private var jumpspeed:int;
private var isJump:Boolean = false;
private var isClimb:Boolean = false;
//地圖陣列
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, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 0, 3, 0, 0, 0, 0, 3, 0, 1],
[1, 0, 3, 0, 0, 1, 1, 3, 0, 1],
[1, 0, 3, 0, 0, 0, 0, 3, 0, 1],
[1, 0, 3, 0, 0, 0, 0, 3, 0, 1],
[1, 0, 3, 0, 0, 0, 0, 3, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 3, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
public function Game()
{
buildMap();
hero.x = 20;
hero.y = 161;
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(isJump==true){
if (jumpspeed >= 18) {
jumpspeed = 18;
}else{
jumpspeed += 2;
}
if (jumpspeed < 0) {
numX = hero.x / 19;
numY = (hero.y + jumpspeed) / 19;
temp = (hero.x + hero.width - 1) / 19;
//碰撞偵測,同時檢測 hero 左上方與右上方是否可以通過
if (Map[numY][numX] != 1 && Map[numY][temp]!=1 ) {
hero.y = hero.y + jumpspeed;
}else {
hero.y = (numY+1) * 19;
jumpspeed = 2;
}
}else {
numX = hero.x / 19;
numY = (hero.y + jumpspeed + hero.height) / 19;
temp = (hero.x + hero.width-1) / 19;
//碰撞偵測,同時檢測 hero 左下方與右下方是否可以通過
if (Map[numY][numX] != 1 && Map[numY][temp]!=1 ) {
hero.y = hero.y + jumpspeed;
}else{
hero.y = (numY * 19) - 10;
isJump = false;
}
}
}
if (key.isDown(Keyboard.UP) ) {
numX = Math.floor(hero.x/19);
numY = Math.floor(hero.y/19);
//碰撞偵測,檢測 hero 是否與繩索接觸
if (Map[numY][numX] == 3 && (Math.floor(hero.x/19)*19)+19 > hero.x+10 ) {
isClimb = true;
isJump = false;
jumpspeed = 0;
hero.x = ((numX) * 19)+5;
numX = hero.x / 19;
numY = (hero.y - speed) / 19;
temp = (hero.x + hero.width - 1) / 19;
//碰撞偵測,同時檢測 hero 左上方與右上方是否可以通過
if (Map[numY][numX] != 1 && Map[numY][temp] != 1) {
hero.y -=speed ;
}else{
hero.y = (numY+1) * 19;
}
}
}
if (key.isDown(Keyboard.DOWN)) {
numX = hero.x / 19;
numY = (hero.y + speed + hero.height) / 19;
temp = (hero.x + hero.width-1) / 19;
if (Map[numY][numX] == 3) {
//碰撞偵測,同時檢測 hero 左下方與右下方是否可以通過
if (Map[numY][numX] != 1 && Map[numY][temp]!=1) {
hero.y +=speed ;
}else{
hero.y = (numY * 19)-hero.height;
}
}else {
isClimb = false;
isJump = true;
}
}
//跳躍
if (key.isDown(Keyboard.SPACE) && isJump!=true) {
isJump = true;
isClimb = false;
jumpspeed = -14;
}
if (key.isDown(Keyboard.RIGHT)) {
numX = (hero.x+speed+hero.height) / 19;
numY = hero.y / 19;
temp = (hero.y + hero.height-1) / 19;
if(isClimb){
isClimb = false;
isJump = true;
jumpspeed = 0;
}
//碰撞偵測,同時檢測 hero 右上方與右下方是否可以通過
if ((Map[numY][numX] == 0 && Map[temp][numX] == 0)
|| (Map[numY][numX] == 3 )) {
hero.x += speed ;
}else{
hero.x = (numX * 19)-hero.width;
}
}
if (key.isDown(Keyboard.LEFT)) {
numX = (hero.x-speed) / 19;
numY = hero.y / 19;
temp = (hero.y + hero.height - 1) / 19;
if(isClimb){
isClimb = false;
isJump = true;
jumpspeed = 0;
}
//碰撞偵測,同時檢測 hero 左上方與左下方是否可以通過
if ((Map[numY][numX] == 0 && Map[temp][numX] == 0)
|| (Map[numY][numX] == 3)) {
hero.x -=speed ;
}else{
hero.x = (numX+1) * 19;
}
}
//偵測物件下方是否有可以站立的 tile,如無則落下
numX = hero.x / 19;
numY = (hero.y + speed + hero.height) / 19;
temp = (hero.x + hero.width-1) / 19;
if ((Map[numY][numX] == 0 && Map[numY][temp] == 0)
|| Map[numY][numX] == 3 || Map[numY][temp] == 3) {
if(!isClimb){
isJump = true;
}
}
numX = hero.x / 19;
numY = hero.y / 19;
if (Map[numY][numX] != 3 && isClimb) {
isClimb = false;
jumpspeed = 0;
}
}
//建立地圖
public function buildMap():void {
var tempTile:Bitmap;
for (var i:int = 0; i < Map.length; i++ ) {
for (var j:int = 0; j < Map[0].length; 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)
}else if (Map[i][j] == 3) { //繩索
tempTile = new Tile4();
tempTile.x=j*19
tempTile.y = i * 19
addChild(tempTile)
}
}
}
}
}
}
檔案下載:Rope.rar ( FlashDevelop AS3 Project )
全站熱搜