2016-11-15 39 views
0

我正在学习游戏&应用程序开发和第一学期我们正在构建一个处理游戏。
在我的游戏中,我在图书馆Game Control Plus的帮助下使用PS4控制器。
如果我按下一个按钮足够的时间,我的游戏崩溃,并给出了控制台上输出(插头“Uarma”是执行时,按下按钮,代码的函数)处理3.2.1 Game Control Plus库java异常

java.lang.reflect.InvocationTargetException 
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.gamecontrolplus.Plug.call(Unknown Source) 
    at org.gamecontrolplus.ControlButton.callPlugs(Unknown Source) 
    at org.gamecontrolplus.ControlButton.update(Unknown Source) 
    at org.gamecontrolplus.ControlDevice.update(Unknown Source) 
    at org.gamecontrolplus.ControlIO.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.AssertionError 
    at org.jbox2d.dynamics.World.createBody(World.java:339) 
    at shiffman.box2d.Box2DProcessing.createBody(Box2DProcessing.java:203) 
    at Meon$Bullet.<init>(Meon.java:202) 
    at Meon.Uarma(Meon.java:294) 
    ... 9 more 
java.lang.RuntimeException: Error on calling plug: Uarma 
    at org.gamecontrolplus.Plug.call(Unknown Source) 
    at org.gamecontrolplus.ControlButton.callPlugs(Unknown Source) 
    at org.gamecontrolplus.ControlButton.update(Unknown Source) 
    at org.gamecontrolplus.ControlDevice.update(Unknown Source) 
    at org.gamecontrolplus.ControlIO.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 

我的Java的理解是不是很大,所以任何可能导致此错误的帮助表示赞赏!

在此先感谢!

这里是参与这一进程的代码:

//Variaveis 
ControlIO controlo; 
ControlDevice comando; 

//inicia o ControlIO (vai ver que comandos estao ligados) 
controlo = ControlIO.getInstance(this); 

//procura comandos compativeis 
comando = controlo.getMatchedDevice("playerControl"); 

//associa funçoes a botoes (Botão para Função) 
BpFp1(); //p1 = player 1 

void BpFp1() { 

    comando.getButton("jump").plug(this, "salto", ControlIO.ON_PRESS); 
    comando.getButton("punch").plug(this, "murro", ControlIO.ON_PRESS); 
    comando.getButton("grabWep").plug(this, "Aarma", ControlIO.ON_PRESS); 
    comando.getButton("useWep").plug(this, "Uarma", ControlIO.ON_PRESS); 
} 

void Uarma() { 

    println("usar armas? check"); 
    bullets.add(new Bullet(player1.playerPos.x + 20, player1.playerPos.y, 5, 5)); 
} 

子弹构造:

class Bullet { 

    Vec2 bulletPos; 
    Body bulletbody; 
    float dbulletLarg; 
    float dbulletAlt; 

    Bullet(float bulletX, float bulletY, float bulletLarg, float bulletAlt) { 

    //definir o corpo 
    BodyDef bulletbd = new BodyDef(); 
    bulletbd.type = BodyType.DYNAMIC; 
    bulletbd.bullet = true; 
    bulletbd.position.set(box2d.coordPixelsToWorld(bulletX, bulletY)); 

    //criar o corpo 
    bulletbody = box2d.createBody(bulletbd); 

    //forma 
    PolygonShape bulletps = new PolygonShape(); 
    bulletps.setAsBox(box2d.scalarPixelsToWorld(bulletLarg/2), box2d.scalarPixelsToWorld(bulletAlt/2)); 


    //o que cola a forma ao corpo 
    FixtureDef bulletfd = new FixtureDef(); 
    bulletfd.shape = bulletps; 

    //parametros que afetam a fisica do objeto 
    bulletfd.density = 0; 

    //colar a forma ao corpo 
    bulletbody.createFixture(bulletfd); 

    dbulletLarg = bulletLarg; 
    dbulletAlt = bulletAlt; 

    bulletbody.applyLinearImpulse(new Vec2(100, 0), bulletbody.getWorldCenter(), true); 
    } 

    void display() { 

    bulletPos = box2d.getBodyPixelCoord(bulletbody); 

    pushMatrix(); 
    translate(bulletPos.x, bulletPos.y); 
    rectMode(CENTER); 
    rect(0, 0, dbulletLarg, dbulletAlt); 
    popMatrix(); 
    } 
} 
+0

你为什么从葡萄牙网站删除这个问题? –

回答

0

这是一个线程的问题。 ControlIO有它自己的用于接收输入线,如可通过此来证明:

at org.gamecontrolplus.ControlIO.run(Unknown Source) 
at java.lang.Thread.run(Thread.java:745) 

然而,Box2D的(下处理罩)在自己的线程操纵它的虚拟世界太。你不应该在Box2D使用的线程之外操作虚拟世界。这样做可能会由于无序的线程干扰而导致数据损坏问题。

为了防止这种类型的问题,Box2D锁定其世界以避免意外操作。然而,它使用assert而不是IllegalArgumentExceptionConcurrentModificationException,大多数其他Java框架用于这种检查。因此,您看到从this line of code丢弃AssertionError

不幸的是,这正是你正在做的。当您创建Bullet时,您可以从ControlIO线程执行此操作。 Bullet构造函数尝试将Bullet添加到虚拟世界。 Box2D被冒犯并击中了assert

解决方案是在Uarma方法内不创建Bullet。相反,将某个对象放在某个列表的某个位置(使用正确的同步),该对象表示应在给定位置创建一个Bullet。回到您的Meon实际处理Box2D线程中的虚拟世界的代码中,通过调用其构造函数来消耗列表,将相应的Bullet s添加到虚拟世界中。