我如何从类中保存一个整数并在另一个类中使用它

时间:2015-03-13 16:52:28

标签: java android

我有2个类,inputHandlerMenu和GameWorld,我想从inputHanlerMenu获取一个整数并在GameWorld中使用它!它尝试了很多,但我没有工作。谁能帮我 ?

我宣布:公共GameWorld seter;

 @Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    screenX = scaleX(screenX);
    screenY = scaleY(screenY);

    if (menuButtons.get(0).isTouchUp(screenX, screenY)) {
        world.getMenuObject().getPad().end();
        seter.setModes(1);
       for (int i = 0; i < menuButtons.size(); i++) {
            menuButtons.get(i).end();
        }
        world.getMenuObject().getVolumeButton().end();
        menuButtons.get(0).tranToGameScreen();


    }if (menuButtons.get(1).isTouchUp(screenX, screenY)) {
        world.getMenuObject().getPad().end();
        seter.setModes(2);
       for (int i = 0; i < menuButtons.size(); i++) {
            menuButtons.get(i).end();
        }
        world.getMenuObject().getVolumeButton().end();
        menuButtons.get(1).tranToGameScreen();

    }if (menuButtons.get(2).isTouchUp(screenX, screenY)) {
        world.getMenuObject().getPad().end();
        seter.setModes(3);
         for (int i = 0; i < menuButtons.size(); i++) ....

我将模式声明为公共int模式;

 public void setModes (int mode){
    this.mode = mode;
}

private void collisions() {
    {
        if (!ball.hasCollided()) {
            for (int i = 0; i < pad.getcolCircles().size(); i++)
                if (Intersector.overlaps(pad.getcolCircles().get(i), ball.getColCircle())) {

                    ball.collide();
                    ball.setCollided(true);
                    //Gdx.app.log("Angle", ball.getVelocity().toString());
                    //double perp = 2.0 * ball.getVelocity().cpy().dot(pad.returnNormal(i));
                    //Vector2 reflectDir = ball.getVelocity().cpy().sub((pad.returnNormal(i).scl((float) perp))).scl(1);
                    float newAngle = getAngle2Vecs(ball.getVelocity(), pad.returnNormal(i));

                    //Gdx.app.log("Angle", newAngle + "");
                    ball.setVelocity(new Vector2(gameWidth / 2 - ball.getColCircle().x, gameHeight / 2 - ball.getColCircle().y));

                    int rand = (int) Math.random() * 90 + 5;
                    if (pad.getAngularVelocity() < 0) {
                        ball.setVelocity(ball.getVelocity().cpy().rotate((float) (rand + Math.random() * 50)));
                    } else if (pad.getAngularVelocity() > 0) {
                        ball.setVelocity(ball.getVelocity().cpy().rotate((float) (-rand - Math.random() * 50)));
                    } else {

                        ball.setVelocity(ball.getVelocity().cpy().rotate(Math.random() < 0.5 ? -rand : rand));

                    System.out.println(mode);

                    if (mode == 1) {
                        if (score <= 5) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_0));
                        } else if (score >= 5 && score < 50) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_5));
                        } else if (score >= 10 && score < 50) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_10));
                        } else if (score >= 20 && score < 50) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_20));
                        } else if (score >= 35 && score < 50) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_35));
                        } else if (score >= 50 && score < 75) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_50));
                        } else if (score >= 65 && score < 75) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_65));
                        } else if (score >= 75 && score < 100) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_75));
                        } else if (score >= 100 ) {
                            ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_100));
                        } ....

我用于每个按钮的模式,就像按下按钮0一样,速度会改变,按钮1 =&gt;速度变化......

这里的int保持在0。它没有改变!

任何一个人都知道怎么做! ?

2 个答案:

答案 0 :(得分:0)

将此方法添加到GameWorld类并确保mode为public int

public int mode;

public int getModes(){
return mode
}

现在,只要你需要整数,就可以在你的另一个班级中调用

int getthatint = seter.getModes();

答案 1 :(得分:0)

您可以使用共享首选项并在输入处理程序菜单中存储整数值,然后您可以在GameWorld中获取值,如下所示。

在输入处理程序中:

SharedPreferences sharedPreferences = getSharedPreferences("MyData" , Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putInt("Key" , your_int_value);
                    editor.commit();

在GameWorld中:

 SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
           int position = sharedPreferences.getInt("key", Default_Value);

存储的整数值将分配到位置,您可以随意使用。

希望这对你有所帮助。