2016-11-15 37 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添加到虛擬世界中。