Basic Sprite not loading - AndEngine - android

Hello I have just started with AndEngine. I have been working with a tutorial here. I thought it was pretty straightforward but I can't seem to get it to work properly. The purpose is simply to display a simple Sprite. I did the code precisely how the tutorial instructed but when I run the game, nothing loads at all. I threw in some Log.i statements and it doesn't appear that any of the basic methods (OnCreateResources etc...) run at all. I'm not getting any errors but the nothing is running either. Does anyone know what I am doing wrong?
(I hope this isn't a stupid question) Code is below:
public class GameMain extends BaseGameActivity {
Scene scene;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_main);
Log.i("TEST", "CREATE GAME");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game_main, menu);
return true;
}
#Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions options = new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
return options;
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
// TODO Auto-generated method stub
Log.i("TEST", "LOADING GFX");
loadGFX();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
private void loadGFX() {
// TODO Auto-generated method stub
Log.i("TEST", "LOAD GFX");
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
playerTexture = new BitmapTextureAtlas(getTextureManager(), 64, 64);
playerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(playerTexture, this, "star.png", 0, 0);
playerTexture.load();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
// TODO Auto-generated method stub
Log.i("TEST", "Scene Background");
this.scene = new Scene();
this.scene.setBackground(new Background(0, 125, 58));
pOnCreateSceneCallback.onCreateSceneFinished(this.scene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
// TODO Auto-generated method stub
Sprite sPlayer = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2,
playerTextureRegion,
this.mEngine.getVertexBufferObjectManager());
sPlayer.setRotation(45.0f);
this.scene.attachChild(sPlayer);
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}

in a short look at the tutorial, i didn't see anything about overwriting the onCreate method, esp. the setContentView part could be wrong.
makes me wonder, that none of the Log.i calls appear in your log, did you set your log to a higher level than info?
let me take a deeper look, if these two hints where not right ;-)

Related

After 5 minutes of running , my libgdx game crashes and turns red

public class PlayScreen implements Screen{
Stage stage;
LabelStyle style;
BitmapFont font;
TextureAtlas backbuttonatlas;
TextButtonStyle backbuttonstyle;
TextButton backbutton;
Skin backskin;
SpriteBatch batch;
Texture pibe;
Sprite sprite;
Vector2 position;
Game game;
Texture texture;
public PlayScreen(Game game){
this.game=game;
}
#Override
public void render(float delta) {
stage=new Stage();
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isKeyPressed(Keys.W))
{
position.x+=5f;
}
if(Gdx.input.isKeyPressed(Keys.A))
{
position.y-=5f;
}
if(Gdx.input.isKeyPressed(Keys.S))
{
position.x-=5f;
}
if(Gdx.input.isKeyPressed(Keys.D))
{
position.y+=5f;
}
if(Gdx.input.isTouched()==true)
{
if(Gdx.input.getY()>Gdx.graphics.getHeight()/2)
{
position.x-=5;
}
if(Gdx.input.getY()<Gdx.graphics.getHeight()/2)
{
position.x+=5;
}
if(Gdx.input.getX()>Gdx.graphics.getWidth()/2)
{
position.y+=5;
}
if(Gdx.input.getX()<Gdx.graphics.getWidth()/2)
{
position.y-=5;
}
if(Gdx.input.isKeyPressed(Keys.BACK))
{
game.setScreen(new MainMenu(game));
}
}
font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
style = new LabelStyle(font, Color.WHITE);
backskin = new Skin();
backbuttonatlas = new TextureAtlas("buttons/backbutton.pack");
backskin.addRegions(backbuttonatlas);
backbuttonstyle = new TextButtonStyle();
backbuttonstyle.up = backskin.getDrawable("backbutton");
backbuttonstyle.over = backskin.getDrawable("backbuttonpressed");
backbuttonstyle.down = backskin.getDrawable("backbuttonpressed");
backbuttonstyle.font = font;
backbutton = new TextButton("", backbuttonstyle);
backbutton.setWidth((float) (Gdx.graphics.getHeight()/8));
backbutton.setHeight((float) (Gdx.graphics.getHeight()/8));
backbutton.setPosition((Gdx.graphics.getWidth()/20), (float) (Gdx.graphics.getHeight()-(Gdx.graphics.getWidth()/8)));
backbutton.addListener(new InputListener(){
public boolean touchDown(InputEvent event, float x, float y, int pointer, int backbutton) {
game.setScreen(new MainMenu(game));
return true;
};});
batch=new SpriteBatch();
stage.addActor(backbutton);
Gdx.input.setInputProcessor(stage);
batch.begin();
batch.draw(texture, 0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
batch.draw(pibe,(position.y/2-pibe.getWidth()/2),(position.x/2-pibe.getHeight()/2));
batch.end();
stage.act();
stage.draw();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
texture = new Texture("cielo.png");
pibe = new Texture("superman (100x52).jpg");
position = new Vector2(Gdx.graphics.getHeight(),Gdx.graphics.getWidth());
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
My LibGDX Game, collapse after a few minutes and I don't know why. I have read a little about the problem, and it says that the solution is to "dispose" the bitmapfont, or something like that. I'm new in LibGDX and I don't understand so much. A full explanation is appreciated. Sorry for my poor English.
This is the Play Class. Please, need help. Thanks.
you have to put your "creating" stuff like batch = new SpriteBatch() inside
#Override
public void create() {
)
you create billions of SpriteBatches that causes a memory issue.
Render is called each time the device is ready to update the screen. You are creating new objects each frame. Some of them must be disposed of manually, which means calling the .dispose() method of that object.
call
font.dispose();
when you are finished with the font to prevent it eating up all the memory.
Ideally you'd want to create that font outside of the render loop.
You should create your objects in the constructor, so that they are not recreated every single frame. Unless of course that is intended behavior.
Try something like this
public class PlayScreen implements Screen{
Stage stage;
LabelStyle style;
BitmapFont font;
TextureAtlas backbuttonatlas;
TextButtonStyle backbuttonstyle;
TextButton backbutton;
Skin backskin;
SpriteBatch batch;
Texture pibe;
Sprite sprite;
Vector2 position;
Game game;
Texture texture;
public PlayScreen(Game game){
this.game=game;
font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
style = new LabelStyle(font, Color.WHITE);
stage=new Stage();
backskin = new Skin();
backbuttonatlas = new TextureAtlas("buttons/backbutton.pack");
backskin.addRegions(backbuttonatlas);
backbuttonstyle = new TextButtonStyle();
backbuttonstyle.up = backskin.getDrawable("backbutton");
backbuttonstyle.over = backskin.getDrawable("backbuttonpressed");
backbuttonstyle.down = backskin.getDrawable("backbuttonpressed");
backbuttonstyle.font = font;
backbutton = new TextButton("", backbuttonstyle);
backbutton.setWidth((float) (Gdx.graphics.getHeight()/8));
backbutton.setHeight((float) (Gdx.graphics.getHeight()/8));
backbutton.setPosition((Gdx.graphics.getWidth()/20), (float) (Gdx.graphics.getHeight()-(Gdx.graphics.getWidth()/8)));
backbutton.addListener(new InputListener(){
public boolean touchDown(InputEvent event, float x, float y, int pointer, int backbutton) {
game.setScreen(new MainMenu(game));
return true;
};});
batch=new SpriteBatch();
stage.addActor(backbutton);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isKeyPressed(Keys.W))
{
position.x+=5f;
}
if(Gdx.input.isKeyPressed(Keys.A))
{
position.y-=5f;
}
if(Gdx.input.isKeyPressed(Keys.S))
{
position.x-=5f;
}
if(Gdx.input.isKeyPressed(Keys.D))
{
position.y+=5f;
}
if(Gdx.input.isTouched()==true)
{
if(Gdx.input.getY()>Gdx.graphics.getHeight()/2)
{
position.x-=5;
}
if(Gdx.input.getY()<Gdx.graphics.getHeight()/2)
{
position.x+=5;
}
if(Gdx.input.getX()>Gdx.graphics.getWidth()/2)
{
position.y+=5;
}
if(Gdx.input.getX()<Gdx.graphics.getWidth()/2)
{
position.y-=5;
}
if(Gdx.input.isKeyPressed(Keys.BACK))
{
game.setScreen(new MainMenu(game));
}
}
Gdx.input.setInputProcessor(stage);
batch.begin();
batch.draw(texture, 0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
batch.draw(pibe,(position.y/2-pibe.getWidth()/2),(position.x/2-pibe.getHeight()/2));
batch.end();
stage.act();
stage.draw();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
texture = new Texture("cielo.png");
pibe = new Texture("superman (100x52).jpg");
position = new Vector2(Gdx.graphics.getHeight(),Gdx.graphics.getWidth());
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}

why arent my ITextureRegions being displayed on my scene

I'm just learning AndEngine Anchor center and I'm working my way through the cookbook. However I'm having some trouble with loading the images to the screen (code compiles fine). Trying to load 3 rectangles(30x40, 40x30,70x50) in the oncreateResources method, code compiles, but does not display images.Thanks
public class GameActivity extends BaseGameActivity {
// The following constants will be used to define the width and height
// of our game's camera view
private static final int WIDTH = 800;
private static final int HEIGHT = 480;
// Declare a Camera object for our activity
private Camera mCamera;
// Declare a Scene object for our activity
private Scene mScene;
#Override
public EngineOptions onCreateEngineOptions() {
// Define our mCamera object
mCamera = new Camera(0, 0, WIDTH, HEIGHT);
// Declare & Define our engine options to be applied to our Engine
// object
EngineOptions engineOptions = new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
mCamera);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engineOptions;
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback) {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(mEngine.getTextureManager(), 120, 120);
ITextureRegion mRetangleOneTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "rectangle_one.png", 10, 10);
ITextureRegion mRetangleTwoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "rectangle_two.png", 50, 10);
ITextureRegion mRetangleThreeTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "rectangle_three.png", 10, 60);
mBitmapTextureAtlas.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
mScene = new Scene();
pOnCreateSceneCallback.onCreateSceneFinished(mScene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) {
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
I haven't used the center anchor branch yet, but your problem seems to be that you need to create sprites with the loaded ITextures. Then, you need to attach those sprites in the scene.

Andengine Troubles, Nothing showing on screen? SimpleBaseGameActivity

I have this code:
public class GameActivity extends SimpleBaseGameActivity{
static final int CAMERA_WIDTH = 800;
static final int CAMERA_HEIGHT = 480;
public Font mFont;
public Camera mCamera;
public Scene mCurrentScene;
public static BaseActivity instance;
#Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
instance = this;
mCamera = new Camera(0,0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}
#Override
protected void onCreateResources() {
// TODO Auto-generated method stub
mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
mFont.load();
}
#Override
protected Scene onCreateScene() {
// TODO Auto-generated method stub
mEngine.registerUpdateHandler(new FPSLogger());
mCurrentScene = new Scene();
mCurrentScene.setBackground(new Background(0.09804f, 0.7274f, 0.8f));
return mCurrentScene;
}
}
My Whole goal is to just get something on the screen, just a simple bitmap. Ive followed a bunch of tutorials on andengine on youtube and google, but for some reason when i start the basegameactivity it doesnt display anything at all just a blank white screen. I also downloaded an example from github and copied and pasted the code and the example gave me the same thing just a blank white screen. Can anyone help me? Id appreciate it :]
I think you are using GLES2 andengine branch and may be your phone is not supporting GLES2.

scene2D in libgdx -Animation not working

I want an animated splash screen like fade in fade out for which I am doing in resize method as follows
public class SplashScreen extends GamePlayScreen {
#Override
public void resize(int width, int height) {
super.resize(width, height);
stage.clear();
Drawable splashDrawable = new TextureRegionDrawable(region);
splashImage = new Image(splashDrawable, Scaling.stretch);
splashImage.setFillParent(true);
splashImage.getColor().a = 0f;
splashImage.addAction(Actions.sequence(Actions.fadeIn(0.75f),
Actions.delay(1.75f), Actions.fadeOut(0.75f),
new Action() {
#Override
public boolean act(float delta) {
// the last action will move to the next screen
System.out.println("moving to next screen");
splashGameObj.setScreen(new GamePlayScreen(
splashGameObj));
return true;
}
}));
stage.addActor(splashImage);
}
}
Try changing your new Action() to new RunnableAction(){ public void run(){.....
Herer are more actions also some other explanation how they work. refare to an other question.
->Actions
Also take a look at:
screen2D, libgdx wiki about actions
Declare this variable
private Stage stage;
#Override
public void render(float delta) {
// TODO Auto-generated method stub
update();
draw(delta);
}
private void draw(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw()
}
#Override
public void resize(int width, int height) {
stage.setViewport( width, height, true );
}
#Override
public void show() {
// TODO Auto-generated method stub
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Image splashImage = new Image(region);
splashImage.addAction( Actions.sequence( Actions.fadeOut( 0.0001f ), Actions.fadeIn( 5f ),
Actions.run(onSplashFinishedRunnable) ) );
stage.addActor(splashImage);
}
Runnable onSplashFinishedRunnable = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
splashGameObj.setScreen(new GamePlayScreen(splashGameObj));
}
};

AndEngine Text.setText not working

I created a helper class that will return Text object based on the custom font and text I give.
Here is code :
public class CustomFontTextHelper {
private Font font;
private ITexture fontTexture;
private Text text;
public Text getTextWithFont(String myText,String fontPath,BaseGameActivity activity,float x,float y,int fontsize,int color){
fontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
FontFactory.setAssetBasePath("fonts/");
this.font = FontFactory.createFromAsset(activity.getFontManager(), fontTexture, activity.getAssets(), fontPath, fontsize, true, color);
this.font.load();
text = new Text(x, y, this.font, myText, activity.getVertexBufferObjectManager());
return text;
}
}
Using this helper class I create a text and attach to my scene. Its working perfectly. But when I try to change the text using text.setText method it crashes.
Below is the code I use to change the text.
public class StartingScreen extends BaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 720;
// ===========================================================
// Fields
// ===========================================================
public Text loadingPercentage;
public float percentLoaded;
public CustomFontTextHelper fontHelper;
#Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0, 0, 0));
fontHelper = new CustomFontTextHelper();
percentLoaded = 0;
loadingPercentage = fontHelper.getTextWithFont("0%", "MyriadPro-Cond.ttf", this, CAMERA_WIDTH-120, 100, 64, Color.BLACK);
scene.attachChild(loadingPercentage);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
pOnPopulateSceneCallback.onPopulateSceneFinished();
loadingPercentage.setText("5%");
new Thread(new Runnable(){
#Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr+=5) {
StartingScreen.this.runOnUpdateThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
StartingScreen.this.loadingPercentage.setText(Float.toString(StartingScreen.this.percentLoaded) + "%");
}
});
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
}
}
// TODO Auto-generated method stub
}
}).start();
// TODO Auto-generated method stub
}
Please help me with the code.
As far as I can see you are using AndEngine GLES2, so as Matim already said you can change the Text instance. The problem is that when you instantiate the Text object the first time you either need to include some text or better you should tell the object how many letters it has to hold (max).
so instead of
text = new Text(x, y, this.font, myText, activity.getVertexBufferObjectManager());
you do
int textLength = 4;
text = new Text(x, y, this.font, myText, textLength, activity.getVertexBufferObjectManager());
I just set the length to 4 since you use it as a percentage, which can only be between 0% and 100% so maximal 4 letters. Of course you can set the text length as you want, if you set it to 1000 and only put "hello" in your text, its fine (if you can, make them small to save memory) .
You just can't reset the text size afterwards – meaning, once you created a Text object with length 5 you can't put 6 letters in it, nor can you call something like setSize(6).. that's not working.
I hope this helps!
Christoph
As pointed out by game droid, the problem is with the text length. However I would like to answer this question and the next one at the same time. The next question is, how do I avoid the delay when changing the text from one value to another, and the solution is to initialize the text with all of the characters you plan to use, like so:
Text text = new Text(x, y, this.font, "1234567890%",activity.getVertexBufferObjectManager());
text.setText("0%");
This form of the constructor sets the maximum text length to whatever the length of the initial string is, so in this case 11.
Text is only set your text value, you can't change that text,
if you want change your text then use ChangeableText.
ChangeableText changeableText = new ChangeableText(x, y, Font, "Your Text");
yourscene.attachChild(changeableText);
changeableText.setText("Your New Text");

Categories

Resources