I am developing a scene manager with AndEngine and am getting a GLMatrixStackOverflowExceptionerror:
E/AndroidRuntime(1906): FATAL EXCEPTION: GLThread 130
E/AndroidRuntime(1906): org.andengine.opengl.util.GLMatrixStack$GLMatrixStackOverflowException
E/AndroidRuntime(1906): at org.andengine.opengl.util.GLMatrixStack.glPushMatrix(GLMatrixStack.java:94)
E/AndroidRuntime(1906): at org.andengine.opengl.util.GLState.pushProjectionGLMatrix(GLState.java:574)
E/AndroidRuntime(1906): at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:243)
E/AndroidRuntime(1906): at org.andengine.entity.Entity.onDraw(Entity.java:1348)
E/AndroidRuntime(1906): at org.andengine.entity.Entity.onManagedDraw(Entity.java:1585)
E/AndroidRuntime(1906): at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:259)
2 Classes below: GameActivity and SceneManager
The problem occurs in GameActivity on this line:
sceneManager.setCurrentScene(SceneType.MENU); // ???
What am I doing wrong?
package com.example;
import java.io.IOException;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
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.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import android.util.Log;
import com.example.scenemanager.SceneManager;
import com.example.scenemanager.SceneManager.SceneType;
/**
* Example of using a SceneManager with GLES2 AnchorCenter
*/
public class GameActivity extends BaseGameActivity {
Scene mScene;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;
SceneManager sceneManager;
Camera mCamera;
#Override
public EngineOptions onCreateEngineOptions() {
mCamera = new Camera (0,0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = (new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT) , mCamera));
return engineOptions;
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws IOException {
// mEngine is from superclass
sceneManager = new SceneManager(this, mEngine, mCamera);
sceneManager.loadSplashSceneResources();
// let the game engine know we are done loading the resources we need
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws IOException {
pOnCreateSceneCallback.onCreateSceneFinished(sceneManager.createSplashScene());
}
#Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback)
throws IOException {
mEngine.registerUpdateHandler(new TimerHandler(3.0f, new ITimerCallback() { // 3 seconds to load all stuff
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
Log.d("onPopulateScene()", "onTimePassed()");
// unregister so as only do this once
mEngine.unregisterUpdateHandler(pTimerHandler);
// done displaying splash, so go to menu
sceneManager.loadMenuSceneResources();
sceneManager.createMenuScene();
mScene.registerUpdateHandler(new IUpdateHandler() {
#Override
public void reset() { }
#Override
public void onUpdate(final float pSecondsElapsed) {
Log.d("onPopulateScene()", "onUpdate()");
sceneManager.setCurrentScene(SceneType.MENU); // ???
}
});
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
package com.example.scenemanager;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
public class SceneManager {
private SceneType currentScene;
private BaseGameActivity activity;
private Engine engine;
private Camera camera;
private Scene splashScene;
private Scene menuScene;
private Scene gameScene;
private BitmapTextureAtlas splashTextureAtlas;
private TextureRegion splashTextureRegion;
private BitmapTextureAtlas menuTextureAtlas;
private TextureRegion menuTextureRegion;
private float xPosition;
private float yPosition;
public enum SceneType
{
SPLASH,
MENU,
GAME
}
/**
* constructor
*
* #param baseGameActivity
* #param engine
* #param camera
*/
public SceneManager(BaseGameActivity baseGameActivity, Engine engine, Camera camera) {
this.activity = baseGameActivity;
this.engine = engine;
this.camera = camera;
xPosition = camera.getWidth()/2;
yPosition = camera.getHeight()/2;
}
public void loadSplashSceneResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
splashTextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),256, 256);
splashTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, this.activity, "splash.png", 0, 0);
splashTextureAtlas.load();
}
public void loadMenuSceneResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
menuTextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),256, 256);
menuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, this.activity, "menu.png", 0, 0);
menuTextureAtlas.load();
}
public void loadGameSceneResources() {
}
public Scene createSplashScene() {
// Set background color and add the splash image
splashScene = new Scene();
splashScene.setBackground(new Background(0, 1, 0)); // green
Sprite splash = new Sprite(0, 0, splashTextureRegion, activity.getVertexBufferObjectManager());
splash.setPosition(xPosition, yPosition);
splashScene.attachChild(splash);
return splashScene;
}
public Scene createMenuScene() {
menuScene = new Scene();
menuScene.setBackground(new Background(0,0,0));
Sprite sprite = new Sprite (0, 0, menuTextureRegion, engine .getVertexBufferObjectManager());
sprite.setPosition(xPosition, yPosition);
menuScene.attachChild(menuScene);
return menuScene;
}
public void createGameScene() {
//Create the Main Game Scene and set background colour to blue
gameScene = new Scene();
gameScene.setBackground(new Background(0, 0, 1));
}
public SceneType getCurrentScene() {
return currentScene;
}
public void setCurrentScene(SceneType scene) {
if (scene != currentScene) {
currentScene = scene;
switch (scene)
{
case SPLASH:
break;
case MENU:
engine.setScene(menuScene);
break;
case GAME:
engine.setScene(gameScene);
break;
default:
break;
}
}
}
}
Related
I use libgdx have one player which moves in x direction from left to right. Now I want the camera to follow it (like in Flappy Bird for example).
What happen is that the player go out off screen when it reaches the right border of screen and the camera don't follow him.
I have tried following options but none of them worked:
camera.position.set(player.getX(), camera.position.y, 0);
camera.position.set(player.getX(), 0, 0);
Vector3 vector3= camera.unproject(new Vector3(player.getX(), 0f, 0f));
player.setX(vector3.x);
I know that this question already exists on SO but none answer works in this case. Maybe I miss something important that i don't know.
The code:
Game.java class
public class Game extends com.badlogic.gdx.Game implements ApplicationListener {
public static Vector2 VIEWPORT = new Vector2(320, 480);
public static int WIDTH;
public static int HEIGHT;
#Override
public void create() {
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
// VIEWPORT = new Vector2(WIDTH/2, HEIGHT/2);
VIEWPORT = new Vector2(WIDTH, HEIGHT);
setScreen(new GameScreen(this));
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
super.resize(width, height);
}
#Override
public void resume() {
}
#Override
public void pause() {
}
}
GameScreen.java class
import android.util.Log;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import java.util.Collections;
public class GameScreen implements Screen {
private OrthographicCamera camera;
private Player player;
private PlayerInputHandler inputHandler1, inputHandler2;
private Sound sound;
FitViewport viewp;
public static int WIDTH;
public static int HEIGHT;
int width_spacing = 0;
int height_spacing = 0;
Stage stage;
Skin skin;
public GameScreen(Game game) {
stage = new Stage(new FitViewport(Game.WIDTH, Game.HEIGHT));
camera = (OrthographicCamera) stage.getCamera();
Gdx.input.setInputProcessor(stage);
}
#Override
public void show() {
resetGame();
}
public void resetGame() {
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
width_spacing = Game.WIDTH / 24;
height_spacing = Game.HEIGHT / 14;
stage.clear();
skin = new Skin(Gdx.files.internal("data2/uiskin.json"));
prepareInputHandlers();
prepare_stage();
}
public void addPlayer() {
Texture texture = new Texture("player.png");
player = new Player(texture);
player.setPosition(Game.WIDTH / 2, Game.HEIGHT * 2 / 3);
stage.addActor(player);
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (delta > 1 / 60f) {
player.setX(player.getX() + (4 * delta));
camera.position.set(player.getX(), camera.position.y, 0);
}
update();
stage.act(delta);
stage.draw();
}
private void update() {
camera.update();
}
private void prepareInputHandlers() {
inputHandler2 = new PlayerInputHandler(player, Input.Keys.LEFT, Input.Keys.RIGHT, Input.Keys.UP, Input.Keys.DOWN);
}
#Override
public void dispose() {
sound.dispose();
player.getTexture().dispose();
}
public void prepare_stage() {
addPlayer();
player.setWidth(64);
player.setHeight(64);
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
}
Player.java class
import com.badlogic.gdx.graphics.Texture;
public class Player extends com.badlogic.gdx.scenes.scene2d.ui.Image{
private Texture texture;
public Player(Texture texture) {
super(texture);
}
public Texture getTexture() {
return texture;
}
#Override
public void act(float delta) {
super.act(delta);
}
}
try to use stage's camera instead of creating your own. Change your GameScreen constructor like this:
public GameScreen(Game game)
{
stage = new Stage(new FitViewport(Game.WIDTH, Game.HEIGHT));
camera = (OrthographicCamera) stage.getCamera();
Gdx.input.setInputProcessor(stage);
}
then in the render method set the camera position just like
camera.position.set(player.getX(), camera.position.y, 0);
before camera.update() call
There is also something strange in your code - why you have camera.position.set() and setting player x in this condition:
if (delta > 1 / 60f) //??
{
player.setX(player.getX() + (4 * delta));
camera.position.set(player.getX(), camera.position.y, 0);
}
I'm pretty sure you don't need this - but even if you need the
camera.position.set(player.getX(), camera.position.y, 0);
should be out of if statement because what is happening now is that even if you are changing your player position (using keybord through PlayerInputHandler object) you **don't update camera position. Try to remove if statement or at least do something like
public void render(float delta)
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (delta > 1 / 60f)
{
player.setX(player.getX() + (100 * delta));
}
camera.position.set(player.getX(), camera.position.y, 0);
update();
stage.act(delta);
stage.draw();
}
last thing is that if your stage is empty (excluding player) when camera will start to follow player - you will see as player not moving at all :) Add someting more to stage
I am trying out AndEngine and cannot work out why my splashscreen is not centered in the device - see the image below.
I have set a default size for the device (800, 480) and placed the splash at camera.getWidth() / 2. It seems like the corner of my splash hits the center pretty spot on but I want the center of the splash to be centered - makes sense?
MainActivity:
package com.example.caspe.getmeout;
import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.scene.Scene;
import java.io.IOException;
public class MainActivity extends BaseGameActivity {
private ResourcesManager resourcesManager;
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera camera;
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(800, 480), this.camera);
engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engineOptions;
}
#Override
public Engine onCreateEngine(EngineOptions pEngineOptions) {
return new LimitedFPSEngine(pEngineOptions, 60);
}
#Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
resourcesManager = ResourcesManager.getInstance();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException {
SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
}
#Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {
mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() {
public void onTimePassed(final TimerHandler pTimerHandler) {
mEngine.unregisterUpdateHandler(pTimerHandler);
// load menu resources, create menu scene
// set menu scene using scene manager
// disposeSplashScene();
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
And the code for Splash:
package com.example.caspe.getmeout;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;
import com.example.caspe.getmeout.BaseScene;
import com.example.caspe.getmeout.SceneManager.SceneType
public class SplashScene extends BaseScene {
private Sprite splash;
#Override
public void createScene() {
splash = new Sprite(0,0, resourcesManager.splash_region, vbom) {
#Override
protected void preDraw(GLState pGLState, Camera pCamera) {
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
};
splash.setScale(1.5f);
splash.setPosition(camera.getWidth() / 2, camera.getHeight() / 2);
attachChild(splash);
}
#Override
public void onBackKeyPressed() {
}
#Override
public SceneType getSceneType() {
return SceneType.SCENE_SPLASH;
}
#Override
public void disposeScene() {
splash.detachSelf();
splash.dispose();
this.detachSelf();
this.dispose();
}
}
Please let me know if you need more code than this.
I am not directly seeking a working piece of code but more where my error is so I can use my braincells and work it out - I have just stared myself blind here.
splash.setPosition(camera.getWidth() / 2, camera.getHeight() / 2);
That is not enough. Your splash is actually centered correctly: the top left corner is in the center.
What you need is to subtract the half width and height of the splash from the position, too.
// untested code
splash.setPosition(camera.getWidth() / 2 - splash.getWidth() / 2, camera.getHeight() / 2 - splash.getHeight() / 2);
i have a source which is a sample wallpaper app, i want to import a animation xml
public WallpaperEngine(Resources r) {
image01=BitmapFactory.decodeResource(r,R.drawable.fire01);
image02=BitmapFactory.decodeResource(r,R.drawable.fire02);
bg=BitmapFactory.decodeResource(r,R.drawable.hktv);
px=1;
translateAnimation = AnimationUtils.loadAnimation(this, android.R.anim.translate_animation);
}
but there is a error at line translate_animation
cannot find symbol variable translate_animation
How can i solve this?
------update------
package com.example.android.livewallpaper;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class FireLiveWallpaper extends WallpaperService {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public Engine onCreateEngine() {
return new WallpaperEngine(getResources());
}
public class WallpaperEngine extends Engine {
private final Handler handler=new Handler();
private Bitmap image; //Image
private Bitmap image01; //Image01 for fire01.PNG
private Bitmap image02; //Image02 for fire02.PNG
private Bitmap bg;
private Paint paint = new Paint();
private int px=0; //Flag for switch
private boolean visible;
private int width;
private int height;
private int _xOffset = 0;
private int _yOffset = 0;
final Animation translateAnimation;
private final Runnable drawThread=new Runnable() {
public void run() {
drawFrame();
}
};
public WallpaperEngine(Resources r) {
image01=BitmapFactory.decodeResource(r,R.drawable.fire01);
image02=BitmapFactory.decodeResource(r,R.drawable.fire02);
bg=BitmapFactory.decodeResource(r,R.drawable.hktv);
px=1;
translateAnimation = AnimationUtils.loadAnimation(this,R.anim.translate_animation);
}
#Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
#Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(drawThread);
}
#Override
public void onSurfaceChanged(SurfaceHolder holder,
int format,int width,int height) {
super.onSurfaceChanged(holder,format,width,height);
this.width =width;
this.height=height;
drawFrame();
}
#Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}
#Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
visible=false;
handler.removeCallbacks(drawThread);
}
#Override
public void onVisibilityChanged(boolean visible) {
this.visible=visible;
if (visible) {
drawFrame();
} else {
handler.removeCallbacks(drawThread);
}
}
#Override
public void onOffsetsChanged(float xOffset,float yOffset,
float xStep,float yStep,int xPixels,int yPixels) {
_xOffset = xPixels;
_yOffset = yPixels;
drawFrame();
}
private void drawFrame() {
SurfaceHolder holder=getSurfaceHolder();
Canvas c=holder.lockCanvas();
c.drawBitmap(bg, _xOffset, _yOffset, paint);
//c.drawColor(Color.BLUE);
if (px == 1) {
image=image01;
px=2;
} else {
image=image02;
px=1 ;
}
c.drawBitmap(image, (width-image.getWidth())/2, (height-image.getHeight())/2, null);
holder.unlockCanvasAndPost(c);
handler.removeCallbacks(drawThread);
if (visible) handler.postDelayed(drawThread,100);
}
}
}
Use R.anim.translate_animation instead of android.R.anim.translate_animation to import animation for res/anim resource :
translateAnimation = AnimationUtils.loadAnimation(this,
R.anim.translate_animation);
I am making my own game. I have already made a splash screen and a main menu. I have made a button "Play" in my menu but I can't link it to my main game, while I can link it to other classes.
Here is the code of the class I can't open:
package com.mygdx.Papermadness.screens;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
public class PaperMadness extends InputAdapter implements ApplicationListener, Screen {
float timer;
public static Preferences prefs;
public static int counter; // The variable you want to save
private BitmapFont font;
//public int counter = 0;
boolean touch = false;
SpriteBatch batch;
SpriteBatch spriteBatch;
Texture spriteTexture;
Sprite sprite;
float scrollTimer = 0.0f;
Player player;
Paper paper;
Huiz huiz;
Lijn lijn;
String money = String.valueOf(counter);
ShapeRenderer sr;
public boolean kukar = false;
public void create() {
font = new BitmapFont();
Gdx.input.setInputProcessor(this);
player = new Player(new Vector2(50, 100), new Vector2(100, 100));
huiz = new Huiz(new Vector2(200, 300), new Vector2(110, 110));
// huiz = new Huiz(new Vector2(200, 300), new Vector2(110, 110));
paper = new Paper(new Vector2(Gdx.input.getX(),
Gdx.graphics.getHeight() - Gdx.input.getY()), new Vector2(50,
50));
lijn = new Lijn(new Vector2(0, 200), new Vector2(600, 2));
// sr = new ShapeRenderer();
spriteBatch = new SpriteBatch();
spriteTexture = new Texture("b9.png");
spriteTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
sprite = new Sprite(spriteTexture);
sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch = new SpriteBatch();
}
#Override
public void render(float delta) {
// bounds.set(Gdx.input.getX(),
// Gdx.graphics.getHeight() - Gdx.input.getY(),
// secondTexture.getWidth(), secondTexture.getHeight());
scrollTimer += Gdx.graphics.getDeltaTime();
if (scrollTimer > 1.0f)
scrollTimer = 0.0f;
sprite.setV(scrollTimer + 2);
sprite.setV2(scrollTimer);
player.update();
paper.update();
lijn.update();
huiz.update();
/*
* if (tree.getBounds().overlaps(ball.getBounds())) {
* System.out.println("Swendley Programinateur"); }
*
* if (tree.getBounds().overlaps(paper.getBounds())) {
* System.out.println("Souk Programinateur"); }
*/
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
batch.begin();
player.draw(batch);
huiz.draw(batch);
// paper.draw(batch);
if (Gdx.graphics.getHeight() / 1.25 < Gdx.input.getY()
&& Gdx.graphics.getWidth() / 2.7 < Gdx.input.getX()
&& Gdx.graphics.getWidth() / 1.7 > Gdx.input.getX()
&& Gdx.input.isTouched() && kukar == false && touch == false) {
kukar = true;
touch = true;
} else if (Gdx.input.isTouched() && kukar == true && touch == true) {
paper.draw(batch);
if (paper.getBounds().overlaps(huiz.getBounds())
|| paper.getBounds().overlaps(huiz.getBounds1())) {
// System.out.println("Huis Geraakt!");
touch = false;
counter++;
checkSpeed();
money = Integer.toString(counter);
// System.out.println(counter);
}
}
if (huiz.getBounds().overlaps(lijn.getBounds())
|| huiz.getBounds1().overlaps(lijn.getBounds())){
//System.out.println("Game Over");
}
font.draw(batch, money, Gdx.graphics.getWidth() / 2.06f,
Gdx.graphics.getHeight() / 1.05f);
font.setColor(Color.BLACK);
font.setScale(2, 2);
// house.draw(batch);
// house1.draw(batch);
lijn.draw(batch);
batch.end();
// sr.begin(ShapeType.Filled);
// sr.setColor(Color.YELLOW);
// sr.rect(Gdx.input.getX(), Gdx.graphics.getHeight() -
// Gdx.input.getY(),
// paper.getSize().x, paper.getSize().y);
// sr.setColor(Color.BLACK);
// sr.rect(huiz.getPosition().x, huiz.getPosition().y,
// huiz.getSize().x, huiz.getSize().y);
// sr.rect(house1.getPosition().x, house1.getPosition().y,
// house1.getSize().x, house1.getSize().y);
// sr.end();
}
public static void savePrefs(){
prefs = Gdx.app.getPreferences("game-prefs"); // The name of your prefs files
prefs.putInteger("counter", counter);
prefs.flush();
System.out.println(prefs);
}
public static void loadPrefs(){
prefs = Gdx.app.getPreferences("game-prefs");
counter = prefs.getInteger("counter",0); //Load counter, default to zero if not found
}
public void checkSpeed() {
if (counter <= 7) {
huiz.huisVelocity = 500f;
}
if (counter > 7 && counter <= 17) {
huiz.huisVelocity = 550f;
}
if (counter > 17 && counter <= 30) {
huiz.huisVelocity = 650f;
}
if (counter > 30 && counter <= 50) {
huiz.huisVelocity = 750;
}
if (counter > 50 && counter <= 75) {
huiz.huisVelocity = 900;
}
if (counter > 75 && counter <= 100) {
huiz.huisVelocity = 1000;
}
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
kukar = false;
touch = false;
return true;
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
#Override
public void render() {
}
}
Here is the menu class:
package com.mygdx.Papermadness.screens;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.mygdx.Papermadness.Papermadness;
public class MainMenu implements Screen {
private Stage stage;// done
private TextureAtlas atlas;// done
private Skin skin;// done
private Table table;// done
private TextButton buttonPlay, buttonExit;
private BitmapFont white, black;// done
private Label heading;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Table.drawDebug(stage);
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
atlas = new TextureAtlas("ui/button.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
black = new BitmapFont(Gdx.files.internal("font/black.fnt"), false);
// maakt buttons
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("button.up");
textButtonStyle.down = skin.getDrawable("button.down");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = black;
buttonExit = new TextButton("EXIT", textButtonStyle);
buttonExit.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
buttonExit.pad(15);
buttonPlay = new TextButton("PlAY", textButtonStyle);
buttonPlay.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener())
.setScreen(new PaperMadness());
}
});
buttonPlay.pad(15);
// maakt header
heading = new Label(Papermadness.TITLE, new LabelStyle(white,
Color.WHITE));
heading.setFontScale(2);
table.add(heading);
table.getCell(heading).spaceBottom(100);
table.row();
table.add(buttonPlay);
table.getCell(buttonPlay).spaceBottom(15);
table.row();
table.add(buttonExit);
// table.debug();
stage.addActor(table);
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
stage.dispose();
atlas.dispose();
skin.dispose();
white.dispose();
black.dispose();
}
}
public class MyGdxGame extends Game{
public static MenuScreen menuScreen;
public static GameScreen gameScreen;
#Override
public void create(){
menuScreen = new MenuScreen(this);
gameScreen = new GameScreen (this);
setScreen(menuScreen);
}
}
Here is MenuScreen
public class MenuScreen implements Screen{
MyGdxGame game;
public MenuScreen(MyGdxGame game){
this.game = game;
}
////////////when you want to change screen type game.setScreen(game.gameScreen)
...........
...........
}
and this is GameScreen
public class GameScreen implements Screen{
...........
...........
...........
}
this is a simple example , try to do the same in your code.
Here is a good tutorial
https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game
If it helped , let me know about it.
I have been trying to implement libgdx without a core project in a new eclipse project, but I keep getting:
The method initialize(ApplicationListener,
AndroidApplicationConfiguration) in the type AndroidApplication is not
applicable for the arguments (AndroidApplication,
AndroidApplicationConfiguration)
The code I use is quite simple at the moment:
package com.debels.androidapplication;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import android.os.Bundle;
public class MainActivity extends AndroidApplication{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new AndroidApplication(), cfg);
}
}
and
package com.debels.androidapplication;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
//import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
//import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.debels.androidapplication.screens.MainMenuScreen;
public class AndroidApplication extends Game{
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private FPSLogger fps;
#Override
public void create(){
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
//texture = new Texture(Gdx.files.internal("data/libgdx.png"));
//texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
//TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
/*sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);*/
setScreen(new MainMenuScreen(this));
fps = new FPSLogger();
}
#Override
public void dispose() {
super.dispose();
}
#Override
public void render() {
super.render();
/*fps.log();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();*/
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
}
Thats because you are sending a (gdx)AndroidApplication instance to the ini instead of a (gdx)AndroidApplicationListener (which is any class that extends Game or implements ApplicationListener).
You get all confused because you named that class AndroidApplication...
Change this:
initialize(new AndroidApplication(), cfg);
to this:
initialize(new com.debels.androidapplication.AndroidApplication(), cfg);
Or better yet, change the name of that class.
Also dont forget to copy the gdx.jar to the android project libs folder.