Games [XUI2D] Tank Example

Erel

B4X founder
Staff member
Licensed User
Longtime User

A simple example with a tank that moves and shoots.

The interesting point in this example is the usage of RevoluteJoint to move and control the cannon.
1. Limits are set to limit the cannon movement.
2. The joint motor is enabled all the time and we just need to change the target speed to move or hold the cannon:
B4X:
If UpDown Then
   revjoint.MotorSpeed = 1
Else If DownDown Then
   revjoint.MotorEnabled = True
   revjoint.MotorSpeed = -1
Else
   revjoint.MotorSpeed = 0
   Kane.Body.AngularVelocity = 0
End If

By setting the MotorSpeed to 0 the joint motor prevents the cannon from moving when forces act on the tank.

Code to fire a cannon ball:
B4X:
Private Sub Fire
   Dim template As X2TileObjectTemplate = TileMap.GetObjectTemplateByName(ObjectLayer, "bullet")
   template.BodyDef.Position = Kane.Body.Position
   'set the velocity direction based on the cannon direction.
   template.BodyDef.LinearVelocity = Kane.Body.Transform.MultiplyRot(X2.CreateVec2(30, 0))
   'better simulation of fast moving objects
   template.BodyDef.Bullet = True
   TileMap.CreateObject(template)
End Sub

To prevent the bullet from hitting the "kane" object the bullet category bits is set to 2 and the kane mask bits are set to 65533 (65535 - 2). Category bits must be a power of 2.


As usual all the game objects are defined with a tiled map:

SS-2018-08-27_09.41.04.png


If you don't see the custom properties in the Tiled editor then you need to import the custom properties.

The example is included in the example pack.
https://www.b4x.com/android/forum/threads/xui2d-example-pack.96454/
 

sorex

Expert
Licensed User
Longtime User
what is the meaning of this objecttypes.json file?

was it created with Tiled aswell?

I see an object editor under view but there I can only load xml files
 

sorex

Expert
Licensed User
Longtime User
for some reason it's not working here.

I need to type in the filename because it will only list xml files but then it gives the error that it doesn't contains object types.

not really a problem for now as I can see it in the json.
 

sorex

Expert
Licensed User
Longtime User
I guess it's the right window. And the filename is correct otherwise it gives another error.

objects.gif
 

sorex

Expert
Licensed User
Longtime User
the help>about gives 0.17.0

wait a minute copyright date is 2016 altho I downloaded and installed the latest.

let me verify something
 

sorex

Expert
Licensed User
Longtime User
sorry about that.

it appeared that I was still running the old one as it installed the new one in a different folder.
 
Top