libgdx简单入门
最近一直想找个android的游戏引擎来研究,但是资源少、大多都不太出名,找了半天,发现libgdx还算不错,于是照着官网的例子,运行了下helloworld示例,发现这个引擎还不错,代码相对来说不算复杂,而且功能也算强大(虽然对3d的支持好像很弱),入门也容易。
这个helloworld是官方自带的,主要由两个类组成,一个是HelloWorldAndroid:
public class HelloWorldAndroid extends AndroidApplication {@Overridepublic void onCreate (Bundle savedInstanceState) {super.onCreate(savedInstanceState);initialize(new HelloWorld(), false);}}
public class HelloWorld implements ApplicationListener {SpriteBatch spriteBatch;Texture texture;BitmapFont font;Vector2 textPosition = new Vector2(100, 100);Vector2 textDirection = new Vector2(1, 1);@Overridepublic void create () {Log.v("====create======", "===="+new Date());font = new BitmapFont();font.setColor(Color.RED);texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));spriteBatch = new SpriteBatch();}@Overridepublic void render () {int centerX = Gdx.graphics.getWidth() / 2;int centerY = Gdx.graphics.getHeight() / 2;Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT);// more fun but confusing :)// textPosition.add(textDirection.tmp().mul(Gdx.graphics.getDeltaTime()).mul(60));textPosition.x += textDirection.x * Gdx.graphics.getDeltaTime() * 60;textPosition.y += textDirection.y * Gdx.graphics.getDeltaTime() * 60;if (textPosition.x < 0) {textDirection.x = -textDirection.x;textPosition.x = 0;}if (textPosition.x > Gdx.graphics.getWidth()) {textDirection.x = -textDirection.x;textPosition.x = Gdx.graphics.getWidth();}if (textPosition.y < 0) {textDirection.y = -textDirection.y;textPosition.y = 0;}if (textPosition.y > Gdx.graphics.getHeight()) {textDirection.y = -textDirection.y;textPosition.y = Gdx.graphics.getHeight();}spriteBatch.begin();spriteBatch.setColor(Color.WHITE);spriteBatch.draw(texture, centerX - texture.getWidth() / 2, centerY - texture.getHeight() / 2, 0, 0, texture.getWidth(),texture.getHeight());font.draw(spriteBatch, "Hello World!", (int)textPosition.x, (int)textPosition.y);spriteBatch.end();}@Overridepublic void resize (int width, int height) {spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);textPosition.set(0, 0);}.....