draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height,
float scaleX, float scaleY,float rotation)
请注意最后一个参数,它使图形会带有角度,可以根据运动的向量取得其角度。
关于该函数的说明:
Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height. The rectangle is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter clockwise rotation of the rectangle around originX, originY.
下面是代码,其中标注的 test pic_1, test pic_2, test pic_3, test pic_4, 分别是对应于图片 1 , 2 , 3 , 4 , 的测试语句。
package com.example.libgdx_sample_spirtebatch; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.FPSLogger; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; public class SampleRender { public static int WIDTH = 320; public static int HEIGHT = 480; OrthographicCamera m_Cam; SpriteBatch batch = new SpriteBatch(5460); FPSLogger fps = new FPSLogger(); ImmediateModeRenderer20 renderer = new ImmediateModeRenderer20(false, true, 0); private Animation m_MoveLeft; private Texture m_StaticTex = null; private float m_StateTime; Vector2 m_Pos = new Vector2(); Texture m_bkg = null; Vector2 m_Vel = new Vector2(); private TextureRegion m_region; public SampleRender() { this.m_Cam = new OrthographicCamera(WIDTH, HEIGHT); this.m_Cam.position.set(WIDTH/2, HEIGHT/2, 0); m_StateTime = 0; createAnimations(); this.m_Vel.set(8, 40); } public void update(float deltaTime) { m_Vel.y -= 4*deltaTime;//抛物线 m_Pos.add(m_Vel.x * deltaTime, m_Vel.y * deltaTime); m_StateTime += deltaTime; if (m_Pos.y < 0) { m_StateTime = 0; m_Vel.set(8, 40); m_Pos.x=0; m_Pos.y=0; } } private void createAnimations() { m_bkg = new Texture(Gdx.files.internal("data1.png")); m_region = new TextureRegion(m_bkg); Texture TankTexture = new Texture(Gdx.files.internal("data.png")); m_StaticTex = new Texture(Gdx.files.internal("data.png")); TextureRegion[] split = new TextureRegion(TankTexture).split(32, 32)[0]; TextureRegion[] mirror = new TextureRegion(TankTexture).split(32,32)[0]; for (TextureRegion region : mirror) region.flip(true, false); m_MoveLeft = new Animation(0.1f, split[0], split[1]); } Vector3 lerpTarget = new Vector3(); public void render (float deltaTime) { //m_Cam.position.lerp(lerpTarget.set(m_Pos.x, m_Pos.y, 0), 2f * deltaTime); //m_Cam.update(); //m_StateTime += deltaTime; renderer.begin(m_Cam.combined, GL20.GL_LINES); renderer.end(); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //batch.setProjectionMatrix(m_Cam.combined); batch.begin(); Animation anim = m_MoveLeft; batch.draw(m_region, 0, 0); // test pic_1 //batch.draw(m_StaticTex, m_Pos.x, m_Pos.y); // test pic_2 //batch.draw(m_StaticTex, m_Pos.x, m_Pos.y,32,32); // test pic_3 //batch.draw(anim.getKeyFrame(m_StateTime, true), m_Pos.x, m_Pos.y); //batch.draw(anim.getKeyFrame(m_StateTime, true), m_Pos.x, m_Pos.y,16,16); // test pic_4 batch.draw(anim.getKeyFrame(m_StateTime, true), m_Pos.x, m_Pos.y, 0.5f, 0.5f, 32, 32, 1, 1, m_Vel.angle()); batch.end(); } public void dispose () { batch.dispose(); } }