user1406716
user1406716

Reputation: 9705

Cannot see image on Screen (Using #AndEngine)

I am trying to create a simple object (an image, player.png) moving around. My code is below, i see the correct background color, but i do not see the image.

My screen (on Galaxy S3) looks like this: How the screen actually looks

It should look like: Expected to see this

The player.png is correctly under the gfx folder under assets (assets/gfx/player.png).

The code compiles and runs on the phone without any errors.MyBringBack TUtorial Here

package com.example.game;

import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
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 com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class GameActivity extends BaseGameActivity {

Scene scene;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;
PhysicsWorld physicsWorld; // define in onCreateScene(...)

@Override
public EngineOptions onCreateEngineOptions() {
    // TODO Auto-generated method stub

    Camera myCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions option = new EngineOptions(true,
            ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
                    CAMERA_WIDTH, CAMERA_HEIGHT), myCamera);
    return option;
}

@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback)
        throws Exception {
    // TODO Auto-generated method stub

    loadGfx();

    pOnCreateResourcesCallback.onCreateResourcesFinished();

}

private void loadGfx() {
    // TODO Auto-generated method stub
    // We will load all our graphics here

    // allows us to refer to the gfx folder under assets as our base
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    // must use 2^n value for width and height
    playerTexture = new BitmapTextureAtlas(getTextureManager(), 64, 64);

    // 0,0 refers to the top left corner of the screen
    playerTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(playerTexture, this, "player.png", 0, 0);

    // load these on the screen
    playerTexture.load();

}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
        throws Exception {
    // TODO Auto-generated method stub
    this.scene = new Scene();

    this.scene.setBackground(new Background(0, 125, 58));

    //Setup how the physical world will be
    // Vector2(0 - x direction,0 - y direction) means no pulling objects in any direction
    physicsWorld = new PhysicsWorld(new Vector2(0,
            SensorManager.GRAVITY_EARTH), false);

    this.scene.registerUpdateHandler(physicsWorld);
    pOnCreateSceneCallback.onCreateSceneFinished(this.scene);

}

// Used to add characters, menu items etc to the scene
@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {


    // Create our sprite - sprite is an object, enemy, anything in a game
    // really
    Sprite sPlayer = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2,
            playerTextureRegion,
            this.mEngine.getVertexBufferObjectManager());

    sPlayer.setRotation(45.0f); // rotate by 45deg

    // Ex - after fred bounces what happens
    final FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(10.0f,
            1.0f, 0.0f);

    // Before we attach scene
    Body body = PhysicsFactory.createCircleBody(physicsWorld, sPlayer,
            BodyType.DynamicBody, PLAYER_FIX);

    sPlayer.attachChild(sPlayer);

    physicsWorld.registerPhysicsConnector(new PhysicsConnector(sPlayer, body, true, false));

    pOnPopulateSceneCallback.onPopulateSceneFinished();
}

}

Is something wrong with my camera settings?

Upvotes: 0

Views: 238

Answers (1)

Rama
Rama

Reputation: 1156

 sPlayer.attachChild(sPlayer);

change above line in your code with

 scene.attachChild(sPlayer);

Upvotes: 1

Related Questions