在场景中更改多个纹理

时间:2012-12-08 15:14:27

标签: android andengine

根据网站andengine examples的示例我创建了自己的项目,但有多个对象。假设它是一个棋盘,我每8个矩形有8种颜色。现在我卡住了,因为如果一个具有特定颜色的矩形将被设置为假(我有变量isclicked = false/true),所有具有相同颜色的矩形也将也会改变。 我该如何解决这个问题?

我写了相同的代码,如上面链接的源代码。我将颜色存储在单张图片中,这是进一步分裂。

这是一张照片:
bar1
没有点击的颜色

bar2
点击颜色

notclicked
在游戏中(没有被点击)

clicked
在游戏中(一个被点击)

这是我的代码,我将非常感谢您的帮助。

public class Main extends SimpleBaseGameActivity {

    private static final int SIZE = 50;
    private static final int IMAGES_COUNT = 8;
    private static int CAMERA_WIDTH = 400;
    private static int CAMERA_HEIGHT = 600;
    private ITextureRegion[] mColorsRegion = new ITextureRegion[8];

    //
    private BitmapTextureAtlas mColorsTextureAtlas;
    private TiledTextureRegion mColorsTextureRegion;
    private TextureRegion mBackgroundTextureRegion;
    private BitmapTextureAtlas mBackgroundTextureAtlas;
    private BitmapTextureAtlas mPanelTextureAtlas;
    private TextureRegion mPanelTextureRegion;

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

    @Override
    protected void onCreateResources() {

        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        // stuff with colors
        this.mColorsTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 320, 40, TextureOptions.BILINEAR);
        this.mColorsTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mColorsTextureAtlas, this, "bar.png",
                0, 0, 8, 1);
        this.mColorsTextureAtlas.load();

        // woohoo! stuff with background
        this.mBackgroundTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 400, 800, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackgroundTextureAtlas, this,
                "bg.jpg", 0, 0);
        this.mBackgroundTextureAtlas.load();

        // stuff with panel
        this.mPanelTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 400, 800, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mPanelTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mPanelTextureAtlas, this, "panel.png", 0, 0);
        this.mPanelTextureAtlas.load();

    }

    @Override
    protected Scene onCreateScene() {

        this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene();
        scene.setBackground(new Background(5.F, 5.F, 5.F));

        // show background
        Sprite background = new Sprite(0, 0, mBackgroundTextureRegion, getVertexBufferObjectManager());
        scene.attachChild(background);

        // show panel
        Sprite panel = new Sprite(0, 400, mPanelTextureRegion, getVertexBufferObjectManager());
        scene.attachChild(panel);

        // show minirectangles

        // Init generating color numbers
        MyColors colors = new MyColors(IMAGES_COUNT);

        // Init minirectangles with randomed images
        MiniRectangle[] minirectangle = new MiniRectangle[IMAGES_COUNT * IMAGES_COUNT];
        for (int i = 0; i < IMAGES_COUNT; i++) {
            for (int j = 0; j < IMAGES_COUNT; j++) {
                final int index = i * IMAGES_COUNT + j;
                minirectangle[index] = new MiniRectangle(j * SIZE + 2, i * SIZE + 2, SIZE - 4, SIZE - 4,
                        mColorsTextureRegion.getTextureRegion(colors.getRan(index)), getVertexBufferObjectManager()) {

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

                        if (this.isVisible()) {
                            setClicked();

                            togglex(this.isClicked());

                        }
                        return true;
                    }
                };

                // further setting for minirectangle
                minirectangle[index].setIndexX(j);
                minirectangle[index].setIndexY(i);
                minirectangle[index].setNumber(index);
                minirectangle[index].setClicked(false);
                minirectangle[index].setColorNumber(colors.getRan(index));
                minirectangle[index].addColors(mColorsRegion);

                // attach to scene and register touch arena
                scene.attachChild(minirectangle[index]);
                scene.registerTouchArea(minirectangle[index]);
            }
        }

        return scene;
    }

    protected void togglex(boolean clicked) {
        this.mColorsTextureAtlas.clearTextureAtlasSources();
        boolean xclicked = clicked;
        BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mColorsTextureAtlas, this, xclicked ? "bar2.png"
                : "bar.png", 0, 0, 8, 1);

    }
}

1 个答案:

答案 0 :(得分:4)

您无法更改纹理图集的内容 - 所有纹理区域都引用它,因此它们都会更改。

将纹理图集视为大数组。纹理区域类似于此数组中指向不同区域的指针。因此,如果您想要更新精灵的纹理区域,您应该将其指向纹理中的另一个区域。但相反,您正在更改此大型数组的内容,即纹理。所以引用它的所有纹理区域也在变化。

<强>解决方案

您应该将两张图片加载到地图册,然后更改纹理区域

加载两张图片:

this.mColorsTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 320, 80, TextureOptions.BILINEAR); //Note that I doubled the height of the texture.
this.mColorsNotClickedTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mColorsTextureAtlas, this, "bar.png", 0, 0, 8, 1);
this.mColorsClickedTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mColorsTextureAtlas, this, "bar2.png", 0, 40, 8, 1); //The position of bar2.png is not 0,0 because it'll override bar.png. If the height is 40, we position it 40 units below the position of bar.png.
this.mColorsTextureAtlas.load();

现在,单击一个矩形时,更改它的纹理区域。

相关问题