2012-08-01 5 views
2

2 개의 볼이 움직이고 하나는 정적 (움직이지 않음) 인 데모 응용 프로그램을 만들고 있습니다. 난 그냥 두 사람이 런타임에 충돌 싶어요. 내가 코드이 줄을 업데이트 위치 진정한를 적용 할 때하지만 내 움직이는 물체는 움직이지 :움직이는 공을 움직이는 공에 어떻게 적용합니까? AndEngine

this.mPhysicsWorld.registerPhysicsConnector (새 PhysicsConnector (공, 거짓 몸, TRUE));

여기 우는 소리가 런타임 이미지이지만, 모두 충돌하지 않는 내 코드

public class DizyBall extends SimpleBaseGameActivity implements IOnSceneTouchListener { 
// =========================================================== 
// Constants 
// =========================================================== 

private static final int CAMERA_WIDTH = 740; 
private static final int CAMERA_HEIGHT = 480; 

private static final float DEMO_VELOCITY = 150.0f; 

// =========================================================== 
// Fields 
// =========================================================== 
private Scene mScene; 
private PhysicsWorld mPhysicsWorld; 
private Body body; 
private FixtureDef objectFixtureDef ; 
private BitmapTextureAtlas mBitmapTextureAtlas; 
private TiledTextureRegion mFaceTextureRegion; 
private TextureRegion mColiTextureRegion; 

// =========================================================== 
// Constructors 
// =========================================================== 

// =========================================================== 
// Getter & Setter 
// =========================================================== 

// =========================================================== 
// Methods for/from SuperClass/Interfaces 
// =========================================================== 

@Override 
public EngineOptions onCreateEngineOptions() { 
    final Camera camera = new Camera(0, 0, DizyBall.CAMERA_WIDTH, DizyBall.CAMERA_HEIGHT); 

    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(DizyBall.CAMERA_WIDTH, DizyBall.CAMERA_HEIGHT), camera); 
} 

@Override 
public void onCreateResources() { 
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 48, 48, TextureOptions.BILINEAR); 
    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "ball.png", 0, 0, 1, 1); 
    this.mColiTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "ball.png",0,0); 

    this.mBitmapTextureAtlas.load(); 
} 

@Override 
public Scene onCreateScene() { 
    this.mEngine.registerUpdateHandler(new FPSLogger()); 


    mScene = new Scene(); 
    mScene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f)); 
    mScene.setOnSceneTouchListener(this); 


    objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); 

    this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 3, 2); 

    final float centerX = (DizyBall.CAMERA_WIDTH - this.mFaceTextureRegion.getWidth())/2; 
    final float centerY = (DizyBall.CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight())/2; 

    final Sprite Coli = new Sprite(centerX, centerY, this.mColiTextureRegion, this.getVertexBufferObjectManager()); 
    body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, Coli, BodyType.DynamicBody, objectFixtureDef); 
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(Coli, body, true, true)); 


    mScene.attachChild(Coli); 
    this.mScene.registerUpdateHandler(this.mPhysicsWorld); 
    return mScene; 
} 

// =========================================================== 
// Methods 
// =========================================================== 


@Override 
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { 

    //  pSceneTouchEvent 
    if(this.mPhysicsWorld != null) { 
     if(pSceneTouchEvent.isActionUp()){ 
      final Ball ball = new Ball(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), this.mFaceTextureRegion, this.getVertexBufferObjectManager()); 



      body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ball, BodyType.DynamicBody, objectFixtureDef); 

      this.mScene.attachChild(ball); 
      this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, body, false, true)); 
      return false; 
     } 
    }else{ 
     Log.v("PhysicsWorld","NULL"); 
    } 

    return false; 
} 
// =========================================================== 
// Inner and Anonymous Classes 
// =========================================================== 

private static class Ball extends AnimatedSprite { 
    private final PhysicsHandler mPhysicsHandler; 

    public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) { 
     super(pX, pY, pTextureRegion, pVertexBufferObjectManager); 
     this.mPhysicsHandler = new PhysicsHandler(this); 
     this.registerUpdateHandler(this.mPhysicsHandler); 
     this.mPhysicsHandler.setVelocity(DizyBall.DEMO_VELOCITY, DizyBall.DEMO_VELOCITY); 
    } 

    @Override 
    protected void onManagedUpdate(final float pSecondsElapsed) { 
     if(this.mX < 0) { 
      this.mPhysicsHandler.setVelocityX(DizyBall.DEMO_VELOCITY); 
     } else if(this.mX + this.getWidth() > DizyBall.CAMERA_WIDTH) { 
      this.mPhysicsHandler.setVelocityX(-DizyBall.DEMO_VELOCITY); 
     } 

     if(this.mY < 0) { 
      this.mPhysicsHandler.setVelocityY(DizyBall.DEMO_VELOCITY); 
     } else if(this.mY + this.getHeight() > DizyBall.CAMERA_HEIGHT) { 
      this.mPhysicsHandler.setVelocityY(-DizyBall.DEMO_VELOCITY); 
     } 

     super.onManagedUpdate(pSecondsElapsed); 
    } 
} 
} 

입니다. enter image description here

+0

이 줄을 주석 처리 할 때 터치됩니다. this.registerUpdateHandler (this.mPhysicsHandler); ../ –

+0

그것은이 질문에 현상금을 넣기 전에 많은 연구를하지 못했던 것처럼 보입니다. –

+0

나는 신축성 1을 사용하여 같은 속도로 튀었고 body.setLinearVelocity (10,10); 너무. 그러나 중력 때문에 잘 움직이지 않는 것은 각성하지 않는 것을 의미합니다. –

답변

0

mScene.registerUpdateHandler (this.mPhysicsHandler);

mScene.registerUpdateHandler(this); 

는 그 다음 updatehandler를 구현하고 구현되지 않은 메소드를 추가, 지금 당신은 다른 물체와 충돌 할

body.setLinearVelocity(10,10); 

처럼의 onUpdate 방법에서 모든 작업을 수행 할 수 있습니다.

+0

AndEngine 예제를 확인할 수 있습니다. 아니면 지금 뭐가 잘못 됐는지 말해 줄 수 있고 내가 도와 줄거야. –

+0

첫 번째 부울은 이동을 활성화하기위한 것이며 두 번째 부울은 회전을 활성화하기위한 것입니다. 공을 움직이기 위해 뭐라도 했니? 그것에 힘을가하거나 그것에 속도를 설정하는 것과 같은가? –

+0

리니어 속도를 피직스 핸들러로 설정하지 않았 으면 바디에 직접 설정하십시오. 직접 작성한 코드가 아닌 것 같습니다. https://github.com/nicolasgramlich/AndEngineExamples 예를 먼저 살펴 보시기 바랍니다. –