<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:

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

    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)

    譯:這個(gè)監(jiān)聽按鍵的按下和釋放通過主類中一組boolean類型的變量記錄對(duì)應(yīng)按鍵的狀態(tài)(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畫布,其實(shí)就是Game類)通過Game構(gòu)造函數(shù)添加下面一代碼來實(shí)現(xiàn)

    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類的構(gòu)造函數(shù)中后,當(dāng)按鍵被按下時(shí),對(duì)應(yīng)的boolean類型的狀態(tài)標(biāo)識(shí)變量將被設(shè)置合適地值。下一步要做的在游戲運(yùn)行過程中實(shí)時(shí)響應(yīng)這些布爾標(biāo)志設(shè)置如果我們?cè)谟螒蛑刑砑?/span>了這一功能,我們為玩家飛船添加鍵盤控制:

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

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

    // update the movement appropraitely  實(shí)現(xiàn)飛船的運(yùn)動(dòng)。開始時(shí)飛船不移動(dòng)。如果任何一個(gè)方向鍵被按下,那么非常就向這個(gè)方向運(yùn)動(dòng)

    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.

    譯:正如你看到的,我們檢查建還是建被按下。如果是左鍵按下,我們新水平移動(dòng)速度為負(fù)值,相反,為正值。因此,當(dāng)對(duì)應(yīng)的代碼被執(zhí)行,飛船將向左或右移動(dòng)

    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()"

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

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

    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:

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

    public void tryToFire() {

    // check that we have waiting long enough to fire 檢查我們是否已經(jīng)等待了足夠長(zhǎng)的時(shí)間了

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

    return;

    }

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

    //如果等候了足夠的時(shí)間,創(chuàng)建子彈實(shí)體,并記錄時(shí)間

    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.

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

    學(xué)軟件開發(fā),到蜂鳥科技!
    超強(qiáng)的師資力量 、完善的課程體系 、超低的培訓(xùn)價(jià)格 、真實(shí)的企業(yè)項(xiàng)目。

    網(wǎng)址:www.ntcsoft.com 
    電話:0371-63839606 
    鄭州軟件開發(fā)興趣小組群:38236716

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

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(2)

    我參與的團(tuán)隊(duì)

    文章檔案(22)

    搜索

    •  

    最新評(píng)論

    主站蜘蛛池模板: 污污污视频在线免费观看| 亚洲美女视频免费| 免费一级毛片一级毛片aa| 国产精品免费小视频| 四虎永久免费影院在线| 免费永久国产在线视频| 国产乱弄免费视频| 亚洲国产人成中文幕一级二级| 亚洲欧洲久久av| 亚洲综合无码精品一区二区三区| 亚洲人成色7777在线观看| 久久夜色精品国产亚洲| 亚洲综合国产精品| 色老板亚洲视频免在线观| 亚洲一区二区观看播放| 日韩亚洲综合精品国产| 一级毛片高清免费播放| 久久免费精品视频| 4399影视免费观看高清直播| 在线视频免费观看爽爽爽| 天天摸天天操免费播放小视频| 破了亲妺妺的处免费视频国产| www.亚洲色图.com| 日韩免费在线视频| 1a级毛片免费观看| 最近中文字幕免费mv视频8| 免费永久看黄在线观看app| 亚洲精品无码久久久久去q| 亚洲人成网址在线观看| 国产精品亚洲午夜一区二区三区| 亚洲国产成人久久精品大牛影视| 日韩精品免费一线在线观看| 成全在线观看免费观看大全| av无码国产在线看免费网站| 国产乱色精品成人免费视频 | 国产视频精品免费视频| 男女作爱在线播放免费网站| 青青草免费在线视频| 久久亚洲高清综合| 亚洲精品视频在线播放| 亚洲狠狠婷婷综合久久蜜芽|