2017-08-26 70 views
0

我正在一個遊戲應用程序。我希望球員分享他們的高分。到目前爲止,我已經創建了分享按鈕,並創建了一個矩形來匹配每個分享按鈕的座標。我不知道的是,如何分享用戶的高分。 這裏是類不知道如何去添加一個分享高分功能

public class ScoreState extends State { 
    private String easyHighScore, hardHighScore, superHardHighScore, superDuperHardHighScore; 
    private int easyY,hardY,superHardY,superDuperHardY; 
    private int highScoreX, shareButtonX,shareWidth,shareHeight; 

    private Rect easyRect,hardRect,superHardRect,superDuperHardRect; 


    @Override 
    public void init() { 
     highScoreX = 112; 
     shareButtonX = 0; 
     easyHighScore = GameMainActivity.getEasyHighScore() + ""; 
     hardHighScore = GameMainActivity.getHardHighScore() + ""; 
     superHardHighScore = GameMainActivity.getSuperHardHighScore() + ""; 
     superDuperHardHighScore = GameMainActivity.getSuperDuperHardHighScore() + ""; 

     easyY=120; 
     hardY=170; 
     superHardY=220; 
     superDuperHardY=270; 

     shareWidth = 112; 
     shareHeight = 50; 

    } 

    @Override 
    public void update(float delta) { 
    } 

    @Override 
    public void render(Painter g) { 
     drawBackground(g); 
     drawHighScoreMessage(g); 
     drawHighScores(g); 
     drawExitMessage(g); 
     drawShareButtons(g); 

    } 

    private void drawHighScoreMessage(Painter g) { 
     g.setColor(Color.WHITE); 
     g.setFont(Typeface.DEFAULT_BOLD, 70); 
     g.drawString("High Scores", 10, 60); 
    } 

    private void drawBackground(Painter g) { 
     g.setColor(Color.rgb(53, 156, 253)); 
     g.fillRect(0, 0, GameMainActivity.GAME_WIDTH, 
       GameMainActivity.GAME_HEIGHT); 
    } 

    private void drawExitMessage(Painter g) { 
     g.setFont(Typeface.DEFAULT_BOLD, 50); 
     g.drawString("Touch the screen to Exit.", 120, 380); 
    } 

    private void drawHighScores(Painter g) { 
     g.setFont(Typeface.DEFAULT_BOLD, 50); 
     /* 
     * 45 is added to the y coordinates of the highScoreStrings in order to 
     * keep it centered with there respective share buttons. Apparently the Canvas.drawText method 
     * which is the base method of g.drawString, starts drawing from the bottom instead of the top. 
     * I didnt read anything to figure this out, but this is obviously the case for whatever reason. 
     */ 
     g.drawString("Easy: " + easyHighScore, highScoreX, easyY + 45); 
     g.drawString("Hard: " + hardHighScore, highScoreX, hardY + 45); 
     g.drawString("SuperHard: " + superHardHighScore, highScoreX, superHardY + 45); 
     g.drawString("SuperDuperHard: " + superDuperHardHighScore, highScoreX, superDuperHardY + 45); 
    } 
    private void drawShareButtons(Painter g){ 

     g.drawImage(Assets.share, shareButtonX, easyY); 
     easyRect = new Rect(shareButtonX, easyY, shareButtonX + shareWidth, easyY + shareHeight); 

     g.drawImage(Assets.share, shareButtonX, hardY); 
     hardRect = new Rect(shareButtonX, hardY, shareButtonX + shareWidth, hardY + shareHeight); 

     g.drawImage(Assets.share, shareButtonX, superHardY); 
     superHardRect = new Rect(shareButtonX, superHardY, shareButtonX + shareWidth, superHardY + shareHeight); 

     g.drawImage(Assets.share, shareButtonX, superDuperHardY); 
     superDuperHardRect = new Rect(shareButtonX, superDuperHardY , shareButtonX + shareWidth, superDuperHardY + shareHeight); 

    } 
    @Override 
    public boolean onTouch(MotionEvent e, int scaledX, int scaledY) { 
     Rect usersTouchRect = new Rect(scaledX,scaledY,scaledX,scaledY); 

     //This is where I would like the sharing to take place 
     if (e.getAction() == MotionEvent.ACTION_UP) { 

      if (easyRect.intersect(usersTouchRect)){ 

      }else if(hardRect.intersect(usersTouchRect)){ 

      }else if (superHardRect.intersect(usersTouchRect)){ 

      }else if (superDuperHardRect.intersect(usersTouchRect)){ 

      } 
      else{ 
      setCurrentState(new MenuState()); 
      } 
     } 
     return true; 
    } 

} 

正如你所看到的,這裏唯一缺少的一步是知道如何按下分享按鈕時共享的高分。

+0

可以產生出得分和用戶圖片和一些文字圖像和分享 – jagapathi

+0

@jagapathi我遇到的問題是通過Facebook,Twitter分享的實際功能。他的用戶能夠看到他自己的恐懼。 –

+0

你的問題對Facebook或Twitter沒有任何評論......爲什麼你不使用Google Play遊戲API? –

回答