Games World.CreateBody()

Discussion in 'Game Development' started by ilan, Aug 7, 2019.

  1. ilan

    ilan Expert Licensed User

    why i cannot create a body like this:

    Code:
    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?

     
  2. Erel

    Erel Administrator Staff Member Licensed User

    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:

    Code:
    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
     
  3. ilan

    ilan Expert Licensed User

    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    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):
    Code:
    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.

    Yes.
     
    Peter Simpson likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice