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:

    Sub createBody
    Dim playerShape As B2CircleShape
    Dim playerFixtureDef As B2FixtureDef
        playerFixtureDef.Shape = playerShape
        playerFixtureDef.Density = 
        playerFixtureDef.Restitution = 
        playerFixtureDef.Friction = 
    Dim playerDef As B2BodyDef
        playerDef.BodyType = playerDef.TYPE_DYNAMIC
    Dim playerBody As B2Body
        playerBody = world.CreateBody(playerDef)
    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:

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

    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):
    For Each body As B2Body In AllBodies
    Dim bw As X2BodyWrapper = body.Tag
               bw.IsVisible = VisibleBodies.ContainsKey(body)
    If bw.IsVisible Then
    If bw.TickIfInvisible Then
    Else If bw.DestroyIfInvisible Then
    End If
    End If
    You can see that each B2Body is expected to have a X2BodyWrapper in its Tag property.

    Peter Simpson likes this.
  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