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
Related
Sorry for my question , but i am stuck. I am new in develop game with lib gdx and don`t judge me strictly.
I have my game activity:
public class MyGame extends Game {
MenuScreen menu;
SplashScreen splash;
DefendScreen def;
#Override
public void create() {
// Gdx.app.log("LogGame", "MyGame create");
menu = new MenuScreen(this);
splash = new SplashScreen(this);
def = new DefendScreen(this);
setScreen(splash);
}
#Override
public void resize(int width, int height) {
}
#Override
public void render() {
super.render();
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
#Override
public void dispose() {
}
}
I have two screens :
public class MenuScreen implements Screen, InputProcessor {
public MenuScreen(final MyGame gam) {
game = gam;
cam = new OrthographicCamera();
cam.setToOrtho(false, 800, 480);
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
defScreen = new DefendScreen(game);
spriteBatch = new SpriteBatch();
stage = new Stage();
}
#Override
public void dispose() {
Gdx.app.log("LogGame", "splashs dispose");
Gdx.input.setInputProcessor(null);
try {
spriteBatch.dispose();
stage.dispose();
font12.dispose();
} catch (Exception e) {
}
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
}
#Override
public void render(float arg0) {
SetCamera(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2f);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(); // update all actors
stage.draw();
}
#Override
public void resize(int arg0, int arg1) {
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void show() {
stage.addActor(new WorldMenu(new Texture(Gdx.files
.internal("images/bg/pause_back.png"))));
Gdx.input.setInputProcessor(stage);
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(
Gdx.files.internal("font/trebuchet_ms.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 25;
font12 = generator.generateFont(parameter);
generator.dispose();
DrawLeftMeu();
DrawRightMeu();
DrawSetingsMenu();
}}
And game screen where i have pause button:
public class DefendScreen implements Screen, InputProcessor {
public DefendScreen(final MyGame gam) {
game = gam;
cam = new OrthographicCamera();
cam.setToOrtho(false, 800, 480);
startTime = TimeUtils.millis();
spriteBatch = new SpriteBatch();
stage = new Stage();
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
}
#Override
public void dispose() {
Gdx.app.log("LogGame", "defend dispose");
try {
combo0.dispose();
combo1.dispose();
combo2.dispose();
good0.dispose();
good1.dispose();
bad0.dispose();
bad1.dispose();
bad2.dispose();
bad3.dispose();
bad4.dispose();
music.dispose();
music.stop();
spriteBatch.dispose();
font.dispose();
pbar.dispose();
scores.dispose();
} catch (Exception e) {
}
}
#Override
public void hide() {
}
#Override
public void pause() {
// this.state = State.PAUSE;
}
#Override
public void render(float delta) {
SetCamera(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2f);
switch (state) {
case RUN:
RunGame();
break;
case PAUSE:
// do stuff here
break;
case RESUME:
break;
default:
break;
}
}
#Override
public void resize(int arg0, int arg1) {
}
#Override
public void resume() {
this.state = State.RESUME;
}
#Override
public void show() {
regions = new ArrayList<Integer>();
addRegions();
initSounds();
speedgame = speedgameEasy;
stage.addActor(worldActor);
stage.addActor(pauseActor);
startTime = TimeUtils.millis();
stage.addListener(clickList);
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(
Gdx.files.internal("font/trebuchet_ms.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 25;
font = generator.generateFont(parameter);
generator.dispose();
pbar = new Texture(Gdx.files.internal("images/bg/line_indicator.png"));
scores = new Texture(
Gdx.files.internal("images/game_images/scorebox.png"));
Gdx.input.setInputProcessor(stage);
Gdx.input.setCatchBackKey(true);
}}
And my main questions !!!
--> what i must do that on pause action i can freeze the game action and call menu screen (whish have button resume)?
--> what i mus do in resume onclick to show game action resume?
on stackoverflow an other forums i find some explains about my question, which means to implement in resume some switch case with choices of game state, but i am do not understand how its realize in my code.
Thanks everyone.
In your game screen you should have a button (an actor added to the stage) which implements a ClickListener that calls either pause() or resume() depending on the current state of the game.
It should look something like this:
pauseButton.addListener(new ClickListener() {
public void clicked(InputEvent evt, float x, float y) {
if (!paused) Gdx.app.getApplicationListener().pause();
else Gdx.app.getApplicationListener().resume();
}
});
Where paused is a boolean variable in your game screen class.
In my opinion this is the easiest way to do this.
image of problem http://imgur.com/ZNpHiiu so I succesfully rendered a background for my game , now i tried to render some sprites on top of it and screen gets all messed up big Blue L.
code main
public class WizardGame extends Game {
public static final String TITLE = "Are you a bad enough wizard to save the princess?!", VERISION = "0.0.0.0.PREALPHA";
PlayTest pt = new PlayTest();
private FPSLogger fps;
public void create() {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
fps = new FPSLogger();
fps.log();
pt.create();
}
public void dispose() {
super.dispose();
}
public void render() {
super.render();
pt.render();
}
public void resize(int width, int height) {
super.resize(width, height);
}
public void pause() {
super.pause();
}
public void resume() {
super.resume();
}
}
playtest code
public class PlayTest implements Screen {
private TextureAtlas atlas;
private AtlasRegion floor;
private SpriteBatch batch;
private OrthographicCamera camera;
private Sprite background;
private BitmapFont font;
private String fps;
Blocks block = new Blocks();
public void render() {
Gdx.gl20.glClearColor(0, 0, 05, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
background.setBounds(0, 0, 720, 1280);
background.draw(batch);
block.renderBlocks();
batch.end();
}
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
public void show() {
}
public void hide() {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
atlas.dispose();
batch.dispose();
}
public void create() {
atlas = new TextureAtlas(Gdx.files.internal("data/wizardpack.pack"));
floor = atlas.findRegion("Backfloor");
background = new Sprite(floor);
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/magicdebug48.fnt"));
block.create();
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
}
}
block code
public class Blocks extends Game {
private TextureAtlas atlas;
private SpriteBatch batch;
private AtlasRegion sword,shield,staff,fireball,iceball;
private Sprite sprSword,sprShield,sprStaff,sprFireball,sprIceball;
#Override
public void create() {
atlas = new TextureAtlas(Gdx.files.internal("data/wizardpack.pack"));
batch = new SpriteBatch();
sword = atlas.findRegion("Sword");
shield = atlas.findRegion("Shield");
staff = atlas.findRegion("Staff");
fireball = atlas.findRegion("flame");
iceball = atlas.findRegion("icespell");
sprSword = new Sprite(sword);
sprShield = new Sprite(shield);
sprStaff = new Sprite(staff);
sprFireball = new Sprite(fireball);
sprIceball = new Sprite(iceball);
}
public void renderBlocks(){
Gdx.gl20.glClearColor(0, 0, 05, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(sword, 0, 0, 0, 0, 32, 32, 1, 1, 0);
//sprShield.setBounds(15, 15, 32, 32);
// sprShield.draw(batch);
batch.end();
}
public void dispose(){
atlas.dispose();
batch.dispose();
}
}
I fixed it :D the call to renderBlocks has to be after the end of batch.
I have been having a problem with the rendering in my screens.
Basically I have an overall class that extends Game and I have created a few other classes for the various pages for my game such as the Main Menu, the actual game etc. When I call setScreen(screen) on one of these classes the rendering loop of that screen is called, but I cant seem to draw anything.
What I have done is that I created the orthographic camera and spritebatch in the overall game class and passed it to the screens through their constructor method. However, I don't seem to be able to draw anything. The screen still clears the background.
Sorry I don't have my source code at the moment but here is roughly what it looks like:
This is my overall game class:
public class MyGdxGame extends Game {
public OrthographicCamera camera;
public SpriteBatch batch;
public ResourceManager Rm;
public StartScreen MainMenu;
public GameScreen CellTD;
public InstructionsScreen Instructions;
public PauseScreen Pause;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Rm = new ResourceManager();
camera = new OrthographicCamera(1.0f, h/w);
batch = new SpriteBatch();
Rm.LoadTexture("Cell.png");
MainMenu = new StartScreen(camera, batch, Rm, this);
CellTD = new GameScreen(camera, batch, Rm, this);
Instructions = new InstructionsScreen(camera, batch, Rm, this);
Pause = new PauseScreen(camera, batch, Rm, this);
setScreen(MainMenu);
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
This is one of my screen classes:
public class StartScreen implements Screen{
private OrthographicCamera camera;
private SpriteBatch batch;
private ResourceManager Rm;
private int SCREEN_W, SCREEN_H;
public MyGdxGame Parent;
private Label Title;
private Sprite s;
StartScreen(OrthographicCamera c, SpriteBatch b, ResourceManager r, MyGdxGame g)
{
camera = c;
batch = b;
Rm = r;
SCREEN_W = Gdx.graphics.getWidth();
SCREEN_H = Gdx.graphics.getHeight();
Parent = g;
camera.setToOrtho(false,1.0f,SCREEN_H/SCREEN_W);
camera.update();
Title = new Label("Cell TD",new Label.LabelStyle(new BitmapFont(Gdx.files.internal("data/CellTDFont.fnt"),false) ,new Color(1.0f,1.0f,1.0f,1.0f)));
Title.setText("Cell TD");
Title.setSize(1.0f, SCREEN_H/SCREEN_W);
Title.setOrigin(Title.getWidth()/2, Title.getHeight()/2);
Title.setPosition(0, 0);
s = new Sprite(Rm.GetTexture("Cell.png"));
s.setSize(1.0f, SCREEN_H/SCREEN_W);
s.setOrigin(s.getWidth()/2, s.getHeight()/2);
s.setPosition(-0.5f, -SCREEN_H/SCREEN_W/2);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0.9f, 0.9f, 0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
//Title.draw(batch,1);
s.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// 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
}
}
Its an easy bug.
Your camera is not correct i guess.
c = new OrthographicCamera(1.0f,h/w);
this means that the camera is 1px width and h/w height... and your sprite is like 100x200 for example. So the only thing you will see is around 1px of the sprite.
Do change the constructor of it to something liket this:
float w = Gdx.graphics.getWidth(); //the width of the window
float h = Gdx.graphics.getHeight();//the height of the window
camera = new OrthographicCamera(w, h);
Dont forget to resize the camera inside of the resize.
maybe take a look at the new libgdx wiki here:
Orthographic camera
Spritebatch, textureregions, and sprite
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?;