Andengine-如何在Android游戏中为Sprite创建重叠和碰撞?

时间:2012-11-16 15:03:24

标签: android sprite andengine

我是 Android游戏开发的新手,我正在使用 AndEngine GLES 2 Java。在此编码中,我创建了2个精灵,分别是 small_box big_box 。当我将 small_box 拖到 big_box 或相反,它相互排斥。这很好,但我的问题是第一个 small_box 和第一个大盒子相互排斥,但第二个 small_box big_box 不会排斥。如何解决?我想要从所有 big_box 中排除所有 small_box ,并且当我将 small_box 拖到另一个 small_box 时,它应该重叠甚至是在 big_box 到另一个 big_box 也........ Plz任何人都可以通过修改此代码或使用示例来帮助它.......

public class SampleActivity extends SimpleBaseGameActivity {
    protected static final int CAMERA_WIDTH = 800;
    protected static final int CAMERA_HEIGHT = 480;
    private ITextureRegion Box1, Box2;;
    private Stack Stack1, Stack2;
    private Sprite small_box,big_box;
    //private final Sprite small_box = new Sprite[1];

    public EngineOptions onCreateEngineOptions() {
        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
                new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }

    public void onCreateResources() {
        try {
            ITexture small_box = new BitmapTexture(this.getTextureManager(),
                    new IInputStreamOpener() {
                        public InputStream open() throws IOException {
                            return getAssets().open("gfx/small_box.png");
                        }
                    });
            ITexture big_box = new BitmapTexture(this.getTextureManager(),
                    new IInputStreamOpener() {
                        public InputStream open() throws IOException {
                            return getAssets().open("gfx/big_box.png");
                        }
                    });
            small_box.load();
            big_box.load();
            this.Box1 = TextureRegionFactory.extractFromTexture(small_box);
            this.Box2 = TextureRegionFactory.extractFromTexture(big_box);
            this.Stack1 = new Stack();
            this.Stack2 = new Stack();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Scene onCreateScene() {
        final Scene scene = new Scene();
        scene.setBackground(new Background(1, 1, 1));
        for( int i=0;i<2;i++){
            small_box = new Sprite(i*300, 100, Box1, this.getVertexBufferObjectManager()) {
                public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,  float pTouchAreaLocalX,  float pTouchAreaLocalY) {
                    if (((Sprite) this.getmStack().peek()).getHeight() != this
                            .getHeight())
                        return false;
                    this.setPosition(pSceneTouchEvent.getX() - this.getWidth()
                            / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                    if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
                        checkForCollisionsWithBoxes();
                    }
                    return true;
                  }
                private Stack getmStack() {
                    // TODO Auto-generated method stub
                    return Stack2;
                }
              };
              big_box = new Sprite(i*300, 200, Box2, this.getVertexBufferObjectManager()) {
                    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,  float pTouchAreaLocalX,  float pTouchAreaLocalY) {
                        if (((Sprite) this.getmStack().peek()).getHeight() != this
                                .getHeight())
                            return false;
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth()
                                / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                        if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
                            checkForCollisionsWithBoxes();
                        }
                        return true;
                      }
                    private Stack getmStack() {
                        // TODO Auto-generated method stub
                        return Stack1;
                    }
                  };
                  scene.attachChild(small_box);
                  scene.attachChild(big_box);
                  this.Stack1.add(big_box);
                  this.Stack2.add(small_box);
                scene.registerTouchArea(small_box);
                scene.registerTouchArea(big_box);
                scene.setTouchAreaBindingOnActionDownEnabled(true);
        }
        return scene;
    }

    private void checkForCollisionsWithBoxes() {
        if ((big_box.collidesWith(small_box) && (Stack1.size() == 0 || small_box.getHeight() < ((Sprite) Stack1.peek()).getHeight()))) {
            small_box.setPosition(100+big_box.getWidth()/2, 50+ big_box.getHeight());
            big_box.setPosition(300+small_box.getWidth()/2, 250+small_box.getHeight()/2);
    }
    }
}

1 个答案:

答案 0 :(得分:0)

您只是检查创建的新框,这就是

的原因

我建议您使用box2D,如果您需要例如小盒子只与小盒子碰撞而不是大盒子碰撞,那么已经有一个例子可用于box2D中的碰撞和过滤碰撞

这是过滤冲突的链接 https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/PhysicsCollisionFilteringExample.java