I've a Screen class that implements GestureListener, but it doesn't provide the PanStop method (which is mentioned in the LibGdx JavaDocs and Wiki)
Has this method been removed (and the docs out of date), or am I missing something?
If the former, then how does one detect and handle a touch-up or pan-stop?
Thanks.
EDIT: Implementation added....
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import java.util.Random;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.utils.Array;
import com.me.mygdxgame.actors.ActorJ;
public class JGameScreen implements Screen, GestureListener {
final MyGdxGame game;
private Stage stage;
private GameGrid gameGrid;
// Constructor & Init Screen
//
public JGameScreen(final MyGdxGame gam) {
game = gam;
stage = new Stage();
stage.setViewport(600, 600, true);
gameGrid = new GameGrid(stage, 10, 10);
}
// GestureListener Events
//
#Override
public void show() {
// TODO Auto-generated method stub
Gdx.input.setInputProcessor(new GestureDetector(this));
}
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
//chain = gameGrid.getChain();
return false;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
// TODO Auto-generated method stub
vector2 = stage.screenToStageCoordinates(vector2.set(x,y));
gameGrid.get(25).setPosition(vector2.x, vector2.y);
return true;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
// TODO Auto-generated method stub
return false;
}
#Override
public void hide() {
// TODO Auto-generated method stub
Gdx.input.setInputProcessor(null);
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( 0.9f, .04f, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
stage.setViewport(game.WIDTH, game.HEIGHT, true);
stage.getCamera().translate(-50, -50, 0);
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public boolean tap(float x, float y, int count, int button) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean longPress(float x, float y) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean zoom(float initialDistance, float distance) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2) {
// TODO Auto-generated method stub
return false;
}
}
I got the nightly build last week and I can see panStop in GestureListener interface. Odd issue.
Related
I am prety new on LibGDX,and trying to create a circle which shows the remaning time of the game.For that purpose I find this called RadialSprite,but dont know how to apply it.
what I have treid so far like this.
public class GameAreaScreen implements Screen{
Skin skin;
Stage stage;
Table table= new Table();
int a,b,c,result;
float bosluk=2;
private TextField txtFieldOperator;
private TextField txtFieldC;
private TextField txtFieldA;
private TextField txtFieldB;
private TextField txtFieldEqual;
private float time=10;
private RadialSprite rd;
private MyGdxGame game;
public GameAreaScreen(final MyGdxGame gam){
this.game=gam;
create();
}
public void create(){
Texture txturecircle = new Texture(Gdx.files.internal("circle.png"));;
TextureRegion regions= new TextureRegion(txturecircle);
rd=new RadialSprite(regions);
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
txtFieldA= new TextField("a Vallue", skin);
txtFieldB= new TextField("b Vallue", skin);
txtFieldC= new TextField("c Vallue", skin);
txtFieldOperator= new TextField("op", skin);
txtFieldEqual= new TextField("=", skin);
table.add(txtFieldA).pad(bosluk).width(30);
table.add(txtFieldOperator).pad(bosluk).width(30);
table.add(txtFieldB).width(30);
table.add(txtFieldEqual).pad(bosluk).width(30);
table.add(txtFieldC).pad(bosluk).width(30);
table.setFillParent(true);
// stage.addActor(table);
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
time -= delta;
rd.setAngle(time/10f * 360f);
Log.d("TIME",String.valueOf(time));
Log.d("angel",String.valueOf(rd.getAngle()));
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
game.batch.begin();
rd.draw(game.batch, 100,100,36);
game.batch.end();
//stage.draw();
//stage.setDebugAll(true);
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
MyGdxGame class
public class MyGdxGame extends Game{
SpriteBatch batch;
BitmapFont font;
public void create() {
batch = new SpriteBatch();
// Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new MenuScreen(this));
}
public void render() {
super.render(); // important!
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
**Iam trying to do something like this.
any help or direction wellcome
I'm at a loss as to how to implement the LIBGDX controller support. So here are the details.
Let's say I'm following the tutorial here: http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/
The Git for the tutorial at Part 4 (where i'm trying to implement a gamepad) is here: https://github.com/obviam/star-assault/tree/part4
I've made it to the final step and now it's running and I can interact with the character using the keyboard. I want to now make the character controllable via a controller either with the OUYA or through USB.
I have read the following: http://www.badlogicgames.com/wordpress/?p=2724
I have added the jar files to the main project and have Ordered and Exported.
I have now modified my GameScreen class to look as follows:
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.PovDirection;
import com.badlogic.gdx.controllers.mappings.Ouya;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Vector3;
public class GameScreen implements Screen, InputProcessor, ControllerListener {
private World world;
private WorldRenderer renderer;
private CharacterController charactercontroller;
private int width, height;
#Override
public void show() {
world = new World();
renderer = new WorldRenderer(world, false);
charactercontroller = new CharacterController(world);
Gdx.input.setInputProcessor(this);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
charactercontroller.update(delta);
renderer.render();
}
#Override
public void resize(int width, int height) {
renderer.setSize(width, height);
this.width = width;
this.height = height;
}
#Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
Gdx.input.setInputProcessor(null);
}
// * InputProcessor methods ***************************//
#Override
public boolean keyDown(int keycode) {
if (keycode == Keys.LEFT)
charactercontroller.leftPressed();
if (keycode == Keys.RIGHT)
charactercntroller.rightPressed();
if (keycode == Keys.UP)
charactercontroller.jumpPressed();
return true;
}
#Override
public boolean keyUp(int keycode) {
if (keycode == Keys.LEFT)
charactercontroller.leftReleased();
if (keycode == Keys.RIGHT)
charactercontroller.rightReleased();
if (keycode == Keys.UP)
charactercontroller.jumpReleased();
return true;
}
#Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int x, int y, int pointer, int button) {
if (!Gdx.app.getType().equals(ApplicationType.Android))
return false;
if (x < width / 2 && y > height / 2) {
charactercontroller.leftPressed();
}
if (x > width / 2 && y > height / 2) {
charactercontroller.rightPressed();
}
return true;
}
#Override
public boolean touchUp(int x, int y, int pointer, int button) {
if (!Gdx.app.getType().equals(ApplicationType.Android))
return false;
if (x < width / 2 && y > height / 2) {
charactercontroller.leftReleased();
}
if (x > width / 2 && y > height / 2) {
charactercontroller.rightReleased();
}
return true;
}
#Override
public boolean touchDragged(int x, int y, int pointer) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
#Override
public void connected(Controller controller) {
// TODO Auto-generated method stub
}
#Override
public void disconnected(Controller controller) {
// TODO Auto-generated method stub
}
#Override
public boolean buttonDown(Controller controller, int buttonCode) {
if (buttonCode == Ouya.BUTTON_O) {
charactercontroller.jumpPressed();
}
return false;
}
#Override
public boolean buttonUp(Controller controller, int buttonCode) {
if (buttonCode == Ouya.BUTTON_O) {
charactercontroller.jumpReleased();
}
return false;
}
#Override
public boolean axisMoved(Controller controller, int axisCode, float value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean povMoved(Controller controller, int povCode,
PovDirection value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean xSliderMoved(Controller controller, int sliderCode,
boolean value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean ySliderMoved(Controller controller, int sliderCode,
boolean value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean accelerometerMoved(Controller controller,
int accelerometerCode, Vector3 value) {
// TODO Auto-generated method stub
return false;
}
}
So based on my understand of how this works, right now when I load this on the OUYA the character should Jump when the "O" button is pressed. But nothing happens. I think I'm missing something. Any glaring mistakes in implementation?
Where I'm at with Java is I've been web developing for a number of years and have just recently returned to Java again to play around with games and interactive stuff on micro consoles. I figure starting off with a simple tutorial and experimenting with building on it would be a good idea, right?
Look forward to getting some awesome help!
Thanks,
Phil
Try adding:
Controllers.addListener(this);
right before:
Gdx.input.setInputProcessor(this);
setInputProcessor only registers listeners for the built-in input events (keys, touch events, etc). The controllers are an external library, and it needs to be initialized and loaded separately.
You will need to import com.badlogic.gdx.controllers.Controllers, too.
I am following a video tutorial on a java game ( so I can go off from there ) but one of my buttons will not work (exit button). Please Help. I'm using lwjgl and slick.
main class:
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static final String gamename = "Java Game Alpha 1.0";
public static final int menu = 0;
public static final int play = 1;
public Game(String gamename) {
super(gamename);
this.addState(new Menu(menu));
this.addState(new Play(play));
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(menu);
}
public static void main(String[] args) {
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(1600, 800, false);
appgc.start();
}catch(SlickException e) {
e.printStackTrace();
}
}
}
menu class:
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;
public class Menu implements GameState {
Image PlayNow;
Image exitGame;
public Menu(int state){
}
#Override
public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void mouseWheelMoved(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void inputEnded() {
// TODO Auto-generated method stub
}
#Override
public void inputStarted() {
// TODO Auto-generated method stub
}
#Override
public boolean isAcceptingInput() {
// TODO Auto-generated method stub
return false;
}
#Override
public void setInput(Input arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(int arg0, char arg1) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(int arg0, char arg1) {
// TODO Auto-generated method stub
}
#Override
public void controllerButtonPressed(int arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void controllerButtonReleased(int arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void controllerDownPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerDownReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerLeftPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerLeftReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerRightPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerRightReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerUpPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerUpReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void enter(GameContainer arg0, StateBasedGame arg1)
throws SlickException {
// TODO Auto-generated method stub
}
#Override
public int getID() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException {
// TODO Auto-generated method stub
PlayNow = new Image ("res/PlayNow.png");
exitGame = new Image ("res/exitGame.png");
}
#Override
public void leave(GameContainer arg0, StateBasedGame arg1)
throws SlickException {
// TODO Auto-generated method stub
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
// TODO Auto-generated method stub
g.drawString("It's time for an adventure!", 50, 50);
//g.drawRect(730, 320, 70, 80); //x, y, width, height
PlayNow.draw(680, 320, 250, 50);
exitGame.draw(680, 380, 250, 50);
int xpos = Mouse.getX();
int ypos = Mouse.getY();
g.drawString("Mouse Position: " + xpos + " " + ypos, 10, 22);
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
// TODO Auto-generated method stub
int xpos = Mouse.getX();
int ypos = Mouse.getY();
Input input = gc.getInput();
//play now
if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
if(Mouse.isButtonDown(0)) {
sbg.enterState(1);
}
//exit Game
if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
if(Mouse.isButtonDown(0)) {
gc.exit();
}
}
}
}
}
and there is one more class, but it doesn't matter. Please help!
The problem is your second if statement is nested within the first. So, the first statement must be true for the second to exit. Unfortunately, for the first statement to be true, the second can never be true. So the second never executes.
The first if requires ypos>440 && ypos<478. The second if requires ypos>379 && <417. There is no overlap between those 2 ranges, and so the second if can never execute. Here's your code with cleaner formatting.
if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
if(Mouse.isButtonDown(0)) {
sbg.enterState(1);
}
//ypos will never be less than 417 because to reach this point it MUST
//be greater than 440 due to first if statement.
if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
if(Mouse.isButtonDown(0)) {
gc.exit();
}
}
}
And here's the solution:
if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
if(Mouse.isButtonDown(0)) {
sbg.enterState(1);
}
}
//exit Game
if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
if(Mouse.isButtonDown(0)) {
gc.exit();
}
}
Now the second if is independent of the first.
I'm writing an libgdx game, but it seems that i cannot show my splashscreen, it only shows a black screen. does anyone know how this comes?
I don't get any runtime error, it only shows a blackscreen, even if i set the glClearColor to an other color then black
package mygame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
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;
public class SplashScreen implements Screen {
Texture splashTexture;
Sprite splashSprite;
SpriteBatch batch;
MyGame game;
public SplashScreen(MyGame game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
splashTexture = new Texture("mygame/splash.png");
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite = new Sprite(splashTexture);
//splashSprite.setColor(1, 1, 1, 0);
splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));
batch = new SpriteBatch();
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
I tried your code (see below) and it works well enough, so perhaps the problem lies in the MyGame class. Are you, for example, overriding Game.render() then not calling super.render()?
package hacks;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL10;
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;
public class MyGame extends com.badlogic.gdx.Game {
#Override
public void create() {
setScreen(new SplashScreen(this));
}
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = MyGame.class.getName();
config.width = 800;
config.height = 480;
config.fullscreen = false;
config.useGL20 = true;
config.useCPUSynch = true;
config.forceExit = true;
config.vSyncEnabled = true;
new LwjglApplication(new MyGame(), config);
}
}
class SplashScreen implements Screen {
Texture splashTexture;
Sprite splashSprite;
SpriteBatch batch;
MyGame game;
public SplashScreen(MyGame game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
splashTexture = new Texture("assets/data/libgdx.png");
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite = new Sprite(splashTexture);
// splashSprite.setColor(1, 1, 1, 0);
splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));
batch = new SpriteBatch();
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
this is how my game class looks like:
package mygame;
import com.badlogic.gdx.Game;
public class MyGame extends Game {
#Override
public void create() {
setScreen(new SplashScreen(this));
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void render() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
This is how your game class should look like...you need to call the super functions .
package com.sample;
import com.badlogic.gdx.Game;
public class Sample extends Game{
#Override
public void dispose() {
// TODO Auto-generated method stub
super.dispose();
}
#Override
public void pause() {
// TODO Auto-generated method stub
super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
super.resume();
}
#Override
public void render() {
// TODO Auto-generated method stub
super.render();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
super.resize(width, height);
}
#Override
public void create() {
// TODO Auto-generated method stub
setScreen(new AnotherScreen(this));
}
}
So I have this method that when i touch the screen my sprite will jump. Now the problem is when I continuously touch the screen the sprite will jump again and again. What I want to do is if it jumps, the jump method cannot be called unless my sprite hits the ground.
Here is the code.
public class PhyiscsActivity extends BaseGameActivity implements IAccelerometerListener, IOnSceneTouchListener{
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mTextureRegion;
private Scene mScene;
private FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(1,-10f, 0.5f);
private PhysicsWorld mPhysicsWorld;
private Body body;
private AnimatedSprite facebox;
private FixtureDef wallfixture = PhysicsFactory.createFixtureDef(-1,0.5f, 0.5f);
#Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
this.mCamera = new Camera(0,0,CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),this.mCamera);
mEngineOptions.getTouchOptions().setRunOnUpdateThread(true);
return new Engine(mEngineOptions);
}
#Override
public void onLoadResources() {
// TODO Auto-generated method stub
this.mBitmapTextureAtlas = new BitmapTextureAtlas(128,128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/face_box_tiled.png", 0, 0, 2, 1);
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}
#Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
this.mScene.setBackground(new ColorBackground(1,1,1));
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH),false);
// This is the walls
final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
final Shape ground = new Rectangle(0,CAMERA_HEIGHT - 2,CAMERA_WIDTH,2);
final Shape left = new Rectangle(0,0,2,CAMERA_HEIGHT);
final Shape right = new Rectangle(CAMERA_WIDTH -2, 0,2,CAMERA_HEIGHT);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallfixture);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallfixture);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallfixture);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallfixture);
// This is the Sprite
facebox = new AnimatedSprite(30,(CAMERA_HEIGHT - 2) - this.mTextureRegion.getHeight() ,this.mTextureRegion);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, facebox, BodyType.DynamicBody, mFixtureDef);
this.mScene.attachChild(facebox);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(facebox,body,true,true));
this.mScene.setOnSceneTouchListener(this);
return this.mScene;
}
#Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
//Accelerometer
#Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
// TODO Auto-generated method stub
final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX()* 3, 10);
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
this.enableAccelerometerSensor(this);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.disableAccelerometerSensor();
}
//This is where i make the sprite jump
#Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
pSceneTouchEvent.isActionDown();{
this.jump(facebox);
}
return false;
}
public void jump(AnimatedSprite sprite){
boolean jumping = false;
if(!jumping ){
body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));
}
}
public void jump(AnimatedSprite sprite){
if(!jumping){
body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-10));
jumping = true;
}
}
#Override
public void beginContact(Contact contact) {
// TODO Auto-generated method stub
jumping = true;
}
#Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub
jumping = false;
}
I don't know why the beginContact and endContact isn't initialzing. are there things that i should do to initialize this? example like updating a handler? contactlistener?
You need to have a method wich tell you if your player is jumping or not.
public void jump(AnimatedSprite sprite){
if( isJumping(sprite)){
body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));
}
In this method you should put the way to compute if the player is jumping. Like if he is touching a wall (if player's body (x,y) is in a wall (rectangle))
Ok you are using box2d to make physics =)
You have to create a ContactListener, you have to implement this class and the function ou want to add.
http://code.google.com/p/andenginephysicsbox2dextension/source/browse/src/com/badlogic/gdx/physics/box2d/ContactListener.java?r=1605f6e82f710ef9ebbe07632d6b055239d3b520
public void beginContact (Contact contact);
public void endContact (Contact contact);
The contact object will countain your objects (Fixture) that are touching. You just have to check and set jumping to false when the body is touching and true when you call jump and you can jump, set jumping to true;
Ok?
#Override
public void beginContact(Contact contact) {
// TODO Auto-generated method stub
jumping = true;
}
#Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub
jumping = false;
}
This is false.. I would think of it like that only:
#Override
public void beginContact(Contact contact) {
jumping = false; //you touched ground so you aren't jumping anymore
}
#Override
public void endContact(Contact contact) {
jumping = true; //you leave ground so you're jumping
}
and one jump method (not two):
public void jump(AnimatedSprite sprite){
if(!jumping ){
jumping = true;
body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));
}
}
You see?;