不知道如何去添加一个分享高分功能
问题描述:
我正在一个游戏应用程序。我希望球员分享他们的高分。到目前为止,我已经创建了分享按钮,并创建了一个矩形来匹配每个分享按钮的坐标。我不知道的是,如何分享用户的高分。 这里是类不知道如何去添加一个分享高分功能
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;
}
}
正如你所看到的,这里唯一缺少的一步是知道如何按下分享按钮时共享的高分。
答
如果您想在Facebook或Twitter等社交平台上分享分数,则需要集成平台特定的sdk。 对于Facebook看到
https://developers.facebook.com/docs/sharing/android
的这个另一种形式可以是你使用谷歌Play游戏服务,并创建一个排行榜。所有成员及其得分都将列在该排行榜上。
https://developers.google.com/games/services/android/leaderboards
可以产生出得分和用户图片和一些文字图像和分享 – jagapathi
@jagapathi我遇到的问题是通过Facebook,Twitter分享的实际功能。他的用户能够看到他自己的恐惧。 –
你的问题对Facebook或Twitter没有任何评论......为什么你不使用Google Play游戏API? –