2015-03-13 51 views
1

我正在嘗試做一個遊戲,我存儲高分。什麼是MyGameProgress類?

的「實現登錄Android上的」鏈接:

https://developers.google.com/games/services/training/signin#signing_the_player_in_at_startup

表示使用MyGameProgress類保存了比分,成就等。 不過,我得到一個「無法解析符號MyGameProgress「當我嘗試聲明一個變量爲MyGameProgress (例如MyGameProgress mGameProgress = new MyGameProgress())

任何人都有線索?

下面

是我MainActivity.java文件:


import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.games.Games; 
import com.google.example.games.basegameutils.BaseGameUtils; 



public class MainActivity extends ActionBarActivity { 

    int Score = 0; 

    GoogleApiClient mGoogleApiClient; 
    MyGameProgress mGameProgress = new MyGameProgress(); 


    private static int RC_SIGN_IN = 9001; 
    private boolean mResolvingConnectionFailure = false; 
    private boolean mAutoStartSignInFlow = true; 
    private boolean mSignInClicked = false; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 




    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (mResolvingConnectionFailure) { 
      // Already resolving 
      return; 
     } 

     // If the sign in button was clicked or if auto sign-in is enabled, 
     // launch the sign-in flow 


     if (mSignInClicked || mAutoStartSignInFlow) { 
      mAutoStartSignInFlow = false; 
      mSignInClicked = false; 
      mResolvingConnectionFailure = true; 

      // Attempt to resolve the connection failure using BaseGameUtils. 
      // The R.string.signin_other_error value should reference a generic 
      // error string in your strings.xml file, such as "There was 
      // an issue with sign in, please try again later." 
      if (!BaseGameUtils.resolveConnectionFailure(this, 
        mGoogleApiClient, connectionResult, 
        RC_SIGN_IN, "R.string.signin_other_error")) { 
       mResolvingConnectionFailure = false; 
      } 
     } 

     // Put code here to display the sign-in button 
    } 




    public void increaseScore(View view) { 
     Score = Score + 1; 
     TextView ScoreTextV = (TextView)findViewById(R.id.textView); 

     ScoreTextV.setText(Integer.toString(Score)); 
    }; 

    public void FinishGame(View view) { 
     //Games.Leaderboards.submitScore(getApiClient(), 
     //  getString(R.string.leaderboard_high_score), Score); 


     // when user finishes level: 
     mGameProgress.addScore(userScore); 
     mGameProgress.addAchievement(fooAchievement); 
     mGameProgress.addAchievement(barAchievement); 
     if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { 
      mGameProgress.save(mGoogleApiClient); 
     } 


    } 
} 

在我的build.gradle(module.app)我已經在依賴這包括:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile project(':BaseGameUtils') 
    compile 'com.android.support:appcompat-v7:21.0.3' 
    //compile 'com.google.android.gms:play-services-games:6.5.87' 
    compile 'com.google.android.gms:play-services:6.5.+' 

在我的AndroidManifest.xml中包括:

<meta-data android:name="com.google.android.gms.games.APP_ID" 
     android:value="@string/app_id" /> 

    <meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

我還進口了BaseGameUtils模塊。


爲了方便起見,這裏是從我上面提供的「實現登錄Android上的」鏈接的代碼片段:

MyGameProgress mGameProgress = ....; 

// when user finishes level: 
mGameProgress.addScore(userScore); 
mGameProgress.addAchievement(fooAchievement); 
mGameProgress.addAchievement(barAchievement); 
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { 
    mGameProgress.save(mGoogleApiClient); 
} 

@Override 
protected void onConnected() { 
    // sign in successful, so save progress if we have any. 
    if (mGameProgress != null) { 
     mGameProgress.save(mGoogleApiClient); 
    } 

    // ... 
} 

,如果我需要創建MyGameProgress類我自己,我不知道如何創建'保存'方法,以便將高分存入遊戲服務雲。


我還看到了一些關於'快照'來保存遊戲進度。你認爲這是一個更好的方式嗎?我應該這樣研究嗎?

+0

您是否將適當的庫添加到您的項目並導入正確的類? – JonasCz 2015-03-13 18:48:35

+0

感謝您的迴應。我剛添加了關於我添加到我的build.gradle和AndroidManifest文件的信息。希望這可以幫助你更好地瞭解可能的錯誤。 – 2015-03-13 21:24:58

+0

我導入了BaseGameUtils模塊。 – 2015-03-13 21:28:43

回答

1

事實證明,MyGameProgress(它有一個'save'方法)是一個文檔希望我創建的類。
但是,我可以改爲使用Games.Leaderboards.submitScore方法。

我能夠使它與聲明一起工作: Games.Leaderboards.submitScore(mGoogleApiClient,getString(R.string.leaderboard_high_score)Score);

注: 原來一些文件說要用 Games.Leaderboards.submitScore(getApiClient()的getString(R.string.leaderboard_high_score),分數);

但getApiClient()部分使它無法正常工作。 它在我將它替換爲 GoogleApiClient變量mGoogleApiClient後生效。