2011-04-29 13 views
10

मैंने अभी जावा और libgdx का उपयोग करना शुरू कर दिया है और यह कोड है, बहुत आसानी से यह स्क्रीन पर एक स्प्राइट प्रिंट करता है। यह पूरी तरह से काम करता है, और मैंने इससे बहुत कुछ सीखा।libGDX का उपयोग कर कीबोर्ड कुंजी के साथ एक स्प्राइट कैसे स्थानांतरित करें?

package com.MarioGame; 

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.files.FileHandle; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.InputProcessor; 

public class Game implements ApplicationListener { 
private SpriteBatch batch; 
private Texture marioTexture; 
private Sprite mario; 
private int marioX; 
private int marioY; 

@Override 
public void create() { 
    batch = new SpriteBatch(); 
    FileHandle marioFileHandle = Gdx.files.internal("mario.png"); 
    marioTexture = new Texture(marioFileHandle); 
    mario = new Sprite(marioTexture, 0, 158, 32, 64); 
    marioX = 0; 
    marioY = 0; 
} 

@Override 
public void render() { 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    batch.begin(); 
    batch.draw(mario, marioX, marioY); 
    batch.end(); 
} 


@Override 
public void resume() { 
} 

@Override 
public void resize(int width, int height) { 
} 

@Override 
public void pause() { 
} 

@Override 
public void dispose() { 
} 

}

कैसे जब कोई उपयोगकर्ता प्रेस उनके कीबोर्ड पर D मैं marioX मूल्य को संशोधित करेगा?

उत्तर

31

आपके काम के लिए आपको इनपुटप्रोसेसर को लागू करने की आवश्यकता भी नहीं हो सकती है। आप इस तरह की रेंडर() विधि में Input.isKeyPressed() विधि का उपयोग कर सकते हैं।

float marioSpeed = 10.0f; // 10 pixels per second. 
float marioX; 
float marioY; 

public void render() { 
    if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) 
     marioX -= Gdx.graphics.getDeltaTime() * marioSpeed; 
    if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) 
     marioX += Gdx.graphics.getDeltaTime() * marioSpeed; 
    if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) 
     marioY += Gdx.graphics.getDeltaTime() * marioSpeed; 
    if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) 
     marioY -= Gdx.graphics.getDeltaTime() * marioSpeed; 

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    batch.begin(); 
    batch.draw(mario, (int)marioX, (int)marioY); 
    batch.end(); 
} 

यह भी ध्यान दें कि मैंने मारियो फ्लोट की स्थिति निर्देशांक बनाया है और आंदोलन को समय-आधारित आंदोलन में बदल दिया है। मारियोस्पेड पिक्सल की संख्या है मारियो को प्रति सेकंड किसी भी दिशा में यात्रा करनी चाहिए। Gdx.graphics.getDeltaTime() आपको सेकेंड में रेंडर() को अंतिम कॉल के बाद से पारित समय देता है। ज्यादातर परिस्थितियों में int को कास्ट वास्तव में आवश्यक नहीं है।

बीटीडब्ल्यू, हमारे पास http://www.badlogicgames.com/forum पर फ़ोरम हैं जहां आप libgdx विशिष्ट प्रश्न भी पूछते हैं!

hth, मारियो

2

आपके गेम लूप विधि (शायद प्रस्तुत करना या बनाना) में इनपुट हैंडलर जोड़ने की आवश्यकता है (लागू करना इनपुटप्रोसेसर)। वर्ग InputProcessor तरह तरीकों:

public boolean keyDown (int keycode); 

/** 
* Called when a key was released 
* 
* @param keycode one of the constants in {@link Input.Keys} 
* @return whether the input was processed 
*/ 
public boolean keyUp (int keycode); 

/** 
* Called when a key was typed 
* 
* @param character The character 
* @return whether the input was processed 
*/ 
public boolean keyTyped (char character); 

और वर्ग Input.Keys keycodes के रूप में स्थिर varibles का एक बहुत है। उदाहरण के लिए:

 public static final int D = 32; 
     public static final int A = 29; 
     public static final int S = 47; 
     public static final int W = 51; 

तो, कुंजी * तरीकों को लागू करने और जाँच लें कि कुछ महत्वपूर्ण दबाया जाता है और वेतन वृद्धि या कमी एक्स/मारियो से Y।

संपादित करें: चेक इस लिंक: http://code.google.com/p/libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/InputProcessor.java http://code.google.com/p/libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/Input.java

या, ट्रंक में, कैसे एक क़ौम को संभाल इनपुट: http://code.google.com/p/libgdx/source/browse/#svn%2Ftrunk%2Fdemos

आशा मदद

+0

dominicbri7 ने KeyListener इंटरफ़ेस का उपयोग कैसे किया? और आपने इनपुटप्रोसेसर का इस्तेमाल किया? क्या उनके बीच कोई अंतर है? – dotty

+0

@ डॉटी 'कीलिस्टर' एक जावा (मूल) वर्ग है। और 'इनपुटप्रोसेसर' एक जीडीएक्स वर्ग है। मेरे अनुभव में जब मैं इंजन का उपयोग कर रहा हूं तो हमेशा इंजन का उपयोग करता है। – jotapdiez

2

आप कीबोर्ड कार्रवाई का पता लगाने के इंटरफ़ेस KeyListener उपयोग कर सकते हैं।

public class Game implements ApplicationListener, KeyListener { 

@Override 
public void create() { 
    //Important 
    this.addKeyListener(this); 

    // TODO Auto-generated method stub 
    batch = new SpriteBatch(); 

    FileHandle marioFileHandle = Gdx.files.internal("mario.png"); 
    marioTexture = new Texture(marioFileHandle); 
    mario = new Sprite(marioTexture, 0, 158, 32, 64); 

    marioX = 0; 
    marioY = 0; 


} 

    @Override 
    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == 68) { //it's the 'D' key 
      //Move your mario 
     } 
    } 

} 
+0

रेंडर विधि में keyPressed() होना चाहिए, ऐसा लगता है कि मैं यह हर फ्रेम जांचना चाहता हूं? – dotty

0

मेरे पसंदीदा तरीका एक InputController जो सभी मानक की जाँच के लिए तैयार कुंजी संग्रहीत करता है उपयोग करने के लिए है।

import com.badlogic.gdx.Input.Keys; 
import com.badlogic.gdx.InputProcessor; 
import com.badlogic.gdx.math.Vector2; 

public class KeyboardController implements InputProcessor { 
    public boolean left,right,up,down; 
    public boolean isMouse1Down, isMouse2Down,isMouse3Down; 
    public boolean isDragged; 
    public Vector2 mouseLocation = new Vector2(0,0); 

    @Override 
    public boolean keyDown(int keycode) { 
     boolean keyProcessed = false; 
     switch (keycode) // switch code base on the variable keycode 
     { 
      case Keys.LEFT:  // if keycode is the same as Keys.LEFT a.k.a 21 
       left = true; // do this 
       keyProcessed = true; // we have reacted to a keypress 
       break; 
      case Keys.RIGHT: // if keycode is the same as Keys.LEFT a.k.a 22 
       right = true; // do this 
       keyProcessed = true; // we have reacted to a keypress 
       break; 
      case Keys.UP:  // if keycode is the same as Keys.LEFT a.k.a 19 
       up = true;  // do this 
       keyProcessed = true; // we have reacted to a keypress 
       break; 
      case Keys.DOWN:  // if keycode is the same as Keys.LEFT a.k.a 20 
       down = true; // do this 
       keyProcessed = true; // we have reacted to a keypress 
     } 
     return keyProcessed; // return our peyProcessed flag 
    } 
    @Override 
    public boolean keyUp(int keycode) { 
     boolean keyProcessed = false; 
     switch (keycode) // switch code base on the variable keycode 
     { 
      case Keys.LEFT:  // if keycode is the same as Keys.LEFT a.k.a 21 
       left = false; // do this 
       keyProcessed = true; // we have reacted to a keypress 
       break; 
      case Keys.RIGHT: // if keycode is the same as Keys.LEFT a.k.a 22 
       right = false; // do this 
       keyProcessed = true; // we have reacted to a keypress 
       break; 
      case Keys.UP:  // if keycode is the same as Keys.LEFT a.k.a 19 
       up = false;  // do this 
       keyProcessed = true; // we have reacted to a keypress 
       break; 
      case Keys.DOWN:  // if keycode is the same as Keys.LEFT a.k.a 20 
       down = false; // do this 
       keyProcessed = true; // we have reacted to a keypress 
     } 
     return keyProcessed; // return our peyProcessed flag 
    } 
    @Override 
    public boolean keyTyped(char character) { 
     return false; 
    } 
    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
     if(button == 0){ 
      isMouse1Down = true; 
     }else if(button == 1){ 
      isMouse2Down = true; 
     }else if(button == 2){ 
      isMouse3Down = true; 
     } 
     mouseLocation.x = screenX; 
     mouseLocation.y = screenY; 
     return false; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
     isDragged = false; 
     //System.out.println(button); 
     if(button == 0){ 
      isMouse1Down = false; 
     }else if(button == 1){ 
      isMouse2Down = false; 
     }else if(button == 2){ 
      isMouse3Down = false; 
     } 
     mouseLocation.x = screenX; 
     mouseLocation.y = screenY; 
     return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) { 
     isDragged = true; 
     mouseLocation.x = screenX; 
     mouseLocation.y = screenY; 
     return false; 
    } 

    @Override 
    public boolean mouseMoved(int screenX, int screenY) { 
     mouseLocation.x = screenX; 
     mouseLocation.y = screenY; 
     return false; 
    } 
    @Override 
    public boolean scrolled(int amount) { 
     return false; 
    } 
} 

तब सब मैं क्या करने की जरूरत

controller = new KeyboardController(); 

के साथ अपने खेल के बनाने के विधि में KeyboardController कर फिर इसका इस्तेमाल करने की घटनाओं

Gdx.input.setInputProcessor(controller); 

अंत के लिए सुनने के लिए GDX को बयां करती हैं अगर मैं जांचना चाहता हूं कि कोई कुंजी दबाई गई है तो मैं

if(controller.left){ 
    player.x -= 1; 
} 
पर जा सकता हूं