2011-12-10 4 views
0

私はちょっとしたゲーム/プロジェクトに取り組んでおり、基本的にはそのエンジンの物理機能の追加と削除が必要なだけだったので、AndEngineを使用することにしました。しかし、私はそれに少し問題があるようです。私はシーンの上に36の異なる写真のようなものを追加しなければならず、選択時にそれらを取り除くことができなければならない。問題は、自分の画像が壊れていて、最初の2つの画像しか表示していないことです。私は何かを逃しているのですか、それとも間違っていますか?ここで私は私のデバイス上で得るものです:Android AndEngineフィジックス - 画像の問題

Broken images

、ここでは私のコードは次のとおりです。

public class Game extends BaseExample implements IAccelerometerListener, IOnSceneTouchListener, IOnAreaTouchListener { 

// =========================================================== 
// Constants 
// =========================================================== 
private static final int CAMERA_WIDTH = 980; 
private static final int CAMERA_HEIGHT = 560; 

// =========================================================== 
// Fields 
// =========================================================== 


private BitmapTextureAtlas mBitmapTextureAtlas; 

private TextureRegion firstSprite; 
private TextureRegion secondSprite; 
private TextureRegion thirdSprite; 
private TextureRegion fourthSprite; 
private TextureRegion fifthSprite; 
private TextureRegion sixSprite; 
private TextureRegion sevenSprite; 
private TextureRegion eightSprite; 
private TextureRegion nineSprite; 
private TextureRegion tenSprite; 

private Scene mScene; 

private PhysicsWorld mPhysicsWorld; 
private int mFaceCount = 0; 

@Override 
public Engine onLoadEngine() { 
    Toast.makeText(this, "Touch the screen to add objects. Touch an object to remove it.", Toast.LENGTH_LONG).show(); 
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

    final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera); 
    engineOptions.getTouchOptions().setRunOnUpdateThread(true); 
    return new Engine(engineOptions); 
} 

@Override 
public void onLoadResources() { 
    this.mBitmapTextureAtlas = new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
    this.firstSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "1.png", 0, 0); // 64x32 
    this.firstSprite.setTextureRegionBufferManaged(false); 
    this.secondSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "2.png", 0, 32); // 64x32 
    this.secondSprite.setTextureRegionBufferManaged(false); 
    this.thirdSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "3.png", 0, 32); // 64x32 
    this.thirdSprite.setTextureRegionBufferManaged(false); 
    this.fourthSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "4.png", 0, 32); // 64x32 
    this.fourthSprite.setTextureRegionBufferManaged(false); 
    this.fifthSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "5.png", 0, 32); // 64x32 
    this.fifthSprite.setTextureRegionBufferManaged(false); 
    this.sixSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "6.png", 0, 32); // 64x32 
    this.sixSprite.setTextureRegionBufferManaged(false); 
    this.sevenSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "7.png", 0, 32); // 64x32 
    this.sevenSprite.setTextureRegionBufferManaged(false); 
    this.eightSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "8.png", 0, 32); // 64x32 
    this.eightSprite.setTextureRegionBufferManaged(false); 
    this.nineSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "9.png", 0, 32); // 64x32 
    this.nineSprite.setTextureRegionBufferManaged(false); 
    this.tenSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "10.png", 0, 32); // 64x32 
    this.tenSprite.setTextureRegionBufferManaged(false); 
    this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas); 

} 

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

    this.mScene = new Scene(); 
    this.mScene.setBackground(new ColorBackground(204f, 204f, 204f)); 
    this.mScene.setOnSceneTouchListener(this); 

    this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); 

    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2); 
    final Shape roof = new Rectangle(0, 0, 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); 

    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef); 

    this.mScene.attachChild(ground); 
    this.mScene.attachChild(roof); 
    this.mScene.attachChild(left); 
    this.mScene.attachChild(right); 

    this.mScene.registerUpdateHandler(this.mPhysicsWorld); 

    this.mScene.setOnAreaTouchListener(this); 

    return this.mScene; 
} 

@Override 
public void onLoadComplete() { 

} 

@Override 
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, 
     ITouchArea pTouchArea, float pTouchAreaLocalX, 
     float pTouchAreaLocalY) { 

    if(pSceneTouchEvent.isActionDown()) { 
     this.removeFace((Sprite)pTouchArea); 
     return true; 
    } 

    return false; 
} 

@Override 
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { 
    if(this.mPhysicsWorld != null) { 
     if(pSceneTouchEvent.isActionDown()) { 
      this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()); 
      return true; 
     } 
    } 
    return false; 
} 

@Override 
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) { 
    final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY()); 
    this.mPhysicsWorld.setGravity(gravity); 
    Vector2Pool.recycle(gravity); 
} 

private void addFace(final float pX, final float pY) { 
    this.mFaceCount++; 

    Sprite face = null; 
    Body body = null; 

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

    /*if(this.mFaceCount % 2 == 0) { 
     face = new Sprite(pX, pY, this.firstSprite); 
     body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
    } else { 
     face = new Sprite(pX, pY, this.secondSprite); 
     body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
    }*/ 

    switch(this.mFaceCount){ 
    case 1: 
      face = new Sprite(pX, pY, this.firstSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 2: 
      face = new Sprite(pX, pY, this.secondSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 3: 
      face = new Sprite(pX, pY, this.thirdSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 4: 
      face = new Sprite(pX, pY, this.fourthSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 5: 
      face = new Sprite(pX, pY, this.fifthSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 6: 
      face = new Sprite(pX, pY, this.sixSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 7: 
      face = new Sprite(pX, pY, this.sevenSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 8: 
      face = new Sprite(pX, pY, this.eightSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 9: 
      face = new Sprite(pX, pY, this.nineSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 
    case 10: 
      face = new Sprite(pX, pY, this.tenSprite); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); 
     break; 

    } 

    if(this.mFaceCount<=9){ 
     this.mScene.registerTouchArea(face); 
     this.mScene.attachChild(face); 
     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true)); 
    } 
} 

@Override 
public void onResumeGame() { 
    super.onResumeGame(); 

    this.enableAccelerometerSensor(this); 
} 


private void removeFace(final Sprite face) { 
    final PhysicsConnector facePhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(face); 

    this.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector); 
    this.mPhysicsWorld.destroyBody(facePhysicsConnector.getBody()); 

    this.mScene.unregisterTouchArea(face); 
    this.mScene.detachChild(face); 

    System.gc(); 
    } 

} 

私のミスである任意のアイデアは?

答えて

2

主な問題はonLoadResourcesメソッドにあります。

テクスチャ(あなたの場合はBitmapTextureAtlas)は、すべて小さな画像を含む大きなキャンバスです。

ここで、方法BitmapTextureAtlasTextureRegionFactory.createFromAsset(..)は、2つの数値パラメータ(最後の2つのパラメータ)を受け取ります。これらは、テクスチャ上の画像位置に使用されます。

ここで、何をしているのかを見てみましょう。すべての画像を同じ位置に読み込んでください!したがって、いくつかの画像(最後にロードされた画像 - 10.png)は、他の画像を上書きします...

詳細については、テクスチャ& TextureRegions hereを参照してください。

あなたのコードを持っている別の問題 - あなたのaddFace方法では、あなたは体がに作成され、それが自動的にあなたの物理学の世界に参加するPhysicsFactory.createBoxBody(...)を呼び出すとき、あなたはそれとの間に物理コネクタを登録していない場合でも、スプライトの間に、スプライトをシーンに付けない場合でも、

の場合はボディを作成してください。が必要です。それ以外の場合は、必ず破壊してください。

+0

だから、それを変更する必要があるすべての画像に同じです私は別の位置からそれらを始める必要があります。しかし私の状況では、私は36種類のイメージでそれをしなければなりません。だからこそ、これを行うには何らかのトリックがありますか、私はすべてのポジションを変えなければなりません。 –

+0

すべての役職を変更します。そして、イメージが同じサイズであれば、ループでそれを行うことができます。実際には、イメージサイズを格納した配列を作成し、それをループしてテクスチャ領域を作成します。 – Jong

+1

BuildableBitmapTextureAtlasがあり、いくつかのAIを使用してスプライトをテクスチャに配置します。位置を手動で設定する必要はありません。私はそれを使用しませんが、あなたは[SVGの例](http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/SVGTextureRegionExample.java)をチェックアウトすることができます。それを使用中に見る。 – jmcdale

0

this.tenSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas、this、 "10.png"、0,32);

この

は今、最後0,32を見てウル画像のdeclrationは、私は理解してあなただけのすべての画像を取得するには、あなたの答えから、64,64