【问题标题】:JavaFx Turn-based Multiplayer Application with Google Service带有 Google 服务的 JavaFx 回合制多人游戏应用程序
【发布时间】:2015-10-10 15:22:27
【问题描述】:

现在我正在使用 Gluon 插件,开始使用 JavaFXPorts 非常有帮助。我已经准备好我的应用程序,我可以在电脑和手机上使用它。我在带有系统android的手机上进行了测试。应用程序运行良好,但仅适用于我的服务器。

我会处理不同的分辨率,我认为现在还不错。

我想要回合制多人游戏应用程序,但我在使用 Google Play 服务方面仍有很大问题>。展示如何为turn-based multiplayer application 使用此服务的教程是用纯 android 编写的并使用 Activity。我的问题可能很简单,但如果我有来自“fxml”的应用程序视图,如何将其用作教程活动?

我想为我的应用程序进行自动匹配,接下来我想覆盖方法 takeTurn() 以使其适合我的应用程序。

例如,我怎样才能将这样的东西(下面的代码)更改为 JavaFX 中的应用程序?

除了 AndroidPlatformProvider.java 之外,我还必须使用 JavaFX(src/main/java 文件夹) 类中的 google 服务,并且所有方法都必须在 src/android/java 中文件夹。我知道我必须使用 PlatformServicePlatformProvider。我按照以下示例进行操作:HelloPlatform SMSTracker

我使用接口 PlatformProvider 中的方法,但应用程序仍然崩溃。 :(

我只使用我的 JavaFX 代码中的 Provider 并且我没有 android Activity。我不知道如何在没有 Activity 或 View 的情况下使用这些方法:

- public void onActivityResult(int request, int response, Intent data)

- public void playTurn(View view)

我可以从 google 服务方法调用我的控制器中的方法来查看 (fxml)。 我不知道这些方法应该如何与 JavaFX 一起使用。

public class TbmpGameActivity extends Activity {
...

@Override
public void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);
    ...

    if (request == RC_SELECT_PLAYERS) {
        if (response != Activity.RESULT_OK) {
            // user canceled
            return;
        }

        // Get the invitee list.
        final ArrayList<String> invitees =
                data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);

        // Get auto-match criteria.
        Bundle autoMatchCriteria = null;
        int minAutoMatchPlayers = data.getIntExtra(
                Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
        int maxAutoMatchPlayers = data.getIntExtra(
                Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
        if (minAutoMatchPlayers > 0) {
            autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
                    minAutoMatchPlayers, maxAutoMatchPlayers, 0);
        } else {
            autoMatchCriteria = null;
        }

        TurnBasedMatchConfig tbmc = TurnBasedMatchConfig.builder()
                .addInvitedPlayers(invitees)
                .setAutoMatchCriteria(autoMatchCriteria)
                .build();

        // Create and start the match.
        Games.TurnBasedMultiplayer
            .createMatch(mGoogleApiClient, tbmc)
            .setResultCallback(new MatchInitiatedCallback());
    }
}
}

或类似的东西:

// Call this method when a player has completed his turn and wants to
// go onto the next player, which may be himself.
public void playTurn(View view) {

    // Get the next participant in the game-defined way, possibly round-robin.
    String nextParticipantId = getNextParticipantId();

    // Get the updated state. In this example, we simply retrieve a
    // text string from the view. In your game, there may be more
    // complicated state.
    mTurnData = mDataView.getText().toString();

    // At this point, you might want to show a waiting dialog so that
    // the current player does not try to submit turn actions twice.
    showSpinner();

    // Invoke the next turn. We are converting our data to a byte array.
    Games.TurnBasedMultiplayer
    .takeTurn(mGoogleApiClient, mMatch.getMatchId(),
             mTurnData.getBytes(Charset.forName("UTF-16")),
             nextParticipantId)
    .setResultCallback(this);
}

【问题讨论】:

    标签: javafx google-play-services javafx-8 google-play-games javafxports


    【解决方案1】:

    最新版本的 jfxmobile 插件 (1.0.3) 包括 Dalvik SDK 的最新更改,其中包含 JavaFX 8 到 android 的端口。

    FXActivity类中,增加了一个非常方便的方法来监听intent的结果:setOnActivityResultHandler()

    这意味着您不应向您的应用添加新活动,而只能使用FXActivity。您应该为其添加一个 Intent,并为 FXActivity 设置一个适当的结果处理程序。

    在 Gluon 的网站上查看最近的post。它解释了如何访问原生服务,基于使用设备的相机拍照并等待获得结果以恢复应用的情况。

    基本上,这就是这种情况下所需要的:

    public void takePicture() {
        // create intent
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        ...
    
        // Add result handler 
        FXActivity.getInstance().setOnActivityResultHandler((requestCode, resultCode, data) -> {
            if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
                ...
            }
        });
    
        // launch activity
        FXActivity.getInstance().startActivityForResult(intent, TAKE_PICTURE);
    }
    

    在您的应用中尝试这种方法。

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 2013-07-17
      • 2015-12-21
      • 2013-05-19
      • 2010-11-02
      相关资源
      最近更新 更多