So finally we are going to start coding for most important part of our game, the GameScene class.
* Create new class, called GameScene and use our BaseScene as a extension.
package com.mindew.fruitecollector;
import com.mindew.fruitecollector.BaseScene;
import com.mindew.fruitecollector.SceneManager.SceneType;
public class GameScene extends BaseScene
{
@Override
public void createScene()
{
}
@Override
public void onBackKeyPressed()
{
}
@Override
public SceneType getSceneType()
{
return SceneType.SCENE_GAME;
}
@Override
public void disposeScene()
{
}
}
Now let’s check out the graphics used,
loneback.png
stone.png
fruit.png
Go to ResourcesManager class and initialize graphics,
public ITextureRegion lonebackgound_region;
// public ITextureRegion cloud_region; //no need of this (you can still use if you wish)
public ITextureRegion stone_region;
public ITextureRegion stonearea_region;
public ITextureRegion stoneleft_region;
public ITextureRegion anchor_region;
public ITextureRegion fruite_region;
private BuildableBitmapTextureAtlas gameTextureAtlas;
private BuildableBitmapTextureAtlas stoneTextureAtlas;
Inside load game graphics put the below code,
private void loadGameGraphics()
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
gameTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
stoneTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
lonebackgound_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "loneback.png");
//cloud_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, //"cloud.png");
stone_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(stoneTextureAtlas, activity, "stone.png");
stonearea_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "bucket.png");
stoneleft_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "stoneleft.png");
fruite_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "fruite.png");
anchor_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "anchor.png");
try
{
this.gameTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
this.gameTextureAtlas.load();
this.stoneTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
this.stoneTextureAtlas.load();
}
catch (final TextureAtlasBuilderException e)
{
Debug.e(e);
}
}
We are done loading the textures and our graphics for game.
2. Now go to GameScene class and Create the background method as below,
private void createBackground()
{
attachChild(backgr = new Sprite(0, 0, resourcesManager.lonebackgound_region, vbom)
{
@Override
protected void preDraw(GLState pGLState, Camera pCamera)
{
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
});
backgr.setCullingEnabled(true);
}
3. Loading custom fonts for displaying game scores. Do this as below,
> Download .ttf font file from any of the free website (I recommend www.1001freefonts.com)
> Create a new folder inside assets folder callsed font and put .ttf file inside it.
> Go to ResourcesManager class and define font variable,
public Font scorefont;
> Create a method as below,
private void loadScoreFonts()
{
ontFactory.setAssetBasePath("font/");
Ffinal ITexture scoreFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
scorefont = FontFactory.createStrokeFromAsset(activity.getFontManager(), scoreFontTexture, activity.getAssets(), "scorefont.ttf", 30, true, Color.WHITE, 2, Color.BLACK);
scorefont.load();
}
Now our score font is ready to use in game.
3. Go back to GameScene class and Create a HUD, which will be used for displaying game controller and score text. HUD objects will always be displayed at higher level on scene.
create new private field for HUD, and method to initialize it.
private HUD gameHUD;
private void createHUD()
{
gameHUD = new HUD();
camera.setHUD(gameHUD);
}
// CREATE SCORE TEXT
private Text scoreText;
private void createHUD()
{
gameHUD = new HUD();
// CREATE SCORE TEXT
scoreText = new Text(20, 420, resourcesManager.font, "Score: 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
scoreText.setAnchorCenter(0, 0);
scoreText.setText("Score: 0");
gameHUD.attachChild(scoreText);
camera.setHUD(gameHUD);
}
create new integer field to keep our score count, and new method responsible for manipulating with score:
private int score = 0;
private int score = 0;
private void addToScore(int i)
{
score += i;
scoreText.setText("Score: " + score);
}
4. Creating physics:
We will need physics in our game, so lets create it.
create new field for our PhysicsWorld and create method to initialize it.
private PhysicsWorld physicsWorld;
private void createPhysics()
{
physicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, SensorManager.GRAVITY_EARTH), false);
registerUpdateHandler(physicsWorld);
}
Now create wall,
private void createWalls() {
// TODO Auto-generated method stub
FixtureDef WALL_FIX = PhysicsFactory.createFixtureDef(0.0f, 0.0f, 0.0f);
Rectangle ground = new Rectangle(0, 480, 800,
15, this.engine.getVertexBufferObjectManager());
ground.setColor(new Color(15, 50, 0));
PhysicsFactory.createBoxBody(physicsWorld, ground, BodyType.StaticBody,
WALL_FIX);
attachChild(ground);
ground.setCullingEnabled(true);
}
execute all creation methods inside createScene() method:
@Override
public void createScene()
{
createBackground();
createHUD();
createPhysics();
createWalls();
}
5. Displaying loading scene while loading game scene:
Loading and unloading menu texture(s):
* As mentioned we will need methods responsible for handling unloading and loading level menu texture(s), create them inside our ResorucesManager class:
public void unloadLMenuTextures()
{
lmenuTextureAtlas.unload();
}
public void loadMenuTextures()
{
lmenuTextureAtlas.load();
}
Its quite self-explanatory what it just help us to free some memory while texture is currently not needed, later we can load it again with one line (we do not have to recreate texture regions etc, we just unloaded texture from memory, we can load it again)
* create method inside SceneManager responsible for displaying loading scene, while initializing game scene and loading its resources, and unloading menu texture.
public void loadGameScene(final Engine mEngine)
{
setScene(loadingScene);
ResourcesManager.getInstance().unloadLMenuTextures();
mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback()
{
public void onTimePassed(final TimerHandler pTimerHandler)
{
mEngine.unregisterUpdateHandler(pTimerHandler);
ResourcesManager.getInstance().loadGameResources();
gameScene = new GameScene();
setScene(gameScene);
}
}));
}
6. Now to use Game scene go back to the LevelMenuScene class and uncomment the line in override method onMenuItemClicked as below,
case MENU_LONE:
SceneManager.getInstance().loadGameScene(engine);
return true;
BRAVO!!!
Above we just created the game background, HUD, defined physics, and attached Score Text.
Run the project and see if you can run the GameScene.
Enjoy!!!
No comments:
Post a Comment