如何将方法转换为类?

时间:2013-03-11 23:46:33

标签: java class methods

我是新来的。我正在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;

如果有帮助,请点击链接here

1 个答案:

答案 0 :(得分: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();