2016-03-24 34 views
2

İlk PacMan oyunumu yazmaya çalışıyorum. Bu yüzden bir rehber takip ettim ve küçük bir oyun motoru oluşturdum ve şimdi oyun motorunu paket olarak içeren PacMan.java dosyasında kod yazıyorum. Sorun, kılavuzun KeyPressed yöntemini geçersiz kılmanızı bildirir, ancak bunu yaptığımda NOTHING olur. Ekrandaki PacMan'ın yönünü, KeyPressed() yöntemine göre bastığım ok tuşlarına göre değiştirmesi gerekiyordu, ancak OK tuşlarına bastığımda NOTHING gerçekleşiyor. Bu kodu alsam bile, küçük pacman'ı ok tuşları üzerinden kontrol edemiyorum!Neden KeyPressed yöntemim hiç çağrılmadı?

Basit bir System.out.println(); KeyPressed() yönteminin gerçekten hiç çağrıldığını görmek için KeyPressed() yönteminin içinde ifade, ancak ASLA denir? oklara bastığımda "tuşuna bastığımda" basılmıyor ve bastığımda PacMan da hareket etmiyor. Anahtarmama yöntemimin neden ok tuşlarına bastığımda çağrılmayacağını anlayabilecek biri var mı?

Kodum: Açıkçası

package game.pacman; 

import org.game.engine.Game; 
import org.game.engine.GameApplication; 

import javax.imageio.ImageIO; 
import java.awt.*; 
import java.awt.event.KeyEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

public class PacMan extends Game { 

    public static void main(String[] args){ 

     GameApplication.start(new PacMan()); 

    } 

    BufferedImage pacman; 
    int frame; 
    int dir; 
    int x, y; 
    final int STEP = 2; 

    public PacMan(){ 
     title = "PacMan"; 
     frame = 0; 
     dir = KeyEvent.VK_RIGHT; 
     x = 300; 
     y = 200; 
     width = height = 400; 

     try { 
      pacman = ImageIO.read(new File("images/packman.png")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } 

    @Override 
    public void keyPressed(KeyEvent e){ 

     System.out.println("pressed the key"); 
     dir = e.getKeyCode(); 
    } 

    @Override 
    public void update(){ 
     frame++; 

     if (frame > 2){ 
      frame = 0; 
     } 
     switch (dir){ 
      case KeyEvent.VK_LEFT: 
       x -= STEP; 
       break; 
      case KeyEvent.VK_RIGHT: 
       x += STEP; 
       break; 
      case KeyEvent.VK_UP: 
       y -= STEP; 
       break; 
      case KeyEvent.VK_DOWN: 
       y += STEP; 
     } 
     if (x < 0){ 
      x = 0; 
     } else if (x > width-28-STEP){ 
      x = width-28-STEP; 
     } 
    } 

    @Override 
    public void draw(Graphics2D g) { 
     g.drawImage(pacman.getSubimage(frame*30,(dir-37)*30,28,28), x, y, null); 
    } 
} 

o (paketine aracılığıyla) KeyListener, MouseListener, MouseMotionListener uygulayan oyun motoru içinde bir sınıf Game.java içine keyPressed bir yöntemi geçersiz gerekiyordu. Eğer KeyPressed() yöntemini gördüğünüz gibi

package org.game.engine; 


import java.awt.*; 
import java.awt.event.*; 

public abstract class Game implements KeyListener, MouseListener, MouseMotionListener{ 

    protected boolean over; 
    protected int delay = 30; 
    protected int width = 400; 
    protected int height = 400; 
    protected String title = "MyGame"; 


    public String getTitle(){ 
     return title; 
    } 
    public long getDelay(){ 
     return delay; 
    } 

    public int getWidth(){ 
     return width; 
    } 
    public int getHeight(){ 
     return height; 
    } 

    public void init(){} 
    abstract public void update(); 
    abstract public void draw(Graphics2D g); 

    public boolean isOver(){ 
     return over; 
    } 



    public void keyTyped(KeyEvent e) { 

    } 


    public void keyPressed(KeyEvent e) { 

    } 


    public void keyReleased(KeyEvent e) { 

    } 


    public void mouseClicked(MouseEvent e) { 

    } 


    public void mousePressed(MouseEvent e) { 

    } 


    public void mouseReleased(MouseEvent e) { 

    } 


    public void mouseEntered(MouseEvent e) { 

    } 


    public void mouseExited(MouseEvent e) { 

    } 


    public void mouseDragged(MouseEvent e) { 

    } 


    public void mouseMoved(MouseEvent e) { 

    } 
} 

sınıf içinde sadece boş ve bu kılavuz olması talimatını nasıl: İşte o sınıf için tüm kodu kodudur. Ben PacMan sınıfında (ana sınıfım) geçersiz kılıyorum. Peki neden ok tuşları çalışmıyor, neden programı çalıştırdığımda neden KeyPressed yönteminin çağrılmıyor? KeyPressed() yöntemini çağırmak için eksik bir şey var mı?

cevap

1

Oyun sınıfındaki işleyicileri uygulamanıza ve bunları alt sınıfa yerleştirmenize rağmen, işleyiciyi istediğiniz öğeye kaydettirmediniz. eski erkek iç Oyun sınıf için

,

something.onKeyPress(this) 

// where this is Game class which is of type KeyListener, MouseListener, MouseMotionListener 
+0

Üzgünüz ama anlamıyorum. Game sınıfı içinde KeyPressed yönteminin içine "something.onKeyPress (this)" eklemem gerektiğini mi söylüyorsunuz? Veya çalışmasını sağlamak için ne değiştirmeliyim? – user5846939

+1

@OfeliavanAnalhard Tuşa basmak istediğiniz hangi eleman üzerinde? –

+0

Öğe burada ne anlama geliyor? Böyle bir başlangıç ​​için üzgünüm ama "element" ile ne demek istediğini anlamadım ... – user5846939

1

bunu aynı şekilde yapın için bileşen JTextField o zaman aynı şekilde olmalıdır ise, Key-Listener ile

bileşeni kaydetmek zorunda

JTextField typingArea = new JTextField(20); 
typingArea.addKeyListener(this); 

iç tuşuna basarken, typingArea - JTextField, bu yöntem de denir,

public void keyPressed(KeyEvent e) { 
     displayInfo(e, "KEY PRESSED: "); 
    } 
İlgili konular