所以我的LibGDX项目在桌面和Android上运行完全正常,但是当我实现一个测量玩家得分的浮点数时,游戏将在android上崩溃。这个想法是,每次屏幕向上滚动时分数会降低,而当屏幕向下滚动时分数会增加。我正在使用gesturedetector控制器进行滚动。这是主要代码:
public class WorldScreen implements Screen{
private Texture bgCity;
private Texture bgLoop;
private Texture hud;
public static OrthographicCamera camera;
SpriteBatch batch;
Rectangle player;
Rectangle background;
Rectangle backgroundloop;
public float camx = 0;
public float camy = 0;
public static float score = 384400000;
GestureListenerC controller;
public WorldScreen(final JetpackGame aa) {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camx = camera.viewportWidth / 2f;
camy = camera.viewportHeight / 2f;
controller.update();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(bgCity, background.x, background.y);
if(background.y <= -3296){
if(backgroundloop.y <= -800) backgroundloop.y = 0;
}
if(camy >= 100) batch.draw(bgLoop, backgroundloop.x, backgroundloop.y);
if(camera.position.y >= 5360) camera.position.y = 4420;
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
controller = new GestureListenerC();
GestureDetector gestureDetector = new GestureDetector(20, 0.5f, 2, 0.15f, controller);
Gdx.input.setInputProcessor(gestureDetector);
bgCity = new Texture(Gdx.files.internal("img/city_BG.png"));
bgLoop = new Texture(Gdx.files.internal("img/loopBG.png"));
hud = new Texture(Gdx.files.internal("img/hud.png"));
camera = new OrthographicCamera();
camera.setToOrtho(false, 420,800);
batch = new SpriteBatch();
player = new Rectangle();
player.x = 420/2;
player.y = 800/2;
background = new Rectangle();
background.x = 0;
background.y = 0;
background.width = 479;
background.height = 4096;
backgroundloop = new Rectangle();
backgroundloop.x = 0;
backgroundloop.y = 4096;
backgroundloop.width = 512;
backgroundloop.height = 1024;
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
batch.dispose();
bgCity.dispose();
bgLoop.dispose();
hud.dispose();
}
}
这是控制器类:
public class GestureListenerC implements GestureListener{
public static float velX, velY;
public static boolean flinging = false;
float initialScale = 1;
public boolean touchDown (float x, float y, int pointer, int button) {
flinging = false;
return false;
}
@Override
public boolean tap (float x, float y, int count, int button) {
return false;
}
@Override
public boolean longPress (float x, float y) {
return false;
}
@Override
public boolean fling (float velocityX, float velocityY, int button) {
flinging = true;
velX = WorldScreen.camera.zoom * velocityX * 0.5f;
velY = WorldScreen.camera.zoom * velocityY * 0.5f;
return false;
}
@Override
public boolean pan (float x, float y, float deltaX, float deltaY) {
WorldScreen.camera.position.y += deltaY;
WorldScreen.score -= deltaY;
return false;
}
@Override
public boolean panStop (float x, float y, int pointer, int button) {
return false;
}
@Override
public boolean zoom (float originalDistance, float currentDistance) {
return false;
}
@Override
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
return false;
}
public void update () {
if (flinging) {
velY *= 0.98f;
WorldScreen.score -= 0.2f * velY;
WorldScreen.camera.position.add(0, velY * Gdx.graphics.getDeltaTime(), 0);
if (Math.abs(velY) < 0.01f) velY = 0;
}
}
}
这就是logcat所说的:
01-30 00:08:04.096 17444-17444/com.vahlaville.game.android I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
01-30 00:08:04.096 17444-17444/com.vahlaville.game.android I/SELinux﹕ Function: selinux_android_load_priority , priority version is VE=SEPF_GT-N7105_4.4.2_0016
01-30 00:08:04.096 17444-17444/com.vahlaville.game.android I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
01-30 00:08:04.101 17444-17444/com.vahlaville.game.android D/dalvikvm﹕ Late-enabling CheckJNI
01-30 00:08:04.246 17444-17444/com.vahlaville.game.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.vahlaville.game.android-1/libgdx.so 0x4225e6d0
01-30 00:08:04.246 17444-17444/com.vahlaville.game.android D/dalvikvm﹕ Added shared lib /data/app-lib/com.vahlaville.game.android-1/libgdx.so 0x4225e6d0
01-30 00:08:04.246 17444-17444/com.vahlaville.game.android D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.vahlaville.game.android-1/libgdx.so 0x4225e6d0, skipping init
01-30 00:08:04.246 17444-17444/com.vahlaville.game.android W/dalvikvm﹕ No implementation found for native Ldalvik/system/VMRuntime;.pauseGc:(Ljava/lang/String;)I
01-30 00:08:04.251 17444-17444/com.vahlaville.game.android E/ActivityThread﹕ Pause GC
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
at dalvik.system.VMRuntime.pauseGc(Native Method)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
01-30 00:08:04.281 17444-17444/com.vahlaville.game.android D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
01-30 00:08:04.301 17444-17444/com.vahlaville.game.android D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
01-30 00:08:04.316 17444-17444/com.vahlaville.game.android D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
01-30 00:08:04.321 17444-17444/com.vahlaville.game.android E/﹕ Device driver API match
Device driver API version: 23
User space API version: 23
01-30 00:08:04.326 17444-17444/com.vahlaville.game.android E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
01-30 00:08:04.386 17444-17444/com.vahlaville.game.android I/AndroidInput﹕ sensor listener setup
01-30 00:08:04.456 17444-17444/com.vahlaville.game.android E/﹕ Device driver API match
Device driver API version: 23
User space API version: 23
01-30 00:08:04.456 17444-17444/com.vahlaville.game.android E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
01-30 00:08:04.521 17444-17444/com.vahlaville.game.android D/OpenGLRenderer﹕ Enabling debug mode 0
01-30 00:08:04.546 17444-17468/com.vahlaville.game.android W/GL2JNIView﹕ creating OpenGL ES 2.0 context
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.vahlaville.game.android-1/libgdx.so 0x4225e6d0
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android D/dalvikvm﹕ Shared lib '/data/app-lib/com.vahlaville.game.android-1/libgdx.so' already loaded in same CL 0x4225e6d0
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/GL2﹕ all initialized 2
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ OGL renderer: Mali-400 MP
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ OGL vendor: ARM
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ OGL version: OpenGL ES 2.0
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ OGL extensions: GL_EXT_debug_marker GL_OES_texture_npot GL_OES_compressed_ETC1_RGB8_texture GL_OES_standard_derivatives GL_OES_EGL_image GL_OES_depth24 GL_ARM_rgba8 GL_ARM_mali_shader_binary GL_OES_depth_texture GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 GL_EXT_blend_minmax GL_OES_EGL_image_external GL_OES_EGL_sync GL_OES_rgb8_rgba8 GL_EXT_multisampled_render_to_texture GL_EXT_discard_framebuffer GL_OES_get_program_binary GL_ARM_mali_program_binary GL_EXT_shader_texture_lod GL_EXT_robustness GL_OES_depth_texture_cube_map
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ framebuffer: (5, 6, 5, 0)
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ depthbuffer: (24)
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ stencilbuffer: (8)
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ samples: (0)
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ coverage sampling: (false)
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ Managed meshes/app: { }
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ Managed textures/app: { }
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ Managed shaders/app: { }
01-30 00:08:04.606 17444-17468/com.vahlaville.game.android I/AndroidGraphics﹕ Managed buffers/app: { }
01-30 00:08:04.641 17444-17468/com.vahlaville.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 184K, 10% free 8392K/9312K, paused 22ms, total 22ms
01-30 00:08:04.641 17444-17468/com.vahlaville.game.android I/dalvikvm-heap﹕ Grow heap (frag case) to 9.494MB for 610323-byte allocation
01-30 00:08:04.656 17444-17444/com.vahlaville.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 10% free 8988K/9912K, paused 16ms, total 16ms
01-30 00:08:05.206 17444-17468/com.vahlaville.game.android W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x4198dc08)
01-30 00:08:05.211 17444-17468/com.vahlaville.game.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 3970
Process: com.vahlaville.game.android, PID: 17444
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: img/hud.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.vahlaville.game.screens.WorldScreen.show(WorldScreen.java:128)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.vahlaville.game.JetpackGame.create(JetpackGame.java:11)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:237)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: img/hud.png (Internal)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.vahlaville.game.screens.WorldScreen.show(WorldScreen.java:128)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.vahlaville.game.JetpackGame.create(JetpackGame.java:11)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:237)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
Caused by: java.io.FileNotFoundException: img/hud.png
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:316)
at android.content.res.AssetManager.open(AssetManager.java:290)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.vahlaville.game.screens.WorldScreen.show(WorldScreen.java:128)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.vahlaville.game.JetpackGame.create(JetpackGame.java:11)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:237)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
01-30 00:08:05.261 17444-17444/com.vahlaville.game.android I/AndroidInput﹕ sensor listener tear down
01-30 00:08:05.261 17444-17444/com.vahlaville.game.android I/AndroidGraphics﹕ Managed meshes/app: { }
01-30 00:08:05.261 17444-17444/com.vahlaville.game.android I/AndroidGraphics﹕ Managed textures/app: { }
01-30 00:08:05.261 17444-17444/com.vahlaville.game.android I/AndroidGraphics﹕ Managed shaders/app: { }
01-30 00:08:05.261 17444-17444/com.vahlaville.game.android I/AndroidGraphics﹕ Managed buffers/app: { }
01-30 00:13:05.336 17444-17468/com.vahlaville.game.android I/Process﹕ Sending signal. PID: 17444 SIG: 9
答案 0 :(得分:1)
这可能不是问题的原因,但您的logcat输出显示加载资源时出错:
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: img/hud.png
我会检查一下。
答案 1 :(得分:1)
我不知道它是否有帮助,但试着回顾一下,这一行:
hud = new Texture(Gdx.files.internal("img/hud.png"));
引起:com.badlogic.gdx.utils.GdxRuntimeException:读取错误 file:img / hud.png(内部)
审核,区分大小写,文件名和外观是文件的副本,而不是链接。
答案 2 :(得分:1)
hud = new Texture(Gdx.files.internal("img/hud.png"));
检查您是否已将图像文件“hud.png”放入Android项目资产文件夹资源中的img文件夹中
希望这会有所帮助!