Flash Game 跳躍程式中,當按下空白鍵,遊戲物件會往上跳躍後落下,在程式中跳躍速度預

設為 –18 ,當按下空白鍵開始跳躍,跳躍速度會依續加上 2,ex( –18,-16,-14,-12,-10,-8,-6,

-4,-2,0,2,4,6,8,10,12,14,16…),物件的 y 座標會依續加上這些值(跳躍速度),遊戲物件會呈

現一開始往上,直到跳躍速度為 0 後,跳躍停止,物件 y 座標開始加上正整數,物件往下落

下,直到遇到可站立的 tile 才停止落下(座標系統:Flash 畫面的左上方為 x=0 y=0)。

 

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/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 = -18;
        private var isJump: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, 0, 0, 0, 0, 0, 0, 1, 1, 1], 
                          [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
                          [1, 0, 0, 0, 0, 1, 1, 0, 0, 1], 
                          [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
                          [1, 0, 0, 1, 1, 0, 0, 0, 0, 1],
                          [1, 0, 0, 0, 0, 0, 0, 0, 1, 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 = 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] == 0 && Map[numY][temp]==0 ) {    
                        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] == 0 && Map[numY][temp]==0) {                    
                        hero.y = hero.y + jumpspeed;
                    }else{                                
                        hero.y = (numY * 19) - 10;
                        isJump = false;                        
                    }    
                                        
                }
            }
                    
            if (key.isDown(Keyboard.SPACE) && isJump!=true) {                
                isJump = true;    
                jumpspeed = -18;                                                
            }            
            
            
            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;
                }
                            
            
            }
            
            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;
                }
                                
            }
            
            //偵測物件下方是否有可以站立的 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) {                    
                isJump = true;
            }
                                        
            
        }        
            
        
        //建立地圖
        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)
                    }
                    
                }
            }
        }
        
    }
    
 
}

檔案下載:Jump.rar ( FlashDevelop AS3 Project )

參考網站:http://www.tonypa.pri.ee/tbw/index.html

iammic 發表在 痞客邦 PIXNET 留言(0) 人氣()