根据来自站点和引擎示例的示例,我创建了自己的项目,但对象不止一个。假设它是一个棋盘,我每 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);
}
}