我目前正在使用 android、libgdx、平铺地图和 AppWarp 的多人游戏 API 为 android 和桌面开发 2d 横向滚动条。我正在尝试实现 App42API 的用户系统,从中我可以将他们的 api 连接到我的游戏,从而启用多人游戏功能。我编写了一个注册脚本,它应该注册一个用户帐户并将其发送到 AppWarp 的 Cloud DB,但事实并非如此。当我运行我的游戏时,将所需的文本放入文本字段(用于创建帐户的输入)后,我收到以下错误:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoClassDefFoundError: org/json/JSONException
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
Caused by: java.lang.NoClassDefFoundError: org/json/JSONException
at com.mygdx.game.network.Register.registerAccount(Register.java:160)
at com.mygdx.game.network.Register$1.clicked(Register.java:122)
at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:89)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:348)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:306)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 8 more
我猜它没有正确连接到 AppWarp 的 api,但我不确定如何修复它。下面是我的 android 启动器类和我的注册类的代码。
** 我读到您必须通过在程序中调用它来初始化 app42api sdk。**
安卓启动器:
package com.mygdx.game.android;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.mygdx.game.MainGameLoop;
import com.mygdx.game.network.App42Handler;
import com.shephertz.app42.gaming.multiplayer.client.WarpClient;
import com.shephertz.app42.paas.sdk.android.App42API;
@SuppressWarnings("unused")
public class AndroidLauncher extends AndroidApplication
{
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// initialize multiplayer api taking in my api key and secret key
App42API.initialize(App42API.appContext, App42Handler.apiKey, App42Handler.secretKey);
initialize(new MainGameLoop(), config);
}
}
注册.java:
package com.mygdx.game.network;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.TextInputListener;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.shephertz.app42.gaming.multiplayer.client.WarpClient;
import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42CallBack;
import com.shephertz.app42.paas.sdk.android.user.User;
import com.shephertz.app42.paas.sdk.android.user.UserService;
public class Register implements Screen, TextInputListener
{
private TextField usernameTxtField, passwordTxtField, confirmpassTxtField, emailTxtField;
private TextFieldStyle txtFieldStyle;
private BitmapFont font;
private Stage stage;
private Table table;
private TextButton registerBtn;
private TextureAtlas registerAtlas;
private Skin registerSkin;
@Override
public void render(float delta)
{
Gdx.gl.glClearColor(0,1,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
@Override
public void show()
{
stage = new Stage();
// set input processor to stage element
Gdx.input.setInputProcessor(stage);
table = new Table();
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
font = new BitmapFont();
txtFieldStyle = new TextFieldStyle();
txtFieldStyle.fontColor = Color.RED;
txtFieldStyle.font = font;
// initialize new text fields
usernameTxtField = new TextField("", txtFieldStyle);
passwordTxtField = new TextField("", txtFieldStyle);
confirmpassTxtField = new TextField("", txtFieldStyle);
emailTxtField = new TextField("", txtFieldStyle);
// set size of text fields
usernameTxtField.setSize(100, 25);
passwordTxtField.setSize(100, 25);
confirmpassTxtField.setSize(100, 25);
emailTxtField.setSize(100, 25);
// set position of text fields
usernameTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 50);
passwordTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 100);
confirmpassTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 150);
emailTxtField.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 200);
// passproof it
passwordTxtField.setPasswordMode(true);
confirmpassTxtField.setPasswordMode(true);
// register button
registerAtlas = new TextureAtlas("resmenu/menu/registerBtn.pack");
registerSkin = new Skin(registerAtlas);
// new style for exit btn
TextButtonStyle registerButtonStyle = new TextButtonStyle();
// when user clicks on button, load new image, when he lets go reload
// original image
registerButtonStyle.up = registerSkin.getDrawable("menuRegisterBtn");
registerButtonStyle.down = registerSkin.getDrawable("menuRegisterBtnPressed");
// off set btn
registerButtonStyle.pressedOffsetX = 1;
registerButtonStyle.pressedOffsetY = -1;
registerButtonStyle.font = font;
registerBtn = new TextButton("", registerButtonStyle);
// add new listener
registerBtn.addListener(new ClickListener()
{ // fire event
public void clicked(InputEvent event, float x, float y)
{
// register account and take in user input from txt fields
if(usernameTxtField.getText().equals("") || passwordTxtField.getText().equals("")
|| confirmpassTxtField.getText().equals("") || emailTxtField.getText().equals(""))
{
System.out.println("fields cannot be empty");
}
else
{
registerAccount(usernameTxtField.getText(), passwordTxtField.getText(),
confirmpassTxtField.getText(), emailTxtField.getText());
}
}
});
registerBtn.pad(20);
table.bottom();
table.row();
table.add(usernameTxtField);
table.row();
table.add(passwordTxtField);
table.row();
table.add(confirmpassTxtField);
table.row();
table.add(emailTxtField);
table.row();
table.add(registerBtn);
table.debug();
stage.addActor(table);
}
// register account into app42api cloud db
public void registerAccount(String username, String password, String confirmpass, String email)
{
// register account
String CUsername = username;
String CPassword = password;
String CConfirmPass = confirmpass;
String CEmail = email;
// connect to api
WarpClient.initialize(App42Handler.apiKey, App42Handler.secretKey);
// build user service
UserService userService = App42API.buildUserService();
// create user using user input and fire callback (event)
if (CPassword.equals(CConfirmPass))
{
userService.createUser(CUsername, CPassword, CEmail, new App42CallBack()
{
public void onSuccess(Object response)
{
User user = (User) response;
System.out.println("successfully registered: " + user.getUserName());
System.out.println("email is " + user.getEmail());
}
public void onException(Exception ex)
{
System.out.println("Exception Message: " + ex.getMessage());
}
});
}
else
{
System.out.println("Passwords need to match!!");
}
}
@Override
public void input(String text)
{
// TODO Auto-generated method stub
}
@Override
public void canceled() {
// TODO Auto-generated method stub
}
@Override
public void resize(int width, int height)
{
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose()
{
stage.dispose();
}
}
所以我希望我能很好地解释这一点。我只是想创建一个帐户并将其发送到 App42API 的云。如果有人可以帮我一把或指出我正确的方向,那就太好了。没有足够的文档,尽管从我目前所见的情况来看它非常强大。
谢谢,德文