Wednesday, April 19, 2017

android - Why does the Texture from AssetManager displays black rectangles only (LibGDX)?


I loaded and get a texture from assetmanager but whenever I test the app it only shows black rectangles instead of the texture. I tried finding some probable cause for this in my code but I can't find any. Here's my code:


MainGame.java


public class MainGame  extends Game {

...
...
@Override
public void create () {

EnemyAssets.load();
EnemyAssets.manager.finishLoading();
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false,screenWidth, screenHeight);
batch = new SpriteBatch();
}

@Override

public void render () {

super.render();
batch.begin();
batch.end();
}

@Override
public void dispose () {
batch.dispose();

EnemyAssets.dispose();
}

EnemyAnimation.java: (Here is where I get the asset from AssetsManager)


public class EnemyAnimation extends Actor{

MainGame app;
LevelOneScreen levelOneScreen;

private Animation enemyAnimation;

private static final int FRAME_COLS_WALK = 10;
private static final int FRAME_ROWS_WALK= 1;
public TextureRegion currentFrame;
public float enemyW;
public float enemyH;
public float enemyWidth;
public float enemyHeight;
private float stateTime = 0f;

public float enemyX;

public float enemyY;
final float delta = Gdx.graphics.getDeltaTime();

public float enemyXBound;
public float enemyYBound;

public EnemyAnimation(final MainGame app) {
this.app = app;
this.enemyX = enemyX;
this.enemyY = enemyY;

Texture enemySprite = EnemyAssets.manager.get("enemySprite.png",Texture.class);
enemySprite.setFilter(Texture.TextureFilter.MipMapLinearNearest,Texture.TextureFilter.MipMapLinearNearest);


TextureRegion[][] tmp = TextureRegion.split(enemySprite, (int) enemySprite.getWidth() / FRAME_COLS_WALK, (int) enemySprite.getHeight() / FRAME_ROWS_WALK);
TextureRegion[] enemyTexRegion = new TextureRegion[FRAME_COLS_WALK * FRAME_ROWS_WALK];
int index = 0 ;
for (int i = 0; i < FRAME_ROWS_WALK; i++) {
for (int j = 0; j < FRAME_COLS_WALK; j++) {
enemyTexRegion[index++] = tmp[i][j];

}
}

enemyAnimation = new Animation(0.044f,enemyTexRegion);
currentFrame = enemyAnimation.getKeyFrame(stateTime, true);
enemyW = currentFrame.getRegionWidth();
enemyH= currentFrame.getRegionHeight();
enemyXBound = getX();
enemyYBound = getY();
enemyWidth = app.screenWidth*0.166667f;

enemyHeight = enemyWidth *(enemyH/enemyW);

this.setSize(enemyWidth, enemyHeight);
this.setBounds(enemyXBound,enemyYBound,enemyWidth,enemyHeight);
this.isTouchable();




}


public void draw(Batch batch, float parentAlpha) {

super.draw(batch, parentAlpha);

final float delta = Gdx.graphics.getDeltaTime();


stateTime += delta;


TextureRegion currentFrame = enemyAnimation.getKeyFrame(stateTime, true);


batch.draw(currentFrame,getX(),getY(),enemyWidth,enemyHeight);

}
}

EnemyAssets.java (Here is my AssetsManager)


public class EnemyAssets {


public static final AssetManager manager = new AssetManager();

public static void load(){
manager.load("enemySprite.png",Texture.class);
}
public static void dispose(){
manager.dispose();
}
}


LevelOneScreen.java


public class LevelOneScreen implements Screen {

public MainGame app;
private Stage stage;
private Stage stageNinja;
public EnemyAnimation enemyAnimation;
private float deltaTime = Gdx.graphics.getDeltaTime();


public Array enemyAnimate;


public LevelOneScreen(final MainGame app){
this.app = app;
this.stage = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));
this.stageNinja = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));
enemyAnimate = new Array();
enemyAnimate.add(new EnemyAnimation(app));
enemyAnimation = new EnemyAnimation(app);

}


@Override
public void show() {

InputMultiplexer inputMultiplexer = new InputMultiplexer();
inputMultiplexer.addProcessor(stage);
inputMultiplexer.addProcessor(stageNinja);
Gdx.input.setInputProcessor(inputMultiplexer);



enemyAct1();

}


public void enemyAct1(){

for(int i = 1; i < 6; i++){enemyAnimate.add(new EnemyAnimation(app));}


enemyAnimate.get(1).setPosition(app.screenWidth*0.1f,app.screenHeight);

stageNinja.addActor(enemyAnimate.get(1));
enemyAnimate.get(1).addAction(moveTo(app.screenWidth*0.1f,enemyDestination,enemyTimeDrop1));


enemyAnimate.get(2).setPosition(app.screenWidth*0.1f+enemyAnimation.enemyWidth,app.screenHeight+enemyAnimation.enemyHeight);
stageNinja.addActor(enemyAnimate.get(2));
enemyAnimate.get(2).addAction(moveTo(app.screenWidth*0.1f+enemyAnimation.enemyWidth,enemyDestination,enemyTimeDrop2));



enemyAnimate.get(3).setPosition(app.screenWidth*0.1f+enemyAnimation.enemyWidth*2f,app. stageNinja.addActor(enemyAnimate.get(3)); enemyAnimate.get(3).addAction(moveTo(app.screenWidth*0.1f+enemyAnimation.enemyWidth*2f,enemyDestination,enemyTimeDrop3));

}


@Override
public void render(float delta) {



Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

update(delta);
}

public void update(float deltaTime){

stage.draw();

stageNinja.draw();

stage.act(deltaTime);
stageNinja.act(deltaTime);

app.batch.begin();
app.batch.end();

}



@Override
public void resize(int width, int height) {
app.camera.setToOrtho(false, app.screenWidth, app.screenHeight);
app.camera.update();
stage.getViewport().setScreenSize(app.screenWidth, app.screenHeight);
stageNinja.getViewport().setScreenSize(app.screenWidth, app.screenHeight);
}

@Override

public void dispose() {
stage.dispose();
stageNinja.dispose();

}
}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...