i have started using java , libgdx , have code, prints sprite onto screen. works perfectly, , learnt lot it.
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() { }
}
how modify mariox
value when user presses d
on keyboard?
for task @ hand might not need implement inputprocessor. can use input.iskeypressed() method in render() method this.
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(); }
also note made position coordinates of mario floats , changed movement time-based movement. mariospeed number of pixels mario should travel in direction per second. gdx.graphics.getdeltatime() returns time passed since last call render() in seconds. cast int not necessary in situations.
btw, have forums @ http://www.badlogicgames.com/forum ask libgdx specific questions well!
hth, mario
Comments
Post a Comment