前提・実現したいこと
いつもお世話になっております。
androidstudioで、簡単なゲームアプリを作っています。
画面の流れとしては、
スタート画面(activity_screenstart.xml)
↓
ゲーム画面(MainView.java)
↓
リトライ画面(activity_screenretry.xml)
となっています。
そのほかに、ハイスコアを管理するPlayLog.javaがあります。
MainViewクラス(とPlayLogクラス)で取得したハイスコアの数値を、
スタート画面に表示したいのですが、どのようにしたらよいでしょうか。
そのままMainViewにハイスコアを表示することはできるのですが、
画面をまたいで?xmlに表示する方法がわかりません。
よろしくお願いいたします。
該当のソースコード
java
1package com.hachiya.candyzombie; 2 3import android.app.Activity; 4import android.content.pm.ActivityInfo; 5import android.os.Bundle; 6import android.os.Handler; 7import android.view.SurfaceView; 8import android.view.View; 9import android.widget.ImageButton; 10 11public class MainActivity extends Activity { 12 13 private SurfaceView view; 14 15 private Handler handlerA = new Handler(); 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 21 22 setScreenStart(); 23 } 24 25 //スタート画面 26 public void setScreenStart() { 27 setContentView(R.layout.activity_screenstart); 28 29 ImageButton startButton = findViewById(R.id.start_button); 30 31 startButton.setOnClickListener(new View.OnClickListener() { 32 @Override 33 public void onClick(View v) { 34 35 setScreenPlay(); 36 } 37 }); 38 } 39 40 //リトライ画面 41 public void setScreenRetry() { 42 setContentView(R.layout.activity_screenretry); 43 44 ImageButton retryButton = findViewById(R.id.retry_button); 45 46 retryButton.setOnClickListener(new View.OnClickListener() { 47 @Override 48 public void onClick(View v) { 49 setScreenStart(); 50 } 51 }); 52 } 53 54 //プレイ画面 55 public void setScreenPlay() { 56 57 view = new MainView(this); 58 setContentView(view); 59 60 // 30秒後に1度だけ処理を実行 61 handlerA.postDelayed(new Runnable() { 62 public void run() { 63 setScreenRetry(); 64 }, 30 * 1000); 65 66} 67
java
1package com.hachiya.candyzombie; 2 3import android.content.Context; 4import android.content.res.Resources; 5import android.graphics.Bitmap; 6import android.graphics.BitmapFactory; 7import android.graphics.Canvas; 8import android.graphics.Color; 9import android.graphics.Paint; 10import android.media.MediaPlayer; 11import android.view.MotionEvent; 12import android.view.SurfaceHolder; 13import android.view.SurfaceView; 14 15import java.util.Random; 16 17// SurfaceViewクラスを継承したクラスを作成 18public class MainView extends SurfaceView implements SurfaceHolder.Callback, Runnable { 19 20 private int score = 0; 21 22 SurfaceHolder surfaceHolder; 23 Thread thread; 24 25 //コンストラクタ 26 public MainView(Context context) { 27 super(context); 28 29 surfaceHolder = getHolder(); 30 surfaceHolder.addCallback(this); 31 32 PlayLog.init(context); 33 34 } 35 36 @Override 37 public void surfaceCreated(SurfaceHolder holder) { 38 thread = new Thread(this); 39 thread.start(); 40 } 41 42 @Override 43 public void surfaceDestroyed(SurfaceHolder holder) { 44 thread = null; 45 } 46 47 @Override 48 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 49 50 } 51 52 @Override 53 public void run() { 54 55 Canvas canvas = null; 56 57 while (thread != null) { 58 59 try { 60 61 PlayLog.logScore(score); 62 63 //描画開始 64 canvas = surfaceHolder.lockCanvas(); 65 66 //省略(ゲーム操作に応じて、scoreが更新されるようになっています) 67 68 //描画終了 69 surfaceHolder.unlockCanvasAndPost(canvas); 70 } 71 catch (Exception e) { } 72 } 73 } 74 75}
java
1package com.hachiya.candyzombie; 2 3import android.content.Context; 4import android.content.SharedPreferences; 5 6public final class PlayLog { 7 8 private static final String FILENAME_PLAYLOG = "playlog"; 9 private static Context context; 10 11 private static int lastScore = 0; 12 private static int highScore = 0; 13 14 public static void init(final Context context) { 15 16 PlayLog.context = context; 17 load(); 18 } 19 20 public static void logScore(final int score) { 21 22 lastScore = score; 23 24 if (highScore < lastScore) { 25 highScore = score; 26 save(); 27 } 28 } 29 30 public static int lastScore() { 31 return lastScore; 32 } 33 34 public static int bestScore() { 35 return highScore; 36 } 37 38 public static void save() { 39 SharedPreferences preference = context.getSharedPreferences(FILENAME_PLAYLOG, Context.MODE_PRIVATE); 40 SharedPreferences.Editor editor = preference.edit(); 41 editor.putInt("hScore", highScore); 42 editor.commit(); 43 } 44 45 public static void load() { 46 SharedPreferences preference = context.getSharedPreferences(FILENAME_PLAYLOG, Context.MODE_PRIVATE); 47 highScore = preference.getInt("hScore", 0); 48 } 49}
xml
1<!--activity_screenstart--> 2 3<?xml version="1.0" encoding="utf-8"?> 4 5<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 6 xmlns:tools="http://schemas.android.com/tools" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <ImageView 12 android:id="@+id/startImg" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:layout_centerHorizontal="true" 16 android:layout_centerVertical="true" 17 android:scaleType="centerCrop" 18 android:src="@drawable/bgimg1" /> 19 20 <ImageButton 21 android:id="@+id/start_button" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_centerInParent="true" 25 android:background="@drawable/start" /> 26 27 <ImageView 28 android:id="@+id/player" 29 android:layout_width="200dp" 30 android:layout_height="150dp" 31 android:layout_alignTop="@+id/start_button" 32 android:layout_toLeftOf="@+id/start_button" 33 android:src="@drawable/player" /> 34 35 <ImageView 36 android:layout_width="300dp" 37 android:layout_height="200dp" 38 android:layout_toLeftOf="@+id/start_button" 39 android:layout_above="@+id/player" 40 android:src="@drawable/startwords" /> 41 42 <TextView 43 android:id="@+id/Title" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:text="@string/title" 47 android:textSize="30sp" 48 android:textColor="#FFFFFF" 49 android:layout_centerHorizontal="true" 50 android:layout_above="@id/start_button" 51 android:layout_marginBottom="20dp"/> 52 53</RelativeLayout>
補足
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/28 08:46
2018/06/28 10:25
2018/07/06 07:00
2018/07/08 05:09
2018/07/08 05:22