我是新来的。我正在学习 Java 中的一个更大的类,并从中创建更小的类。这是我要放入类中的代码的第一部分:
public void initializeMainScreen() {
final Display display = getWindowManager().getDefaultDisplay();
int CAMERA_WIDTH = display.getWidth();
int CAMERA_HEIGHT = display.getHeight();
mainScreen = new ArrayList<Sprite>();
BitmapTextureAtlas tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegion tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/gametitle.png", 0,
0);
mEngine.getTextureManager().loadTexture(tempTexture);
Sprite tempSprite = new Sprite(getCenter(CAMERA_WIDTH, tempTextureRegion
.getWidth()), getCenter(CAMERA_HEIGHT, 4 * tempTextureRegion
.getHeight() + 20), tempTextureRegion);
UISprite us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.5f;
us.height = 1/5f;
us.leftMargin = 0.26f;
us.topMargin = 0.01f;
us.SetProperties();
mainScreen.add(us.sprite);
我把它改成这样:
public class MenuScreen extends GameActivity {
ArrayList<Sprite> mainScreen;
public Sprite sprite;
public void initializeMainScreen() {
final Display display = getWindowManager().getDefaultDisplay();
int CAMERA_WIDTH = display.getWidth();
int CAMERA_HEIGHT = display.getHeight();
mainScreen = new ArrayList<Sprite>();
BitmapTextureAtlas tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegion tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/gametitle.png", 0,
0);
mEngine.getTextureManager().loadTexture(tempTexture);
Sprite tempSprite = new Sprite(getCenter(CAMERA_WIDTH, tempTextureRegion
.getWidth()), getCenter(CAMERA_HEIGHT, 4 * tempTextureRegion
.getHeight() + 20), tempTextureRegion);
UISprite us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.5f;
us.height = 1/5f;
us.leftMargin = 0.26f;
us.topMargin = 0.01f;
us.SetProperties();
mainScreen.add(us.sprite);
一切都解决了,但我在 GameActivity.java 中遇到一个错误,告诉我“对于 GameActivity 类型,方法 initializeMainScreen() 是未定义的。” 当我已经导入 GameActivity 并扩展它时。还有什么我需要做的吗?
我想将其余代码放入其中并没有什么坏处,但它直接来自 GameActivity.java
public void initializeMainScreen() {
final Display display = getWindowManager().getDefaultDisplay();
int CAMERA_WIDTH = display.getWidth();
int CAMERA_HEIGHT = display.getHeight();
mainScreen = new ArrayList<Sprite>();
BitmapTextureAtlas tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegion tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/gametitle.png", 0,
0);
mEngine.getTextureManager().loadTexture(tempTexture);
Sprite tempSprite = new Sprite(getCenter(CAMERA_WIDTH, tempTextureRegion
.getWidth()), getCenter(CAMERA_HEIGHT, 4 * tempTextureRegion
.getHeight() + 20), tempTextureRegion);
UISprite us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.5f;
us.height = 1/5f;
us.leftMargin = 0.26f;
us.topMargin = 0.01f;
us.SetProperties();
mainScreen.add(us.sprite);
tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/logo.png", 0, 0);
mEngine.getTextureManager().loadTexture(tempTexture);
tempSprite = new Sprite(CAMERA_WIDTH - tempTextureRegion.getWidth()
- 10, CAMERA_HEIGHT - tempTextureRegion.getHeight() + 5,
tempTextureRegion) {
@Override
public boolean onAreaTouched(final TouchEvent t, final float x,
final float y) {
if (t.isActionDown()) {
unloadScreen(mainScreen);
loadScreen(aboutScreen);
currentScreenID = ABOUT_SCREEN_ID;
return true;
}
return false;
}
};
us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.2f;
us.height = 1/9f;
us.bottomMargin = 0.0f;
us.rightMargin = 0.0f;
us.SetProperties();
mainScreen.add(us.sprite);
StackPanel sp = new StackPanel(CAMERA_WIDTH, CAMERA_HEIGHT);
tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/newgamebutton.png", 0,
0);
mEngine.getTextureManager().loadTexture(tempTexture);
tempSprite = new Sprite(getCenter(CAMERA_WIDTH, tempTextureRegion
.getWidth()), getCenter(CAMERA_HEIGHT, 4 * tempTextureRegion
.getHeight() + 20), tempTextureRegion) {
@Override
public boolean onAreaTouched(final TouchEvent t, final float x,
final float y) {
if (t.isActionDown()) {
unloadScreen(mainScreen);
if (chooseLevelScreen.size() > 0) {
loadScreen(chooseLevelScreen.get(0));
currentScreenID = CHOOSELEVEL_SCREEN_ID;
}
return true;
}
return false;
}
};
us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.41f;
us.height = 1 / 8f;
us.bottomMargin = 0.0f;
us.rightMargin = 0.0f;
us.SetProperties();
sp.AddElement(us);
tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/optionsbutton.png", 0,
0);
mEngine.getTextureManager().loadTexture(tempTexture);
tempSprite = new Sprite(getCenter(CAMERA_WIDTH, tempTextureRegion
.getWidth()), getCenter(CAMERA_HEIGHT, 4 * tempTextureRegion
.getHeight() + 20)
+ tempTextureRegion.getHeight() + 10, tempTextureRegion) {
@Override
public boolean onAreaTouched(final TouchEvent t, final float x,
final float y) {
if (t.isActionDown()) {
unloadScreen(mainScreen);
loadScreen(optionScreen);
currentScreenID = OPTION_SCREEN_ID;
return true;
}
return false;
}
};
us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.41f;
us.height = 1 / 8f;
us.bottomMargin = 0.0f;
us.rightMargin = 0.0f;
us.SetProperties();
sp.AddElement(us);
tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/helpbutton.png", 0, 0);
mEngine.getTextureManager().loadTexture(tempTexture);
tempSprite = new Sprite(getCenter(CAMERA_WIDTH, tempTextureRegion
.getWidth()), getCenter(CAMERA_HEIGHT, 4 * tempTextureRegion
.getHeight() + 20)
+ 2 * tempTextureRegion.getHeight() + 20, tempTextureRegion) {
@Override
public boolean onAreaTouched(final TouchEvent t, final float x,
final float y) {
if (t.isActionDown()) {
unloadScreen(mainScreen);
mScene.setBackground(new ColorBackground(0, 0, 0));
loadScreen(helpScreen);
currentScreenID = HELP_SCREEN_ID;
return true;
}
return false;
}
};
us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.41f;
us.height = 1 / 8f;
us.bottomMargin = 0.0f;
us.rightMargin = 0.0f;
us.SetProperties();
sp.AddElement(us);
tempTexture = new BitmapTextureAtlas(512, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
tempTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(tempTexture, this, "gfx/quitbutton.png", 0, 0);
mEngine.getTextureManager().loadTexture(tempTexture);
tempSprite = new Sprite(getCenter(CAMERA_WIDTH, tempTextureRegion
.getWidth()), getCenter(CAMERA_HEIGHT, 4 * tempTextureRegion
.getHeight() + 20)
+ 3 * tempTextureRegion.getHeight() + 30, tempTextureRegion) {
@Override
public boolean onAreaTouched(final TouchEvent t, final float x,
final float y) {
if (t.isActionDown()) {
finish();
return true;
}
return false;
}
};
us = new UISprite(CAMERA_WIDTH, CAMERA_HEIGHT, tempSprite);
us.width = 0.41f;
us.height = 1 / 8f;
us.bottomMargin = 0.0f;
us.rightMargin = 0.0f;
us.SetProperties();
sp.AddElement(us);
for (UIElement element : sp.elements) {
mainScreen.add(((UISprite)element).sprite);
}
}
}
GameActivity.java 很大,很难在这里粘贴
public class GameActivity extends LayoutGameActivity implements
IOnSceneTouchListener, IAccelerometerListener {
// ===========================================================
// Constants
// ===========================================================
public static final short CATEGORYBIT_NO = 0;
public static final short CATEGORYBIT_WALL = 1;
public static final short CATEGORYBIT_BOX = 2;
public static final short CATEGORYBIT_CIRCLE = 4;
public static final short MASKBITS_WALL = CATEGORYBIT_WALL
+ CATEGORYBIT_BOX + CATEGORYBIT_CIRCLE;
public static final short MASKBITS_BOX = CATEGORYBIT_WALL + CATEGORYBIT_BOX; // Missing:
// CATEGORYBIT_CIRCLE
public static final short MASKBITS_CIRCLE = CATEGORYBIT_WALL
+ CATEGORYBIT_CIRCLE; // Missing: CATEGORYBIT_BOX
public static final short MASKBITS_NOTHING = 0; // Missing: all
public static final short MASKBITS_ONLY_WALL = CATEGORYBIT_WALL; // Missing:
// all
// but
// wall
public static final FixtureDef WALL_FIXTURE_DEF = PhysicsFactory
.createFixtureDef(0, 0.5f, 0.8f, false, CATEGORYBIT_WALL,
MASKBITS_WALL, (short) 0);
public static final FixtureDef BOX_FIXTURE_DEF = PhysicsFactory
.createFixtureDef(1, 0.5f, 0.5f, false, CATEGORYBIT_BOX,
MASKBITS_BOX, (short) 0);
public static final FixtureDef CIRCLE_FIXTURE_DEF = PhysicsFactory
.createFixtureDef(1, 0.5f, 0.5f, false, CATEGORYBIT_CIRCLE,
MASKBITS_CIRCLE, (short) 0);
public static final FixtureDef NO_FIXTURE_DEF = PhysicsFactory
.createFixtureDef(1, 0.5f, 0.5f, false, CATEGORYBIT_NO,
MASKBITS_NOTHING, (short) 0);
public static final FixtureDef ONLY_WALL_FIXTURE_DEF = PhysicsFactory
.createFixtureDef(1, 0.2f, 1f, false, CATEGORYBIT_CIRCLE,
MASKBITS_ONLY_WALL, (short) -1);
public static final int NOSCREEN = 0;
public static final int MAIN_SCREEN_ID = 1;
public static final int OPTION_SCREEN_ID = 2;
public static final int HELP_SCREEN_ID = 3;
public static final int ABOUT_SCREEN_ID = 4;
public static final int CHOOSELEVEL_SCREEN_ID = 5;
public static final int PAUSE_SCREEN_ID = 6;
public static final int LOADING_SCREEN_ID = 7;
如果有帮助,这里的链接