Ich habe eine Klasse erstellt und möchte Blöcke an bestimmten Koordinaten erstellen.
Nur habe ich das Problem, dass die Sprites nicht erstellt oder nicht angezeigt werden.
Ich weiß leider gar nicht woran das liegen könnte. Ich arbeite das erste mal mit der Arraylist, vlt. habe ich da was falsch gemacht. Ich hoffe ich könnt mir helfen.
MainActivity:
package com.makeinfo.andenginetemplate;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.color.Color;
public class MainActivity extends BaseGameActivity {
//Engine
private Camera mCamera;
//Konstanten
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
//Sprite
private BitmapTextureAtlas[] mAtlas_Menu = new BitmapTextureAtlas[MAX_MENUSPRITES];
private ITextureRegion[] mTextur_Menu = new ITextureRegion[MAX_MENUSPRITES];
private BlockSystem BS = new BlockSystem(20,12);;
@Override
public EngineOptions onCreateEngineOptions() {
mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),mCamera);
}
@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
RessourceManager.prepareManager(this.getEngine(),this, mCamera, getVertexBufferObjectManager());
BS.createBlock(0,0,0,"bst_1");
BS.createBlock(5, 0, 0, "bst_1");
BS.createBlock(5,1,0,"bst_1");
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
//Test Scene
Scene mScene = new Scene();
mScene.setBackground(new Background(Color.WHITE));
pOnCreateSceneCallback.onCreateSceneFinished(mScene);
BS.loadBlocks(mScene);
}
@Override
public void onPopulateScene(Scene pScene,OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
BlockSystem Klasse:
package com.makeinfo.andenginetemplate;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;
import java.util.ArrayList;
public class BlockSystem {
//Konstanten
public static final int BLOCK_SIZE = 40;
//Mapvariablen
public static int MAP_WIDTH = 0;
public static int MAP_HEIGHT = 0;
//Blockvariable
private ArrayList<Boolean> BlockCreated = new ArrayList<Boolean>();
private ArrayList<BitmapTextureAtlas> BlockAtlas = new ArrayList<BitmapTextureAtlas>();
private ArrayList<ITextureRegion> BlockTexture = new ArrayList<ITextureRegion>();
private ArrayList<Integer> BlockType = new ArrayList<Integer>();
private ArrayList<Integer> BlockPosX = new ArrayList<Integer>();
private ArrayList<Integer> BlockPosY = new ArrayList<Integer>();
public ArrayList<Sprite> sp = new ArrayList<Sprite>();
//Engine
protected Engine engine;
protected VertexBufferObjectManager vbom;
protected BaseGameActivity activity;
protected Camera camera;
BlockSystem(int maxMapX, int maxMapY)
{
this.engine = RessourceManager.getInstance().engine;
this.vbom = RessourceManager.getInstance().vbom;
this.camera = RessourceManager.getInstance().camera;
this.activity = RessourceManager.getInstance().activity;
this.MAP_WIDTH = maxMapX;
this.MAP_HEIGHT = maxMapY;
}
public int deleteBlock(int id)
{
if(BlockCreated.get(id) == false) return 0;
BlockCreated.set(id,false);
BlockAtlas.set(id,null);
BlockTexture.set(id,null);
BlockPosX.set(id,null);
BlockPosY.set(id, null);
//sp.get(id).detachChild
return 1;
}
public int createBlock(int x, int y, int type, String textur)
{
if(x < MAP_WIDTH || y < MAP_HEIGHT) return 0;
int id = -1, i = 0;
while(BlockCreated.get(i) == false) {
id = i;
i++;
}
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/Boden/");
BlockCreated.add(true);
BlockType.add(type);
BlockAtlas.add(new BitmapTextureAtlas(engine.getTextureManager(), BLOCK_SIZE, BLOCK_SIZE, TextureOptions.DEFAULT));
BlockTexture.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(BlockAtlas.get(id), activity, textur + ".png", 0, 0));
BlockPosX.add(x);
BlockPosY.add(y);
BlockAtlas.get(id).load();
return 1;
}
public void loadBlocks(Scene mScene) {
for(int i = 0; i < BlockCreated.size(); i++)
{
if(BlockCreated.get(i) == false) continue;
BlockAtlas.get(i).load();
sp.add(new Sprite(0, 0, BlockTexture.get(i), vbom));
sp.set(i,new Sprite(0, 0, BlockTexture.get(i), vbom));
mScene.attachChild(sp.get(i));
//800 - (BLOCK_SIZE * (BlockPosX.get(i)+1)), 480 - (BLOCK_SIZE * BlockPosY.get(i)+1)
}
}
}
Ich hoffe ihr könnt mir etwas helfen und einen Fehler finden.
Ich habe schon knapp eine Stunde gesucht, habe aber leider nichts gefunden.
(Könnte auch an Schlafmangel liegen :p)