Games World.CreateBody()

ilan

Expert
Licensed User
Longtime User
why i cannot create a body like this:

B4X:
Sub createBody
    Dim playerShape As B2CircleShape
    playerShape.Initialize(5)
   
    Dim playerFixtureDef As B2FixtureDef
    playerFixtureDef.Shape = playerShape
    playerFixtureDef.Density = 1
    playerFixtureDef.Restitution = 0.3
    playerFixtureDef.Friction = 0.4
 
    Dim playerDef As B2BodyDef
    playerDef.BodyType = playerDef.TYPE_DYNAMIC
    playerDef.Position.Set(0,0)
   
    Dim playerBody As B2Body
    playerBody = world.CreateBody(playerDef)
    playerBody.CreateFixture(playerFixtureDef)
End Sub

i know i can create a body via Tiled and X2BodyWrapper but why is this code that works in libgdx and in other frameworks that use box2d is working but using xui2d it is not?

Waiting for debugger to connect...
Program started.
Size scale: 1.07
1
1
*** MainLoop starting. ResumableIndex = 1
Error occurred on line: 131 (X2Utils)
java.lang.NullPointerException
at b4j.example.x2utils$ResumableSub_MainLoop.resume(x2utils.java:1618)
at b4j.example.x2utils._mainloop(x2utils.java:1381)
at b4j.example.x2utils._start(x2utils.java:317)
at b4j.example.game._startgame(game.java:129)
at b4j.example.main._appstart(main.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.start(main.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
but why is this code that works in libgdx and in other frameworks that use box2d is working but using xui2d it is not?
The error happens because you are not working with X2 framework correctly. Don't "fight" with the X2 framework. If you want to use it then you should follow its guidelines.
It is very simple to create bodies programmatically. You need to call X2.CreateBodyAndWrapper instead of calling World.CreateBody:

B4X:
 Dim playerBody As X2BodyWrapper = X2.CreateBodyAndWrapper(playerDef, Null, "player")
 'playerBody.Body will return the B2Body object.

If you want to attach graphics to this body: https://www.b4x.com/android/forum/threads/xui2d-image-body-different-size.105212/#post-659241
 

ilan

Expert
Licensed User
Longtime User
Don't "fight" with the X2 framework. If you want to use it then you should follow its guidelines.

sorry, i think i was misunderstood. i am not fighting x2 and i do want to work with it.


so basically this is the way to create a body and attach a graphic to it, right? can I add the sprites anywhere in the game cycle to the cache or do I need to do it in the initialize process?

thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
so basically this is the way to create a body and attach a graphic to it, right?
Yes.
If you want to play with the X2 framework then a good way to do it is by modifying the Game class of X2 Source "example".
The error that you encountered happened here (X2Utils.MainLoop):
B4X:
For Each body As B2Body In AllBodies
           Dim bw As X2BodyWrapper = body.Tag
           bw.IsVisible = VisibleBodies.ContainsKey(body)
           If bw.IsVisible Then
               bw.Tick(gs)
           Else
               If bw.TickIfInvisible Then
                   bw.Tick(gs)
               Else If bw.DestroyIfInvisible Then
                   bw.Delete(gs)
               End If
           End If
       Next
You can see that each B2Body is expected to have a X2BodyWrapper in its Tag property.

can I add the sprites anywhere in the game cycle to the cache
Yes.
 
Top