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.
Related
This is my second game application using LibGdx framework and this is my first time to use a framework in android. I'm trying to understand how to use it.I have already done with the design in the main menu and my problem is the functionality of each button to switch screen. Any link tutorial or sample codes are much appreciated thank you and advance
Here is my code
MainScreen.java
public class MainScreen extends ApplicationAdapter implements Screen {
//Screen Size
private static final int WIDTH= 720;
private static final int HEIGHT= 1280;
Viewport viewport;
private Camera camera;
private Stage stage;
private TextureRegion myTextureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private ImageButton playBtn;
private Texture Background,logo,exit,credits,help,option,play;
SpriteBatch spriteBatch;
Sprite sprite;
MyGdxGame game;
public MainScreen(final MyGdxGame game) {
this.game = game;
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
spriteBatch = new SpriteBatch();
camera = new PerspectiveCamera();
viewport = new ScreenViewport(camera);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Background = new Texture(Gdx.files.internal("backgroundimage.png")); //background image
//Menu Buttons
exit = new Texture(Gdx.files.internal("menu/exit.png"));
logo = new Texture(Gdx.files.internal("menu/logo.png"));
option = new Texture(Gdx.files.internal("menu/options.png"));
help = new Texture(Gdx.files.internal("menu/help.png"));
credits = new Texture(Gdx.files.internal("menu/credits.png"));
play = new Texture(Gdx.files.internal("menu/play.png"));
sprite = new Sprite(play);
sprite.setPosition(130,360);
sprite.setSize(0,0);
myTextureRegion = new TextureRegion(play);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
playBtn = new ImageButton(myTexRegionDrawable); //Set the button up
stage.addActor(playBtn); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
playBtn.addListener(new InputListener(){
#Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Button Pressed");
game.setScreen(new IngamedayOne());
}
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(playBtn);
}
#Override
public void create(){
}
#Override
public void show() {
Gdx.input.setInputProcessor(stage); //Start taking input from the ui
}
#Override
public void render(float delta) {
}
#Override
public void resize(int width, int height) {
// viewport.update(width, height);
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
spriteBatch.begin();
spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, WIDTH, HEIGHT);
spriteBatch.draw(Background,0,0);
spriteBatch.draw(exit,590,1140);
spriteBatch.draw(logo,125,600);
spriteBatch.draw(play,130,360);
spriteBatch.draw(option,170,100);
spriteBatch.draw(help,350,100);
spriteBatch.draw(credits,470,100);
stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic
spriteBatch.end();
//stage.draw(); //Draw the ui
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
Background.dispose();
logo.dispose();
exit.dispose();
}
}
My Second Screen
public class IngamedayOne extends ApplicationAdapter implements Screen {
// Constant rows and columns of the sprite sheet
private static final int FRAME_COLS = 5, FRAME_ROWS = 1;
private boolean peripheralAvailable;
// Objects used
Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
private Texture cat ,left_paw,right_paw,progressbar_background,progressbar_knob,pause,meter;
Texture carpet,desk,plants,square_carpet,shoes;
SpriteBatch spriteBatch;
Sprite sprite;
private Texture Background;
Viewport viewport;
private Camera camera;
private Stage stage;
// A variable for tracking elapsed time for the animation
float stateTime;
//Screen Size
private static final int WIDTH= 720;
private static final int HEIGHT= 1280;
public IngamedayOne() {
}
public IngamedayOne(MyGdxGame game) {
}
#Override
public void create() {
stage = new Stage();
spriteBatch = new SpriteBatch();
// Load the sprite sheet as a texture
cat = new Texture(Gdx.files.internal("cat.png"));
sprite = new Sprite(cat);
sprite.setPosition(0,0);
sprite.setSize(0,0);
peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
camera = new PerspectiveCamera();
viewport = new ScreenViewport(camera);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//Display Items
carpet = new Texture("equip/carpet2.png");
desk = new Texture("equip/Desk.png");
square_carpet = new Texture("equip/Carpet.png");
plants = new Texture("equip/Plants.png");
shoes = new Texture("equip/Shoes.png");
// Progressbar
progressbar_background = new Texture("progression_map.png");
progressbar_knob = new Texture("cat_head.png");
//pause
pause = new Texture("pause.png");
meter = new Texture("meter.png");
//background
Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
//button controller
left_paw = new Texture(Gdx.files.internal("left_paw.png"));
sprite = new Sprite(left_paw);
right_paw = new Texture(Gdx.files.internal("right_paw.png"));
sprite = new Sprite(right_paw);
// Use the split utility method to create a 2D array of TextureRegions. This is
// possible because this sprite sheet contains frames of equal size and they are
// all aligned.
TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth() / FRAME_COLS, cat.getHeight()/ FRAME_ROWS);
// Place the regions into a 1D array in the correct order, starting from the top
// left, going across first. The Animation constructor requires a 1D array.
TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
// Initialize the Animation with the frame interval and array of frames
walkAnimation = new Animation<TextureRegion>(0.200f, walkFrames);
// Instantiate a SpriteBatch for drawing and reset the elapsed animation
// time to 0
spriteBatch = new SpriteBatch();
stateTime = 0f;
}
#Override
public void show() {
}
#Override
public void render(float delta) {
}
#Override
public void resize(int width, int height) {
viewport.update(width, height);
}
#Override
public void render() {
// clear previous frame
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
// Get current frame of animation for the current stateTime
TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
spriteBatch.begin();
spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, WIDTH, HEIGHT);
spriteBatch.draw(Background,0,0);
spriteBatch.draw(square_carpet,150,2,408,800);
spriteBatch.draw(carpet,230,980,250,260);
spriteBatch.draw(desk,10,1150,160,260);
spriteBatch.draw(plants,500,700,200,260);
spriteBatch.draw(shoes,300,500,110,110);
spriteBatch.draw(meter,190,990);
spriteBatch.draw(progressbar_background,20,1170);
spriteBatch.draw(progressbar_knob,18,1170);
spriteBatch.draw(pause,580,1150);
spriteBatch.draw(left_paw,10,25);
spriteBatch.draw(right_paw,517,25);
spriteBatch.draw(currentFrame, 260, 120 ); // Draw current frame at (50, 50)
spriteBatch.end();
stage.act(); //acting a stage to calculate positions of actors etc
stage.draw(); //drawing it to render all
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() { // SpriteBatches and Textures must always be disposed
spriteBatch.dispose();
cat.dispose();
left_paw.dispose();
right_paw.dispose();
stage.dispose();
Background.dispose();
progressbar_background.dispose();
progressbar_knob.dispose();
}
}
Your game should possess only one ApplicationListener(called when your application created).
Let's suppose that is your MainScreen which extends ApplicationAdapter(Adapter class of ApplicationListener interface), then not extend your IngamedayOne class again by ApplicationAdapter. You only need to implement with Screen interface.
public class IngamedayOne implements Screen {
}
create() method comes from ApplicationListener interface so migrate your code that in inside create() method to show().
Take a look of this wiki for more clearance.
https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game
You should create actors to add to your stage (in this case buttons, like an ImageButton)
and then use the setScreen() function to change screens on button presses addClickListener()
You can read more about it here:
https://github.com/libgdx/libgdx/wiki/Scene2d
I am having some issues with some of the boiler plate code on Android Studio. I was running through the debugger and discovered that mEngine is not initialized and is getting accessed as a null value. I was following this tutorial:
http://www.makethegame.net/android-andengine/how-to-setup-andengine-with-android-studio/
I am not sure how to fix this error since I do not know how to initialize mEngine. Any tips?
public class gameplay extends LayoutGameActivity {
//AndEngine variables//
private Camera camera;
private int CAMERA_WIDTH = 320;
private int CAMERA_HEIGHT = 480;
public Scene currentScene;
//End of AndEngine variables//
//AndEngine boiler plate code//
#Override
protected int getLayoutID() {
return R.layout.activity_gameplay;
}
#Override
protected int getRenderSurfaceViewID() {
return R.id.gameView;
}
#Override
public EngineOptions onCreateEngineOptions() {
this.camera = new Camera(0,0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), camera);
engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engineOptions;
}
#Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException {
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
//ERROR IN THIS METHOD BELOW
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException {
mEngine.registerUpdateHandler(new FPSLogger());
//Attempt to read from field 'org.andengine.engine.Engine org.andengine.opengl.view.EngineRenderer.mEngine' on a null object reference
currentScene = new Scene();
currentScene.setBackground(new org.andengine.entity.scene.background.Background(0.09804f, 0.7274f, 0.8f));
}
#Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
//End of AndEngine boiler plate code//
}
Attached below is my error log:
Is it necessary for you to extend the LayoutGameActivity.
Try BaseGameActivity instead.
Try running your error line
mEngine.registerUpdateHandler(new FPSLogger()); in OnPopulateScene.
I have made my layout in scene 2d and implemented it. But the sizing is the issue. It comes in different alignment for my android phone and on desktop.
Although I think that the solution is to use viewport but I don't know how to correctly implement it.
The way I am implementing it gives me a white screen
#Override
public void show() {
width=GameScreen.getGameWidth();
height=GameScreen.getGameHeight();
camera = new OrthographicCamera();
camera.setToOrtho(true,width ,height);
stage = new Stage(new FillViewport(width, height,camera));
parameter.size = 40;
parameter.color=Color.WHITE;
parameter.flip=false;
font12 = generator.generateFont(parameter); // font size 12 pixels
generator.dispose();
TextureAtlas atlas= new TextureAtlas(Gdx.files.internal("data/uiAtlas.atlas"));
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
skin2= new Skin(Gdx.files.internal("data/achievementSkin.json"),atlas);
Gdx.input.setInputProcessor(stage);
mainTable = new Table();
levelTable = new Table();
stage.addActor(mainTable);
mainTable.setFillParent(true);
scrollPane = new ScrollPane(levelTable, skin);
scrollPane.setFlickScroll(true);
scrollPane.setFadeScrollBars(true);
labelStyle=new LabelStyle();
labelStyle.font=font12;
makeLevelMenu();
}
public void makeLevelMenu() {
for (int i = 0; i < 10; i++) {
columnTable.add(new Table());
// Image
button.add(new ImageButton(skin2, "playButton"));
columnTable.get(i).add(button.get(i)).colspan(2).padBottom(100)
.padLeft(100).padRight(100).padTop(100);
columnTable.get(i).row();
// Enemy Label
enemyLabel.add(new Label("Enemy " + (i + 1), labelStyle));
columnTable.get(i).add(enemyLabel.get(i)).left().padBottom(300)
.padTop(100);
columnTable.get(i).row();
// Battle Style
battleStyle.add(new Label("Battle Style " + (i + 1), labelStyle));
columnTable.get(i).add(battleStyle.get(i)).left().padBottom(100);
levelTable.add(columnTable.get(i)).expand();
}
levelTable.debug();
// achievementsTable.setFillParent(true);
for (int i = 0; i < button.size(); i++) {
final int k = i;
button.get(k).addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
System.out.println("Level " + (k + 1) + "touchDown");
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
}
});
}
scrollPane.setScrollingDisabled(false, false);
mainTable.add(scrollPane).expand().fill();
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl20.glClearColor(1, 1, 1, 1);
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
LibGDX indeed offers Vieworts to help you with different resolutions. Personally I mostly use FitViewport, it keep the aspect ratio and "black" boxes the rest. There are a number of other viewports such as StretchViewport which does exactly what you expect.
You can easily test what fits your game if you setup everything correctly. The following code is straight from www.gamesfromscratch.com. And they describe the behavior of the less obvious viewports.
public class ViewportDemo extends ApplicationAdapter {
SpriteBatch batch;
Sprite aspectRatios;
OrthographicCamera camera;
Viewport viewport;
#Override
public void create () {
batch = new SpriteBatch();
aspectRatios = new Sprite(new Texture(Gdx.files.internal("Aspect.jpg")));
aspectRatios.setPosition(0,0);
aspectRatios.setSize(100,100);
camera = new OrthographicCamera();
viewport = new FillViewport(100,100,camera);
viewport.apply();
camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0);
}
#Override
public void render () {
camera.update();
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
aspectRatios.draw(batch);
batch.end();
}
#Override
public void dispose(){
aspectRatios.getTexture().dispose();
}
#Override
public void resize(int width, int height){
viewport.update(width,height);
camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0);
}
You could do this yourself. Create any image, stick it to the stage and play with the different viewports and screen size. Also forget working with pixels since a pixel is a hardware thing.
Edit
The above implementation from www.gamesfromscratch.com does not show how to use the implementation in a stage. However this is as simple as it gets.
stage = new Stage(viewport, batch);
Now the stage will use the viewport and batch you provided. Now simply add stuff to the stage and draw it like you would do normally.
The following is my code that creates a very basic menu screen using a stage and a fitviewport. You do need your own atlas and skin to make this work, check my answer here on SO to learn more about that.
public class MenuScreen implements Screen{
private SpriteBatch batch;
protected Stage stage;
private Viewport viewport;
private OrthographicCamera camera;
private TextureAtlas atlas;
protected Skin skin;
public MenuScreen()
{
atlas = new TextureAtlas("atlas.txt");
skin = new Skin(Gdx.files.internal("skin.json"), atlas);
batch = new SpriteBatch();
camera = new OrthographicCamera();
viewport = new FitViewport(RealmOfConflict.WorldWidth, RealmOfConflict.WorldHeight, camera);
viewport.apply();
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
stage = new Stage(viewport, batch);
//Stage should controll input:
Gdx.input.setInputProcessor(stage);
}
#Override
public void show() {
//Create Table
Table mainTable = new Table();
//Set table to fill stage
mainTable.setFillParent(true);
//Set alignment of contents in the table.
mainTable.top();
//Create buttons
TextButton playButton = new TextButton("Play", skin);
TextButton optionsButton = new TextButton("Options", skin);
TextButton exitButton = new TextButton("Exit", skin);
//Add listeners to buttons
playButton.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
((Game)Gdx.app.getApplicationListener()).setScreen(new PlayScreen());
}
});
exitButton.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
//Add buttons to table
mainTable.add(playButton);
mainTable.row();
mainTable.add(optionsButton);
mainTable.row();
mainTable.add(exitButton);
//Add table to stage
stage.addActor(mainTable);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(.1f, .12f, .16f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
#Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
skin.dispose();
atlas.dispose();
}
}
The problem is in the viewport's camera. You are creating your own camera and don't setting its position and not updateing it.
If there is no reason to create your own camera just lean on default stage camera. Instead of:
camera = new OrthographicCamera();
camera.setToOrtho(true,width ,height);
stage = new Stage(new FillViewport(width, height,camera));
use
//no camera defining
stage = new Stage(new FillViewport(width, height));
and then normal flow of your app
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.
I'm a noob to Android development and i want to develop a game you using AndEngine and use SherlockActionBar for navigation. The problem is that AndEngine requires me to extend SimpleBaseGameActivity and SherlockActionBar needs to extend SherlockActivity. I am attempting to use a nested class approach, but i can't figure out how to make AndEngine initialize during OnCreate. Any help is greatly appreciated.
--EDIT--
I've tinkered with the static attachment demo in the ActionBarSherlock examples, but still can't figure out how to make this work, since there is no OnCreate method in the existing AndEngine demo code.
public class WABActivity extends SimpleBaseGameActivity implements IOnMenuItemClickListener, OnCreateOptionsMenuListener {
ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
protected static final int MENU_RESET = 0;
protected static final int MENU_QUIT = MENU_RESET + 1;
private Camera mCamera;
protected Scene mMainScene;
private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mFaceTextureRegion;
private Font mFont;
protected MenuScene mMenuScene;
#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
public void onCreateResources() {
FontFactory.setAssetBasePath("font/");
final ITexture fontTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
this.mFont = FontFactory.createFromAsset(this.getFontManager(), fontTexture, this.getAssets(), "Plok.ttf", 48, true, android.graphics.Color.WHITE);
this.mFont.load();
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box_menu.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
#Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mMenuScene = this.createMenuScene();
/* Just a simple scene with an animated face flying around. */
this.mMainScene = new Scene();
this.mMainScene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final Sprite face = new Sprite(0, 0, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
face.registerEntityModifier(new MoveModifier(30, 0, CAMERA_WIDTH - face.getWidth(), 0, CAMERA_HEIGHT - face.getHeight()));
this.mMainScene.attachChild(face);
setTheme(R.style.Theme_Sherlock); //<--Not sure if this goes here
mSherlock.setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW); //<--Not sure if this goes here
mSherlock.setContentView(???); //<--How do I set ContentView? Do I need to set contentview?
return this.mMainScene;
}
#Override
public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
if(pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
if(this.mMainScene.hasChildScene()) {
/* Remove the menu and reset it. */
this.mMenuScene.back();
} else {
/* Attach the menu. */
this.mMainScene.setChildScene(this.mMenuScene, false, true, true);
}
return true;
} else {
return super.onKeyDown(pKeyCode, pEvent);
}
}
#Override
public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
switch(pMenuItem.getID()) {
case MENU_RESET:
/* Restart the animation. */
this.mMainScene.reset();
/* Remove the menu and reset it. */
this.mMainScene.clearChildScene();
this.mMenuScene.reset();
return true;
case MENU_QUIT:
/* End Activity. */
this.finish();
return true;
default:
return false;
}
}
protected MenuScene createMenuScene() {
final MenuScene menuScene = new MenuScene(this.mCamera);
final IMenuItem resetMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_RESET, this.mFont, "RESET", this.getVertexBufferObjectManager()), new Color(1,0,0), new Color(0,0,0));
resetMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
menuScene.addMenuItem(resetMenuItem);
final IMenuItem quitMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_QUIT, this.mFont, "QUIT", this.getVertexBufferObjectManager()), new Color(1,0,0), new Color(0,0,0));
quitMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
menuScene.addMenuItem(quitMenuItem);
menuScene.buildAnimations();
menuScene.setBackgroundEnabled(false);
menuScene.setOnMenuItemClickListener(this);
return menuScene;
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
return mSherlock.dispatchCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
sub = menu.addSubMenu("Options");
sub.add(0, 1, 0, "Settings");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
#Override //<-- Giving Error: must override or implement a supertype method
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home || item.getItemId() == 0) {
return false;
}
if(item.getItemId()==1){
//SETTINGS SCREEN//
sub.removeItem(1);
sub.removeItem(2);
sub.removeItem(3);
sub.removeItem(4);
sub.removeItem(5);
sub.add(0, 5, 0, "Home");
sub.add(0, 2, 0, "High Scores");
sub.add(0, 3, 0, "Help");
sub.add(0, 4, 0, "About");
//mMode = startActionMode(new AnActionModeOfEpicProportions());
}
}
}
Look at the static attachment example included in the ActionBarSherlock demo code.
Static attachment exists exactly for this purpose.