2015-03-13 21 views
-1

我有2個類,inputHandlerMenu和GameWorld,我想從inputHanlerMenu獲得一個整數並在GameWorld中使用它!它嘗試了很多,但我沒有工作。誰能幫我 ?我怎麼能保存一個類的整數,並在另一個類中使用它

我聲明:public 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 =>速度變化......

這裏的INT保持爲0。它不會改變!

任何人都知道該怎麼做! ?

+0

您應該將值傳遞給活動。將值設置爲一個模型類,並在其他模型類中設置值不會工作... – 2015-03-13 16:55:49

+0

您可以將變量聲明爲'protected static',而不是'public'。然後將它引用到包含類的名稱前面。 I.e .:'score = Class1.intValue + 5;' – 2015-03-13 17:07:40

+0

idk該怎麼做? – 2015-03-13 22:45:15

回答

0

此方法添加到您的遊戲世界一流的,並確保模式是公衆詮釋

public int mode; 

public int getModes(){ 
return mode 
} 
在其他類

現在簡單的,只要你需要的整數調用

int getthatint = seter.getModes(); 
+0

它沒有工作!它不會void它的int;)! – 2015-03-13 18:31:22

+0

確保在開始活動時初始化類,以便在需要數據時不必創建新實例。如果你這樣做,它會工作。 – Xjasz 2015-03-13 18:49:02

+0

哪個班?如何 ? – 2015-03-13 22:44:24

0

您可以使用共享偏好和將整數值存儲在Input Handler Menu中,然後您可以在GameWorld中獲取如下所示的值。

在輸入處理程序:

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

在遊戲世界:

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

存儲整數值將被分配到的位置,你可以使用任何你想去的地方。

希望這可以幫助你。

+0

檢查編輯。我已經將String位置更改爲int位置,因爲您正在獲取int值。對不起,錯過了。 – Keshav1234 2015-03-13 17:30:31

+0

我是否必須創建一個新類SharedPreferences? – 2015-03-13 22:40:09

+0

不,這是不夠的.. – Keshav1234 2015-03-13 23:46:38

相關問題