1

根据来自站点和引擎示例的示例,我创建了自己的项目,但对象不止一个。假设它是一个棋盘,我每 8 个矩形有 8 种颜色。现在我卡住了,因为如果将具有特定颜色的矩形之一设置为 false (我有变量isclicked = false/true),所有具有相同颜色的矩形也会发生变化。我应该如何解决这个问题?

我编写了与上面链接中的源代码相同的代码。我将颜色存储在单个图片中,该图片进一步拆分。

这是一张图片:
酒吧1
未点击的颜色游戏中的

酒吧2
点击颜色

未点击
(未点击)

点击
游戏中的颜色(点击了一个)

这是我的代码,我将不胜感激。

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

    }
}
4

1 回答 1

4

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

将纹理图集视为一个大数组。纹理区域就像指向该数组中不同区域的指针。所以如果你想更新你的精灵的纹理区域,你应该把它指向 texure 中的另一个区域。但是,您正在更改这个大数组的内容,即纹理。所以所有引用它的纹理区域也在变化。

解决方案

您应该将这两个图像加载到您的图集,并更改纹理区域。

加载两个图像:

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();

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

于 2012-12-09T00:40:30.080 回答