2011-08-16 2 views
0

그래서이 메서드를 사용하면 화면을 터치하면 스프라이트가 점프됩니다. 이제 문제는 스프라이트가 계속해서 반복적으로 튀어 나오는 화면을 계속 터치 할 때입니다. 내가 원하는 것은 점프하면 내 스프라이트가 땅에 떨어지지 않는 한 점프 메서드를 호출 할 수 없다는 것입니다.AndEngine 점프 메서드가 호출되면 점프하지 않는 스프라이트 만들기 방법

다음은 코드입니다. beginContact 및 endContact이 초기화 해되지 않는 이유

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

모르겠어요. 내가 이것을 초기화하기 위해해야 ​​할 일이 있습니까? 핸들러 업데이트와 같은 예가 무엇입니까? contactlistener?

답변

1

플레이어가 점프하고 있는지 여부를 알려주는 방법이 필요합니다.

public void jump(AnimatedSprite sprite){ 

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

이 방법에서는 플레이어가 점프하고있는 경우 계산 방법을 지정해야합니다. 마찬가지로 그는 벽을 만지는 경우 (플레이어의 몸은 (x, y)의 벽 (사각형)에있는 경우)

) = 당신은 물리학을 만들기 위해 Box2D의 사용 확인

당신은 ContactListener을 만들 필요가

, 당신을 이 클래스를 구현하고 추가 할 함수가 있어야합니다. 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됩니다. 몸을 만지고 점프를 부를 때 점프를 확인하고 점프를 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 
} 

한 점프 방식 (안 2 개) :

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

이 거짓 .. 나는 단지처럼 생각하는 것입니다 당신이 볼

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

?;

+0

어떻게 할 수 있습니까? 내 플레이어가 벽에 닿았는지 알 수있는 사람. –

+0

벽이 메모리에 어떻게 저장되어 있는지 말해야합니다. – jDourlens

+0

나는 모든 코드를 올릴 것입니다. –