2011-08-16 4 views
0

私はこのメソッドを使用して、画面に触れるとスプライトが飛びます。今問題は、スプライトが何度も何度もジャンプする画面に連続して触れるときです。私がしたいことはジャンプする場合、私のスプライトが地面に当たっていなければジャンプメソッドを呼び出すことができないということです。AndEngineジャンプメソッドが呼び出されるとスプライトを作成しないようにする方法

ここにコードがあります。

 
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; }

beginContactとendContactがinitialzingされていない理由を私は知りません。私はこれを初期化するためにすべきことはありますか?ハンドラの更新のような例? contactlistener?

答えて

1

あなたのプレイヤーがジャンプしているかどうかを伝える方法が必要です。

public void jump(AnimatedSprite sprite){ 

    if(isJumping(sprite)){ 
     body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));   
     } 

この方法では、プレーヤーがジャンプしている場合に計算する方法を指定する必要があります。あなたは、あなたがContactListenerを作成する必要があります(プレイヤーの身体(x、y)は、壁(長方形)である場合)、彼は

)= OK、あなたが物理学を作るためにBOX2Dを使用している

を壁に触れている場合と同様にこのクラスを実装し、追加したい関数を持っていなければなりません。 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); 

連絡先オブジェクトは、接触している、あなたのオブジェクト(フィクスチャー)をcountainます。あなたは、体が接触しているときに跳躍を偽にチェックして設定しなければならず、ジャンプを呼び出すときに真であり、ジャンプすることができます。

@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; 
} 

これが偽..私はそれだけのようにそれを思うだろう:

@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 
} 

と1ジャンプ方法(ない2):参照してください

public void jump(AnimatedSprite sprite){ 
     if(!jumping){ 
      jumping = true; 
      body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));   
      } 
     } 

?;

+0

あなたは私にそれを表示することができますか?私のプレーヤーが壁に触れているかどうかを知る人。 –

+0

壁がどのように記憶されているか教えてください。 – jDourlens

+0

ok私はすべてのコードを投稿します。 –