初心者のスマホ(Android)でRPG作成blog

初心者がスマホ(Android)向けのRPGゲームアプリを作っていくブログです。 アプリの作り方やPRの仕方等を試行錯誤しながら紹介して行きます。

タグ:Java

多くの人に見て貰える様にランキングへのご協力をお願いします!

ゲーム制作 ブログランキングへ

説明画面のJavaソースとなります。

 HelpActivityというアクティビティ内に以下の処理が含まれています。
<処理内容>
・onCreate : 画面の初期作成処理(レイアウトファイルの読み込み等)
・showDialog : 画面に文字をポップアップさせるプログラム(他のプログラムに呼ばれる)
・onClickXX : 格ボタン毎の処理内容を設定(XXは数字が入る)

クラスの設定開始直後に以下の記載がありますが、
これは「BR」という変数は今後改行を意味するという設定です。
(文字列の表示に改行を入れたい場合は宣言しておくと便利)

 private final static String BR=System.getProperty("line.separator");

今後紹介するJavaファイルでは良く利用する表示方法なのですが、
onCreateの中で「textView1~3」「button1~10」に対して、
「textSetting.setText」で文字列を表示させています。

これにより画面が開いた直後に各ボタンやタイトルに文字が表示され、
「開始」用のボタンだったり、「終了」用のボタンというのがすぐにわかります。

それとこのJavaプログラムの処理は非常にシンプルな作りになっていて、
「終了」ボタンである「onClick0」は「finish()」でアクティビティを終了させ、
その他は「showDialog」で解説用の説明文を表示させるだけです。

簡単な処理から説明を行っていますが、よくわからないという方は
メールやコメント等を頂ければ説明致しますのでご連絡をお願いします!


■HelpActivity.java(Javaファイル)

package com.hidefumi.kasahara.evel_slayer;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class HelpActivity extends Activity {
private final static String BR=System.getProperty("line.separator");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.help);
//文字列表示
TextView textSetting = (TextView)findViewById(R.id.textView1);
    textSetting.setText("《基本情報》");
//文字列表示
TextView textSetting2 = (TextView)findViewById(R.id.textView2);
   textSetting2.setText("《戦闘画面情報》");
//文字列表示
TextView textSetting3 = (TextView)findViewById(R.id.textView3);
   textSetting3.setText("《移動画面情報》");
   
//「目的」の説明
TextView textSettingb01 = (TextView)findViewById(R.id.button1);
   textSettingb01.setText("目的");
//「依頼」の説明
TextView textSettingb02 = (TextView)findViewById(R.id.button2);
   textSettingb02.setText("依頼");
//「能力」の説明
TextView textSettingb03 = (TextView)findViewById(R.id.button3);
   textSettingb03.setText("ステータス");

   //「攻撃」の説明
TextView textSettingb04 = (TextView)findViewById(R.id.button4);
   textSettingb04.setText("攻撃");
//「魔法」の説明
TextView textSettingb05 = (TextView)findViewById(R.id.button5);
   textSettingb05.setText("剣技");
//「回復」の説明
TextView textSettingb06 = (TextView)findViewById(R.id.button6);
   textSettingb06.setText("回復");
//「契約」の説明
TextView textSettingb7 = (TextView)findViewById(R.id.button7);
   textSettingb7.setText("契約");

   //「探索」の説明
TextView textSettingb08 = (TextView)findViewById(R.id.button8);
   textSettingb08.setText("探索");
//「帰還」の説明
TextView textSettingb09 = (TextView)findViewById(R.id.button9);
   textSettingb09.setText("帰還");
//「情報」の説明
TextView textSettingb10 = (TextView)findViewById(R.id.button10);
   textSettingb10.setText("情報");
}

//showDialogの実装
private void showDialog(HelpActivity mainActivity, String title,String text) {
AlertDialog.Builder ad=new AlertDialog.Builder(mainActivity);
ad.setTitle(title);
ad.setMessage(text);
ad.setPositiveButton("OK", null);
ad.show();
}
//「戻る」コマンド実行処理
public void onClick0(View view){
finish() ;
}

//「目的」解説コマンド
public void onClick1(View view){
showDialog(HelpActivity.this,"目的","シナリオのクリアが目的となります。"+BR+BR+"各章の選択肢によりエンディングが変化します。");
}
//「依頼」解説コマンド
public void onClick2(View view){
showDialog(HelpActivity.this,"依頼","各章で達成すべき目的です。"+BR+BR+"酒場で悪魔の討伐等を依頼されます。");
}
//「ステータス」解説コマンド
public void onClick3(View view){
showDialog(HelpActivity.this,"ステータス","[HP]"+BR+"体力。"+BR+"0になると戦闘不能。"+BR+"HPが0になるとお金を奪われ、酒場に強制送還されます。"+BR+BR+"[SP]"+BR+"精神力。"+BR+"剣技利用時に必要。"+BR+BR+"[ATK]"+BR+"攻撃力。"+BR+"攻撃利用時のダメージ量。");
}
//「攻撃」解説コマンド
public void onClick4(View view){
showDialog(HelpActivity.this,"攻撃","通常攻撃です。"+BR+"ATK+武器能力のダメージを与えます。");
}
//「剣技」解説コマンド
public void onClick5(View view){
showDialog(HelpActivity.this,"技能","習得した剣技を使用します。"+BR+BR+"剣技は一定数利用すると成長します。");
}
//「回復」解説コマンド
public void onClick6(View view){
showDialog(HelpActivity.this,"回復","HPを全回復します。"+BR+BR+"ただし、回復には傷薬が一つ必要です。");
}
//「契約」解説コマンド
public void onClick7(View view){
showDialog(HelpActivity.this,"契約","悪魔と契約して使い魔にする事が出来ます。"+BR+BR+"悪魔はそれぞれに異なる能力を持っています。"+BR+"また、情報画面より魔石を一定数付与すると能力が変化します。"+BR+BR+"契約には魔石が必要です。");
}

//「探索」解説コマンド
public void onClick8(View view){
showDialog(HelpActivity.this,"探索","攻略エリアを探索します。"+BR+"100回の探索で現在のエリアを攻略します。"+BR+BR+"また、モンスターとの戦闘やアイテム取得等のイベントも発生します。");
}
//「帰還」解説コマンド
public void onClick9(View view){
showDialog(HelpActivity.this,"帰還","エリアの攻略を中断し、酒場に戻ります。");
}
//「情報」解説コマンド
public void onClick10(View view){
showDialog(HelpActivity.this,"情報","ステータスや所持品等の各種情報を参照します。");
}


■イビル・スレイヤー
https://play.google.com/store/apps/details?id=com.hidefumi.kasahara.evel_slayer 

多くの人に見て貰える様にランキングへのご協力をお願いします!

ゲーム制作 ブログランキングへ

スタート画面のJavaソースファイルになります。

 MainActivityというアクティビティに中に以下の処理が含まれています。
<処理内容>
・onCreate : 画面の初期作成処理(レイアウトファイルの読み込み等)
・showYesNoDialog : Yes/No選択肢を表示するプログラム(他のプログラムに呼ばれる)
・showDialog : 画面に文字をポップアップさせるプログラム(他のプログラムに呼ばれる)
・onClick1 : 1番のボタンを押した際のプログラム
・onClick2 : 2番のボタンを押した際のプログラム
・onClick3 : 3番のボタンを押した際のプログラム
・onClick4 : 4番のボタンを押した際のプログラム
・onActivityResult : startActivityForResultで画面が移った際の戻り処理

細かなポイントとしては、画面を「図鑑」や「解説」画面に移動する際は、
「startActivity」を使ってただMainActivityの上に表示させています。

それに対して「開始」ボタンの場合は「startActivityForResult」を使い、
「onActivityResult」による画面が戻った際の処理を行う様にさせています。

これはゲーム開始後のメイン画面で「終了」ボタンを押した際に、
再びスタート画面を表示させない様にする為の工夫です。
(「終了」を押したのに終わらないとイラっとしますよね?)

それとボタン4の「初期化」ではアイテム数やイベントフラグ等の
ゲームに関するデータを消去する処理を組み込んでいます。

ポイントという程では無いですが、私の場合はここにパラメータを入れる事で、
利用中のパラメータ初期値を管理したり、パラメータの修正漏れが無い様に
チェックしたりしています。


■MainActivity.java(Javaファイル)

package com.hidefumi.kasahara.evel_slayer;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.Window;

public class MainActivity extends Activity {
private final int REQUEST_CODE = 111;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
//YesNoDialog実装
private void showYesNoDialog(MainActivity mainActivity, String title,
String text, DialogInterface.OnClickListener listener) {
AlertDialog.Builder ad=new AlertDialog.Builder(mainActivity);
ad.setTitle(title);
ad.setMessage(text);
ad.setPositiveButton("YES", listener);
ad.setNegativeButton("NO", listener);
ad.show();
}
//ShowDialog実装
private void showDialog(MainActivity mainActivity, String title,String text) {
AlertDialog.Builder ad=new AlertDialog.Builder(mainActivity);
ad.setTitle(title);
ad.setMessage(text);
ad.setPositiveButton("OK", null);
ad.show();
}

//「開始」コマンド実行処理
public void onClick1(View view){
Intent intent = new Intent(MainActivity.this, SubActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}

//「説明」コマンド実行処理
public void onClick2(View view){
Intent intent = new Intent();
intent.setClassName("com.hidefumi.kasahara.evel_slayer", "com.hidefumi.kasahara.evel_slayer.HelpActivity");
startActivity(intent);
}
//「図鑑」コマンド実行処理
public void onClick3(View view){
Intent intent = new Intent();
intent.setClassName("com.hidefumi.kasahara.evel_slayer", "com.hidefumi.kasahara.evel_slayer.BookActivity");
startActivity(intent);
}
//「初期化」コマンド実行処理
public void onClick4(View view){
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
showYesNoDialog(this, "初期化", "これまでのデータを消去しますか?",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which==DialogInterface.BUTTON_POSITIVE){
showDialog(MainActivity.this,"初期化","データを初期化しました");
Editor editor = sharedPreferences.edit();
editor.putInt("EV_MAIN", 0);
editor.putInt("STAGE", 1);
editor.putInt("AREA", 1);
editor.putInt("MAJIN", 0);
editor.putInt("LV", 1);
editor.putInt("EXP", 0);
editor.putInt("HP", 20);
editor.putInt("MHP", 20);
editor.putInt("SP", 10);
editor.putInt("MSP", 10);
editor.putInt("ATK", 2);
editor.putInt("WEPON", 1);
editor.putInt("SKILL_LV", 1);
editor.putInt("SKILL_EXP", 0);
editor.putInt("DRAG", 3);
editor.putInt("STONE", 3);
editor.putInt("LIFE", 0);
editor.putInt("GOLD", 100);
editor.putInt("EXP", 0);
editor.putInt("SEL_FAM", 1);
editor.putInt("FMLA", 0);
editor.putInt("FAM01", 0);
editor.putInt("FAM02", 0);
editor.putInt("FAM03", 0);
editor.putInt("FAM04", 0);
editor.putInt("FAM05", 0);
editor.putInt("FAM06", 0);
editor.putInt("FAM07", 0);
editor.putInt("FAM08", 0);
editor.putInt("FAM09", 0);
editor.putInt("FAM10", 0);
editor.putInt("FAM11", 0);
editor.putInt("FAM12", 0);
editor.putInt("FAM13", 0);
editor.putInt("FAM14", 0);
editor.putInt("FAM15", 0);
editor.putInt("FAM16", 0);
editor.putInt("FAM17", 0);
editor.putInt("FAM18", 0);
editor.putInt("SKILL_LV_MOB01", 1);
editor.putInt("SKILL_LV_MOB02", 1);
editor.putInt("SKILL_LV_MOB03", 1);
editor.putInt("SKILL_LV_MOB04", 1);
editor.putInt("SKILL_LV_MOB05", 1);
editor.putInt("SKILL_LV_MOB06", 1);
editor.putInt("SKILL_LV_MOB07", 1);
editor.putInt("SKILL_LV_MOB08", 1);
editor.putInt("SKILL_LV_MOB09", 1);
editor.putInt("SKILL_LV_MOB10", 1);
editor.putInt("SKILL_LV_MOB11", 1);
editor.putInt("SKILL_LV_MOB12", 1);
editor.putInt("SKILL_LV_MOB13", 1);
editor.putInt("SKILL_LV_MOB14", 1);
editor.putInt("SKILL_LV_MOB15", 1);
editor.putInt("SKILL_LV_MOB16", 1);
editor.putInt("SKILL_LV_MOB17", 1);
editor.putInt("SKILL_LV_MOB18", 1);
editor.putInt("SKILL_EXP_MOB01", 0);
editor.putInt("SKILL_EXP_MOB02", 0);
editor.putInt("SKILL_EXP_MOB03", 0);
editor.putInt("SKILL_EXP_MOB04", 0);
editor.putInt("SKILL_EXP_MOB05", 0);
editor.putInt("SKILL_EXP_MOB06", 0);
editor.putInt("SKILL_EXP_MOB07", 0);
editor.putInt("SKILL_EXP_MOB08", 0);
editor.putInt("SKILL_EXP_MOB09", 0);
editor.putInt("SKILL_EXP_MOB10", 0);
editor.putInt("SKILL_EXP_MOB11", 0);
editor.putInt("SKILL_EXP_MOB12", 0);
editor.putInt("SKILL_EXP_MOB13", 0);
editor.putInt("SKILL_EXP_MOB14", 0);
editor.putInt("SKILL_EXP_MOB15", 0);
editor.putInt("SKILL_EXP_MOB16", 0);
editor.putInt("SKILL_EXP_MOB17", 0);
editor.putInt("SKILL_EXP_MOB18", 0); editor.commit();
} else if (which==DialogInterface.BUTTON_NEGATIVE){
showDialog(MainActivity.this,"初期化","初期化をキャンセルしました");
}
}
}
);
}
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   finish();
}
}
 
■イビル・スレイヤー
https://play.google.com/store/apps/details?id=com.hidefumi.kasahara.evel_slayer

↑このページのトップヘ