I'm using AndEngine GLES2, and I tried to load an image on my phone but what I got was a black screen
Here are my code below:
public class BaseActivity extends SimpleBaseGameActivity {
static final int CAMERA_WIDTH = 800;
static final int CAMERA_HEIGHT = 480;
public Font mFont;
public Camera mCamera;
private MainMenuScene mainMenuScene;
public Engine mEngine;
public Scene mScene;
public BuildableBitmapTextureAtlas mBitmapTextureAtlas;
public ITextureRegion mITextureRegion;
#Override
public EngineOptions onCreateEngineOptions() {
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() {
mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
mFont.load();
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
mITextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "splash1.png");
try {
mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1));
} catch (TextureAtlasBuilderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mBitmapTextureAtlas.load();
}
#Override
protected Scene onCreateScene() {
final float positionX = CAMERA_WIDTH * 0.5f;
final float positionY = CAMERA_HEIGHT * 0.5f;
Sprite mSprite = new Sprite(positionX, positionY, mITextureRegion, mEngine.getVertexBufferObjectManager());
mScene.attachChild(mSprite);
return mCurrentScene;
}
The Logcat shows that I have a nullPointerException in my onCreateResources and onCreateScene, and I have no idea what has gone wrong. What might be the problem?
One error was because you never created your Scene object, then tried to attach a sprite to it, the other was that the VertexbufferObjectManager is not that of the Engine class but of the SimpleBaseGameActivity class. onCreateScene() becomes:
#Override
protected Scene onCreateScene() {
mScene = new Scene();
final float positionX = CAMERA_WIDTH * 0.5f;
final float positionY = CAMERA_HEIGHT * 0.5f;
Sprite mSprite = new Sprite(positionX, positionY, mITextureRegion, getVertexBufferObjectManager());
mScene.attachChild(mSprite);
return mScene;
}
I also commented out some variables to make it build, though I suspect these are due to stripping down your code sample:
// private MainMenuScene mainMenuScene;
// instance = this;
Related
I'm building a live wallpaper with Andengine and I am having troubles starting it off. I cannot seem to get a parallexing background working. The end result of my code results in the assets upsidedown on the top right of the screen.
public class WallpaperService extends BaseLiveWallpaperService {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private static final String BG_FOREGROUND = "background/arctic_foreground.png";
private static final String BG_MIDGROUND = "background/arctic_midground.png";
private static final String BG_BACKGROUND = "background/arctic_land.png";
private static final int BG_TEXTURE_ATLAS_HEIGHT_WIDTH = 2048;
private static final int BG_FOREGROUND_START_HEIGHT = 0;
private static final int BG_MIDGROUND_START_HEIGHT = 600;
private static final int BG_BACKGROUND_START_HEIGHT = 1200;
private Camera camera;
private BitmapTextureAtlas backgroundTexture;
private TextureManager textureManager;
private TextureRegion mParallaxLayerFront;
private TextureRegion mParallaxLayerBack;
private TextureRegion mParallaxLayerMid;
#Override
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.camera);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
textureManager = new TextureManager();
return engineOptions;
}
#Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.backgroundTexture = new BitmapTextureAtlas(textureManager, BG_TEXTURE_ATLAS_HEIGHT_WIDTH, BG_TEXTURE_ATLAS_HEIGHT_WIDTH, TextureOptions.DEFAULT);
this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_FOREGROUND, 0, BG_FOREGROUND_START_HEIGHT);
this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_BACKGROUND, 0, BG_MIDGROUND_START_HEIGHT);
this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_MIDGROUND, 0, BG_BACKGROUND_START_HEIGHT);
this.mEngine.getTextureManager().loadTexture(this.backgroundTexture);
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
final Scene scene = new Scene();
final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 0);
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerBack, getVertexBufferObjectManager())));
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerMid, getVertexBufferObjectManager())));
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerFront, getVertexBufferObjectManager())));
scene.setBackground(autoParallaxBackground);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
#Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
Any help is greatly appreciated
All the images have 600 px of heigth? the problem seems to be that the images are collapsing with each other.
Maybe the problem is here:
autoParallaxBackground.attachParallaxEntity(
new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerBack, getVertexBufferObjectManager())));
try to use this code:
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, CAMERA_HEIGHT- this.mParallaxLayerBack.getHeight(),
this.mParallaxLayerBack, getVertexBufferObjectManager())));
i hope this will help you!
private Camera mCamera;
private Scene mMainScene;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mPlayerTextureRegion;
#Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32);
mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
mBitmapTextureAtlas.load();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mMainScene = new Scene();
this.mMainScene.setBackground(new Background(1, 1, 1));
final int iStartX = (CAMERA_WIDTH - mBitmapTextureAtlas.getWidth()) / 2;
final int iStartY = (CAMERA_HEIGHT - mBitmapTextureAtlas.getHeight()) / 2;
final Sprite oPlayer = new Sprite(iStartX, iStartY, mPlayerTextureRegion, getVertexBufferObjectManager());
this.mMainScene.attachChild(oPlayer);
}
My sprite looks like it is cut in half. Can somebody explain why?
And background color is still black and it should be white.
You have to return the scene in your onCreateScene() method.
And you said that your sprite looks like it is cut in half, may be your image size is bigger than your bitmaptextureAtlas. If so, increase your textureAtlas size.
Am new to andengine. Am trying to implement the game controller image in the screen. Am following the andengine examples. But the image is broken and appeared in the corner of the screen in a shape of triangle. Please Help. My code as follows.....................
public class ExtremeGame extends SimpleBaseGameActivity {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mFaceTextureRegion;
private BitmapTextureAtlas mOnScreenControlTexture;
private TextureRegion mOnScreenControlBaseTextureRegion;
private TextureRegion mOnScreenControlKnobTextureRegion;
private DigitalOnScreenControl mDigitalOnScreenControl;
#Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
#Override
protected void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(null, 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
this.mOnScreenControlTexture = new BitmapTextureAtlas(null, 256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
this.mEngine.getTextureManager().loadTexture(mOnScreenControlTexture);
}
#Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
final int centerX = (int) ((CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2);
final int centerY = (int) ((CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2);
final Sprite face = new Sprite(centerX, centerY, mFaceTextureRegion, getVertexBufferObjectManager());
final PhysicsHandler physicsHandler = new PhysicsHandler(face);
face.registerUpdateHandler(physicsHandler);
scene.attachChild(face);
this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
this.mDigitalOnScreenControl.refreshControlKnobPosition();
scene.setChildScene(this.mDigitalOnScreenControl);
return scene;
}
}
It all depends what andengine branch you are using, gles2 or gles2 Anchor Center.
The main, most significant change in the GLES2 Anchor Center is that the coordinate system has changed. The coordinate system in the GLES2 Anchor Center branch has its origin in the lower left, this was changed for multiple reasons.
To understand how does new coordinate system works, see article posted on my blog.
http://www.matim-dev.com/gles2-anchor-center.html
Hi am doing one app here i need to move sprite like bouncing ball with some force. using box2d how to apply force and velocity and how to move sprite like ball,i did nt get any idea. please any one suggest me...
MainActivity.class:
public class MainActivity extends SimpleBaseGameActivity{
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mFaceTextureRegion;
private BitmapTextureAtlas mOnScreenControlTexture;
private ITextureRegion mOnScreenControlBaseTextureRegion;
private ITextureRegion mOnScreenControlKnobTextureRegion;
#Override
public EngineOptions onCreateEngineOptions() {
Toast.makeText(this, "Also try tapping this AnalogOnScreenControl!", Toast.LENGTH_LONG).show();
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
#Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
#Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
final PhysicsHandler physicsHandler = new PhysicsHandler(face);
face.registerUpdateHandler(physicsHandler);
scene.attachChild(face);
return scene;
}
}
does anyone know why applyforce only works on one of my sprites? Also when I press the other sprites it also applies force to the one individual sprite. The nextTile method works fine.
enter code herepackage com.martynnorman.jude;
/**
* #author Nicolas Gramlich
* #since 11:54:51 - 03.04.2010
*/
public class MainActivity extends BaseGameActivity implements IOnAreaTouchListener {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
int centerX;
int centerY;
// ===========================================================
// Fields
// ===========================================================
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mFaceTextureRegion;
private BitmapTextureAtlas mBitmapTextureAtlas2;
private TiledTextureRegion mFaceTextureRegion2;
Random random = new Random();
Ball sprite;
int scale;
Scene scene;
private BitmapTextureAtlas mFontTexture;
private Font mFont;
Text textcenter;
int t = 1;
Ball rgSprite[] = new Ball[10];
private PhysicsWorld mPhysicsWorld;
Body body;
int isTouched;
final Vector2 gravity2 = new Vector2(0, 20);
final Vector2 gravity = new Vector2(0, 0);
private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
#Override
public Engine onLoadEngine() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}
#Override
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "gfx/ball.png", 0, 0, 2, 1);
this.mBitmapTextureAtlas2 = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas2, this,"gfx/ball2.png", 0, 0, 2, 1);
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas, mBitmapTextureAtlas2);
this.mEngine.getFontManager().loadFont(this.mFont);
this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
}
#Override
public Scene onLoadScene() {
createAETimeHandler(2);
/* final Text textcenter = new Text(100, 60, this.mFont, "touched", HorizontalAlign.CENTER);
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);*/
final Scene scene = new Scene();
scene.setOnAreaTouchListener(this);
scene.setBackground(new ColorBackground(0.6274f, 0.6274f, 0.8784f));
mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
scene.registerUpdateHandler(mPhysicsWorld);
mPhysicsWorld.setGravity(gravity);
final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
scene.attachChild(ground);
final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
scene.attachChild(left);
scene.attachChild(right);
scene.attachChild(roof);
// scene.setOnSceneTouchListener((IOnSceneTouchListener) this);
//final Ball hit = new Ball(random.nextInt(600)+1, random.nextInt(400)+1, this.mFaceTextureRegion.clone());
//final Ball hit2 = new Ball(random.nextInt(600)+1, random.nextInt(400)+1, this.mFaceTextureRegion2.clone());
//body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, hit2, BodyType.DynamicBody, FIXTURE_DEF);
//scene.attachChild(hit2);
//hit2.setScale(2);
//scene.registerTouchArea(hit2);
//this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(hit2, body, true, true));
for (int i = 0; i < rgSprite.length; i++) {
rgSprite[i] = new Ball(random.nextInt(600)+1, 200, this.mFaceTextureRegion.clone());
}
mPhysicsWorld.setGravity(gravity2);
for (Ball sprite : rgSprite) {
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, sprite, BodyType.DynamicBody, FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true));
scene.registerTouchArea(sprite);
scene.attachChild(sprite);
sprite.setScale(2);
}
//scene.attachChild(hit);
//hit.setScale(2);
//scene.registerTouchArea(hit);
return scene;
}
#Override
public void onLoadComplete() {
}
private void createAETimeHandler(float mEffectSpawnDelay)
{ TimerHandler spriteTimerHandler2;
this.getEngine().registerUpdateHandler(spriteTimerHandler2 = new TimerHandler(mEffectSpawnDelay, true, new ITimerCallback()
{
#Override
public void onTimePassed(final TimerHandler pTimerHandler)
{
/*if (isTouched == 1){
mFaceTextureRegion.setCurrentTileIndex(0);
isTouched = 2;
} */
}
}));
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,float pTouchAreaLocalY) {
if(this.mPhysicsWorld != null) {
if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
this.onOff((AnimatedSprite)pTouchArea);
}
} return false;
}
private void onOff(final AnimatedSprite ball) {
ball.nextTile();
body.applyForce(new Vector2(200,-1500), new Vector2(body.getWorldCenter()));
}
}
It is because you only have one Body-variable, and it gets overwritten by the loop. When all sprites have been created you only know the body of the last sprite.
You must apply the force to the body associated with the sprite you click.