<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0,  comments - 6,  trackbacks - 0

    Keyboard Input 鍵盤輸入

    Our next step is to make the ship controllable. To do this we need to add a simple inner class to our main game. It looks like this:

    譯:我們的下一個步驟是使飛船可控。為了做到這一點,我們需要我們的主游戲類中添加一個簡單的內部類。它看起來像這樣:

    private class KeyInputHandler extends KeyAdapter {

    public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {

    leftPressed = true;

    }

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

    rightPressed = true;

    }

    if (e.getKeyCode() == KeyEvent.VK_SPACE) {

    firePressed = true;

    }

    public void keyReleased(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {

    leftPressed = false;

    }

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

    rightPressed = false;

    }

    if (e.getKeyCode() == KeyEvent.VK_SPACE) {

    firePressed = false;

    }

    }

    public void keyTyped(KeyEvent e) {

    // if we hit escape, then quit the game

    if (e.getKeyChar() == 27) {

    System.exit(0);

    }

    }

    }

    This class simply picks up keys being pressed and released and records their states in a set of boolean variables in the main class. In addition it checks for escape being pressed (which in our case exits the game)

    譯:這個監聽按鍵的按下和釋放通過主類中一組boolean類型的變量記錄對應按鍵的狀態(true按下,false釋放)。此外,它還檢查esc按鈕是否被按下(在本例中退出游戲)

    To make this class work we need to add it to our canvas as a "KeyListener". This is done by adding the following line to the constructor of our Game class:

    譯:為了使這一類工作,我們需要將它作為 “KeyListener” 添加到我們的 canvas畫布,其實就是Game類)通過Game構造函數添加下面一代碼來實現

    addKeyListener(new KeyInputHandler());

    With this source code in place the boolean flags will be set to appropriate values as the keys are pressed. The next step is to respond to these boolean flags being set in the game loop. If we add this in the game loop we can add keyboard control to the ship:

    譯:上面的代碼添加到Game類的構造函數中后,按鍵被按下時,對應的boolean類型的狀態標識變量將被設置合適地值。下一步要做的在游戲運行過程中實時響應這些布爾標志設置如果我們在游戲中添加了這一功能,我們為玩家飛船添加鍵盤控制:

    // resolve the movement of the ship. First assume the ship 

    // isn't moving. If either cursor key is pressed then

    // update the movement appropraitely  實現飛船的運動。開始時飛船不移動。如果任何一個方向鍵被按下,那么非常就向這個方向運動

    ship.setHorizontalMovement(0);

    if ((leftPressed) && (!rightPressed)) {

    ship.setHorizontalMovement(-moveSpeed);

    } else if ((rightPressed) && (!leftPressed)) {

    ship.setHorizontalMovement(moveSpeed);

    }

    As you can see we check if left or right is pressed. If they are we update the movement values associated with the player's ship. Hence when we hit the entity movement code the ship will move left or right.

    譯:正如你看到的,我們檢查建還是建被按下。如果是左鍵按下,我們新水平移動速度為負值,相反,為正值。因此,當對應的代碼被執行,飛船將向左或右移動

    If the player holds the fire key we'd like the ship to fire a shot up towards the aliens. However, we don't want to let the player just keep firing. It'd be good to limit how often they can fire. We'll put this functionality in a utility method called (somewhat logically) "tryToFire()"

    譯:如果玩家按下開火鍵我們就要使飛船向外星人發射一顆子彈。但是,我們并不想讓玩家的飛船一直保持開火的狀態。限制飛船每隔一定時間開一次火是個不錯的主意。我們把這項功能交給一個叫“tryToFire”的實用方法來實現。

    // if we're pressing fire, attempt to fire 如果我們按下開發鍵,嘗試開火

    if (firePressed) {

    tryToFire();

    }

    To prevent the player firing too often we'll record the time whenever they take a shot and prevent them taking a shot if the last shot was less than a set interval ago. Hmm, sounds complicated, its not! really! Here it is:

    譯:為了阻止玩家過于頻繁的射擊,我們將記錄射擊時的時間,如果間隔時間少于規定間隔時間就不執行相應的射擊操作。恩,這聽起來很復雜,其實不是這樣的。下面就是實現源代碼:

    public void tryToFire() {

    // check that we have waiting long enough to fire 檢查我們是否已經等待了足夠長的時間了

    if (System.currentTimeMillis() - lastFire < firingInterval) {

    return;

    }

    // if we waited long enough, create the shot entity, and record the time.

    //如果等候了足夠的時間,創建子彈實體,并記錄時間

    lastFire = System.currentTimeMillis();

    ShotEntity shot = new ShotEntity(this,"sprites/shot.gif",ship.getX()+10,ship.getY()-30);

    entities.add(shot);

    }

    First we check if the last time the player took a shot was long enough ago. If it wasn't we don't bother firing and just return. If it was we record the current time. Next we create and add an entity to represent the shot fired by the player. Since our shot is setup to move up the screen it shoots off from the player towards the aliens.

    譯:首先,我們檢查距離上次玩家射擊的時間是否足夠長了,如果小于規定的間隔時間就不執行射擊的操作,并直接返回;反之,我們記錄當前時間。下一步,創建并添加一個實體來表示一顆玩家發射的子彈。于是在屏幕上,我們的子彈由玩家飛船發射,射向外星人。

    學軟件開發,到蜂鳥科技!
    超強的師資力量 、完善的課程體系 、超低的培訓價格 、真實的企業項目。

    網址:www.ntcsoft.com 
    電話:0371-63839606 
    鄭州軟件開發興趣小組群:38236716

    posted on 2010-11-25 23:59 whistler 閱讀(336) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(2)

    我參與的團隊

    文章檔案(22)

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 中文字幕成人免费视频| eeuss免费天堂影院| 污视频在线观看免费| 精品国产亚洲一区二区三区| 成年免费a级毛片| 爱情岛论坛网亚洲品质自拍| 特级毛片A级毛片免费播放| 又粗又大又硬又爽的免费视频| 亚洲av永久无码精品秋霞电影秋| 天天干在线免费视频| 成a人片亚洲日本久久| 免费在线观看中文字幕| 成年大片免费高清在线看黄| 国产av无码专区亚洲av果冻传媒| 怡红院免费全部视频在线视频| 亚洲国产成人精品不卡青青草原| 99久久免费中文字幕精品| 91在线精品亚洲一区二区| 日本亚洲免费无线码| 亚洲av午夜国产精品无码中文字| 夜色阁亚洲一区二区三区| 一级特黄特色的免费大片视频| 国产成人无码综合亚洲日韩| 曰批全过程免费视频播放网站 | 亚洲熟妇无码另类久久久| 久久精品国产免费| 亚洲伊人久久大香线蕉在观| 四色在线精品免费观看| 日韩一级片免费观看| 亚洲av无码一区二区三区网站 | 久久国产精品免费看| 亚洲影视一区二区| 免费在线黄色网址| 日本免费污片中国特一级| 亚洲三级在线免费观看| 亚洲福利视频一区二区| 久久精品国产这里是免费| 亚洲精品精华液一区二区| 亚洲精品中文字幕无码蜜桃| 国产精品久久久久久久久久免费 | 免费国产成人午夜电影|