preface

Last we’ve learned to respond to user input, and the implementation of a small rocket flight game, but we found that the badlogic. Etf has. The graphics. Texture encapsulates a picture only, does not contain a grain of other attributes, in order to achieve the complete object of the game, We usually use encapsulated relatively perfect com. Badlogic. Etf has. Graphics. G2d. Sprite class.

The Sprite elves

Compared with Texture, Sprite can better achieve a game object, it inherits the com. Badlogic. Etf has. Graphics. G2d. TextureRegion classes, and expanded the many Texture related attributes, its common construction method is as follows:

– Sprite(TextureRegion region) : Create Sprite – Sprite(Texture Texture,int srcWidth,int srcHeight) with region: Start from (0,0) and cut the size of the texture srcWidthsrcHeight to create a Sprite – Sprite(texture texture,int srcX,int srcY,int srcWidth,int srcHeight) : Create Sprite – Sprite(TextureRegion region,int srcX,int srcY,int) by intercepting the size of the texture srcWidthsrcHeight from (srcX,srcY) SrcWidth,int srcHeight) : Create a Sprite by intercepting the size of region srcWidth*srcHeight from (srcX,srcY)

Use Sprite DE

As mentioned earlier, Sprite contains many texture-related attributes, and yes, it changes texture attributes by using the following methods (commonly used):

// Set the Sprite coordinates sprite.setx (x); sprite.setY(y); sprite.setPosition(x,y); // Set Sprite size sprite.setwidth (width); sprite.setHeight(height); sprite.setSize(width,height); SetBounds (x,y,width,height); // Set the position and size of the Sprite. Sprite.setscale (scaleXY); sprite.setScale(scaleX,scaleY); // Set the rotation Angle of the Sprite to the unit degree and the counterclockwise direction to positive sprite.setRotation(dcapital); SetOrigin (originX,originY); SetFlip (isFlipX,isFirpY); // Set the Sprite texture to sprite.settexture (texture); sprite.setRegoin(regoin); Of course, you can also get Sprite properties: Java sprite.getx (); sprite.getY(); sprite.getWidth(); spritecaleY(); sprite.getTetxure.getHeight(); sprite.getBoundingRectangle(); / / get the rectangular (com) badloc. Etf has. Maths. Rectangle) Sprite. GetScaleX (); sprite.getS();Copy the code

Sprite DE draw

Since Sprite itself contains many properties, drawing simply passes SpriteBatch into the Draw () method of Sprite:

soeite.draw(spriteBatch);
Copy the code

try

Next, we’ll use the Sprite class to modify the little rocket program from the previous section:

package com.libGDX.test;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Test extends ApplicationAdapter {
    private SpriteBatch batch;
    private Texture hjTexture;
    private Sprite hj;
    @Override
    public void create(a) {
        batch=new SpriteBatch();
        hjTexture=new Texture(Gdx.files.internal("hj.png"));
        hj=new Sprite(hjTexture);
        Gdx.input.setInputProcessor(new InputAdapter() {
            @Override
            public boolean keyTyped(char character) {
                if(character=='w') {
                    // Increment y and rotation Angle
                    hj.setY(hj.getY()+1000*Gdx.graphics.getDeltaTime());
                    hj.setRotation(hj.getRotation()+10);
                }
                return false; }}); }@Override
    public void render(a) {
        Gdx.gl.glClearColor(1.1.1.1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        // The batch is passed directly
        hj.draw(batch);
        batch.end();
    }
    
    @Override\
    public void dispose(a) {
        // Since hj references hjTexture, just release hjTexture, but hj will no longer be usedhjTexture.dispose(); batch.dispose(); }}Copy the code

Running results: