1

ごあいさつ Stackoverflowians

私はAndroidカードゲームアプリに取り組んでいます。ゲームの特定の State にいる 2 人の Player のいずれかが異なるアクションを利用できることを考慮して、State Design Pattern (State Machine) を使用して実装した GameClass にゲーム全体のダイナミクスを組み込みました。

たとえば、ゲームの開始時に、プレーヤー A は、GameClass が持つ合計 21 個のメソッドのうち 5 個しか実行できません。どのアクションとカードがプレイされるかに応じて、メソッドはさまざまな状態に移行し、さまざまな状態ごとにさまざまなメソッドが利用可能になります。

したがって、各メソッドのブール値を設定および取得する CurrentActions というクラスを使用して、さまざまなメソッドの可用性を解決しました。したがって、プレイヤーがゲームの開始時に選択できるアクションをプレイヤーに示したい場合は、一連の IF (約 21...) を使用して、Getter のいずれかが true に設定されているかどうかを確認します。ボタンをレイアウトに追加すると、すべてが魅力的に機能します。

さて、私の最大の問題は、最初の状態のボタンを表示する方法がわからないことです。その後、プレーヤー A がそれらのボタン (メソッド) の 1 つをクリックすると、以前の状態で使用可能だったすべてのボタンがフラッシュされ、現在表示されています。ゲームの新しい状態の新しいボタン。

これを行う方法はありますか?私はすでに Layout.removeAllViews() メソッドを使用してすべてのボタンをクリアしていますが、次の State に従ってすべてのボタンを再度設定することが私の最大の問題です。この件に関する何らかのガイダンスをいただければ幸いです。

コードを表示したいというあなたのリクエストに応えて、ゲーム、レイアウト、ボタン、および可能なアクションを処理するコードをアクティビティに追加しました。

// CurrentActions CA holds the current actions that are available 
//for the current State of the game.
final CurrentActions CA = new CurrentActions();

//JuegoTruco JT is the game itself

    final JuegoTruco JT = new JuegoTruco(CA);

//I've changed the names up to make it understandable
//If Action1 is performable at the current State of the game, set the button up and
//await a posible Click 
    if(CA.performAction1())
    {
        Action1Button = new Button(this);
        Action1Button.setId(13);
        Action1Button.setLayoutParams(paramsActions);
        Action1Button.setText("Action1");
        layoutAcciones.addView(Action1Button);
        Action1Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TOGGLE_ON_CLICK) {
                    mpButton.start();

//Here I change the state of JT, by performing an action and 
//I also give it instance CA, so the State Handler
//can change the available Actions for the following state

                    JT.performAction(otherParameters, CA);
                    layoutActions.removeAllViews();

                }
            }
        });
    }
if(CA.performAction2())
    {
        Action2Button = new Button(this);
        Action2Button.setId(13);
        Action2Button.setLayoutParams(paramsActions);
        Action2Button.setText("Action2");
        layoutAcciones.addView(Action2Buttonn);
        Action2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TOGGLE_ON_CLICK) {
                    mpButton.start();
                    JT.performAction2(OtherParameters, CA);
                    layoutActions.removeAllViews();

                }
            }
        });
    }

ゲーム内に存在するすべてのアクションに対して、これらの IF の 1 つを用意します。だから....(ゲームの状態に応じて)利用可能なボタンの1つをクリックした後、actionsLayoutをクリアしてボタンの新しいセットを実行し、ボタンを押すたびにこれを継続的に実行する方法はありますか(つまり、状態が変化するたびに)?

4

1 に答える 1

0

最も簡単なアプローチは、アクティビティに onCreate() の状態を決定させ、それに応じて有効なボタンを作成させることだと思います (それはまさにあなたがしていることのように聞こえます)。

ここで、既存のビューを操作する代わりに (複雑さが増すにつれて面倒になる可能性があります)、単にアクティビティを再起動し、以前と同じコードでゲームを再作成します。

// Move to new state
startActivity(new Intent(MainActivity.this, MainActivity.class));

これにより、ユーザーが戻るボタンを押すと問題が発生する可能性があります。戻ると正しい状態が表示されますが、動作が混乱する可能性があります。少なくともそれが私の意見です。これを防ぐには:

@Override
public void onBackPressed() {
    // confirm exit being a dialog, this just mocks the intent of the code
    if (confirmExit()) {
          Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
    }
}

Eyecandy: アクティビティ間のトランジションを定義して、少しクールに見せることができます。

MainActivity の onCreate() での左から右への遷移の例:

overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

/res/anim/slide_in_right.xml と anim.slide_out_left.xml は次のとおりです。

/// in_right
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

<translate
    android:duration="200"
    android:fromXDelta="100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

</set>

// out_left
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

<translate
    android:duration="200"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="-100%"
    android:toYDelta="0%" />

</set>

もちろん、この質問には多くの答えがあります。正直なところ、これは私が考えることができるプログラミング、読み取り、および保守が最も簡単だと思います。

フラグメント:このアプローチは、後でフラグメントを使用して改善できます。これにより、Ajax がサーバーベースの Web サイトに行うのと同じ改善がアプリにもたらされます (これは、私の考えでは最も適切なアナロジーです)。ただし、これについては、この記事で掘り下げる範囲外です。言及したかっただけです。

最後の注意として、ソリューションへの私の提案は、アクティビティからビューを追加/削除することをいじるよりも、将来フラグメントベースに移行する方がはるかに苦痛が少なくなります。

于 2013-09-27T18:26:33.390 に答える