0

私はここで初めてです。私は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 でエラーが発生し、「メソッド initializeMainScreen() が GameActivity 型に対して定義されていません」というメッセージが表示されます。すでに 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;

それが役立つ場合は、ここにリンクがあります

4

1 に答える 1

0

あなたの質問のために:

How do I turn a method into a class?

1) Create a new class.
2) Copy the method from the old class into the constructor of the new class
3) Create a getter for the return value.

例:

class OldClass {
    OldClass() {
    }

    int methodToBeAClass(int x) {
        x++;
        return x;
    }
}

class MethodToBeAClass {
    int result;

    MethodToBeAClass(int x) {
        x++;
        result = x;
    }

    int getResult() {
        return result;
    }
}

使用法:

    final MethodToBeAClass methodToBeAClass = new MethodToBeAClass(1);
    final int x = methodToBeAClass.getResult();
于 2013-03-12T01:18:52.217 に答える