libgdx实例metagun代码分析(三)
继续接上篇 http://macken.iteye.com/blog/1816783
写写metagun的图片加载机制
libgdx的坐标系使用的是笛卡尔坐标系,原点位于左下角。由于计算机图形学的历史,图形的渲染基本都是左上角开始,这种渲染方式也比较舒服。因此需要设置一下坐标系的原点为左上角;设置代码
public final void init (Metagun metagun) {this.metagun = metagun;Matrix4 projection = new Matrix4();projection.setToOrtho(0, 320, 240, 0, -1, 1);//设置坐标原点位于左上角 正常libgdx的坐标位于右下角spriteBatch = new SpriteBatch(100);spriteBatch.setProjectionMatrix(projection);}
?加载图片素材的代码如下
private static TextureRegion[][] split (String name, int width, int height, boolean flipX) {Texture texture = new Texture(Gdx.files.internal(name));int xSlices = texture.getWidth() / width;int ySlices = texture.getHeight() / height;TextureRegion[][] res = new TextureRegion[xSlices][ySlices];for (int x = 0; x < xSlices; x++) {for (int y = 0; y < ySlices; y++) {res[x][y] = new TextureRegion(texture, x * width, y * height, width, height);res[x][y].flip(flipX, true);// Y轴翻转}}return res;}
?
加载读取图片素材是以左上角为原点,由于之前设置将原点由左下角变更为左上角,对Y坐标进行了翻转,因此在绘图是也需要将图片素材的y坐标进行翻转,因此使用了flip(x,true)函数进行翻转。
?
?
人物在地图中可以向左或向右移动,每个方向的人物图片都一样,这里借用对图片进行X轴翻转flip(true,x)实现了只需要保存一个方向的图片即可。
?
?