Android 更改场景中的多个纹理

Android 更改场景中的多个纹理,android,andengine,Android,Andengine,根据网站上的例子,我创建了自己的项目,但有不止一个对象。假设它是一个棋盘,每8个矩形有8种颜色。现在我卡住了,因为如果一个具有特定颜色的矩形将被设置为false(我有变量isclicked=false/true),那么所有具有相同颜色的矩形也将被更改。 我应该如何解决这个问题 我写的代码与上面链接中的源代码相同。我在一张图片中存储颜色,这是进一步的分割 这里有一张图片: 未单击的颜色 单击的颜色 在游戏中(没有被点击) 在游戏中(一个被点击) 这是我的代码,如果有任何帮助,我将不胜感激

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

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

这里有一张图片:

未单击的颜色


单击的颜色


在游戏中(没有被点击)


在游戏中(一个被点击)

这是我的代码,如果有任何帮助,我将不胜感激

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

    }
}
public类Main扩展了SimpleBaseGameActivity{
私有静态最终整数大小=50;
私有静态最终整数图像计数=8;
专用静态int摄像机_宽度=400;
专用静态int摄像机的高度=600;
私有ITextureRegion[]mColorsRegion=新的ITextureRegion[8];
//
私有BitmapTextureAtlas mcolorTextureAtlas;
私人瓷砖纺织区mColorsTextureRegion;
私人纹理区域mBackgroundTextureRegion;
私人BitmapTextureAtlas mBackgroundTextureAtlas;
私人BitmapTextureAtlas mPanelTextureAtlas;
私有纹理区域mPanelTextureRegion;
@凌驾
public EngineOptions onCreateEngineOptions(){
最终摄像机=新摄像机(0,0,摄像机宽度,摄像机高度);
返回新引擎选项(true、ScreenOrientation.PORTRAIT\u FIXED、新比率解决方案策略(摄像头宽度、摄像头高度)、摄像头);
}
@凌驾
受保护的void onCreateResources(){
BitmapTextureLastTextureRegionFactory.setAssetBasePath(“gfx/”);
//用颜色填充
this.mColorsTextureAtlas=新的BitmapTextureAtlas(this.getTextureManager(),320,40,TextureOptions.BILINEAR);
this.mColorsTextureRegion=BitMapTextureAtlastTextureRegionFactory.createTiledFromAsset(this.mColorsTextureAtlas,this,“bar.png”,
0, 0, 8, 1);
this.mColorsTextureAtlas.load();
//呜呜!有背景的东西
this.mBackgroundTextureAtlas=新的BitmapTextureAtlas(getTextureManager(),400800,TextureOptions.BILINEAR\u PREMULTIPLYALPHA);
this.mBackgroundTextureRegion=BitMapTextureAtlastTextureRegionFactory.createFromAsset(this.mBackgroundTextureAtlas,this,
“bg.jpg”,0,0);
这个.mBackgroundTextureAtlas.load();
//填充面板
this.mPanelTextureAtlas=新的BitmapTextureAtlas(getTextureManager(),400800,TextureOptions.BILINEAR\u PREMULTIPLYALPHA);
this.mPanelTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mPanelTextureAtlas,this,“panel.png”,0,0);
这个.mPanelTextureAtlas.load();
}
@凌驾
受保护的场景onCreateSecene(){
this.mEngine.registerUpdateHandler(新的FPSLogger());
最终场景=新场景();
场景.挫折背景(新背景(5.F,5.F,5.F));
//展示背景
精灵背景=新精灵(0,0,mBackgroundTextureRegion,getVertexBufferObjectManager());
场景。附加儿童(背景);
//显示面板
精灵面板=新精灵(0,400,mPanelTextureRegion,getVertexBufferObjectManager());
场景。附件(面板);
//显示小矩形
//初始化生成色数
MyColors颜色=新的MyColors(图像计数);
//带有随机图像的初始小矩形
迷你矩形[]迷你矩形=新迷你矩形[图像计数*图像计数];
对于(int i=0;i
无法更改纹理图集的内容-所有纹理区域都在引用它,因此它们都已更改

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

解决方案

你应该把两个都装进去
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();