The main activity is not relevant as it is not cross platform. The main place for your code is the Game class.can you create an example where everything is in the main activity and you cretate manually the bitmap view and then the body view without tiled or any class that does it automatically??
can you create an example where everything is in the main activity and you cretate manually the bitmap view and then the body view without tiled or any class that does it automatically??
Sub Class_Globals
Private Ground As X2BodyWrapper
Dim blocksize As Int
End Sub
Public Sub Initialize (Parent As B4XView)
Parent.LoadLayout("GameLayout")
X2.Initialize(Me, ivForeground)
world = X2.World
X2.ConfigureDimensions(world.CreateVec2(5, 0),10)
X2.EnableDebugDraw
StartGame
End Sub
Public Sub StartGame
If X2.IsRunning Then Return
X2.UpdateWorldCenter(X2.CreateVec2(X2.ScreenAABB.Width/2,X2.ScreenAABB.Height/2))
X2.Start
blocksize=X2.ScreenAABB.Width/10
Dim sprites As List = X2.ReadSprites(xui.LoadBitmap(File.DirAssets, "crate.png"), 1, 1,blocksize,blocksize)
X2.GraphicCache.PutGraphic("crate", sprites)
CreateGround
Create_Bullet
For y=0 To 9
Create_Crate(0,y*blocksize)
Next
End Sub
Private Sub CreateGround
Dim GroundBox As B2Vec2 = X2.CreateVec2(9.60, 0)
Dim bd As B2BodyDef
bd.BodyType = bd.TYPE_STATIC
bd.Position = X2.CreateVec2(X2.ScreenAABB.Center.X, X2.ScreenAABB.BottomLeft.Y + GroundBox.Y / 2)
Ground = X2.CreateBodyAndWrapper(bd, Null, "Ground")
Dim rect As B2PolygonShape
rect.Initialize
rect.SetAsBox(GroundBox.X / 2, GroundBox.Y / 2)
Ground.Body.CreateFixture2(rect, 1)
End Sub
Private Sub Create_Bullet
Dim bd As B2BodyDef
bd.BodyType = bd.TYPE_DYNAMIC
bd.Position = X2.CreateVec2(0,2)
Dim wrapper As X2BodyWrapper = X2.CreateBodyAndWrapper(bd,Null, "crate")
wrapper.GraphicName = "crate"
Dim size As B2Vec2 = X2.GraphicCache.GetGraphicSizeMeters("crate", 0)
Dim rect As B2PolygonShape
rect.Initialize
rect.SetAsBox(size.X / 2, size.Y / 2)
wrapper.Body.CreateFixture2(rect, 1)
wrapper.Body.ApplyLinearImpulse(X2.CreateVec2(10,10),X2.CreateVec2(0,0))
End Sub
Private Sub Create_Crate(x As Int, y As Int)
Dim size As B2Vec2 = X2.GraphicCache.GetGraphicSizeMeters("crate", 0)
Dim xoffset,yoffset As Double
Dim bd As B2BodyDef
yoffset=blocksize/2
xoffset=X2.ScreenAABB.Width-blocksize
bd.BodyType = bd.TYPE_DYNAMIC
bd.Position = X2.CreateVec2(xoffset + x*size.x,yoffset + y*size.y)
Dim wrapper As X2BodyWrapper = X2.CreateBodyAndWrapper(bd,Null, "crate")
wrapper.GraphicName = "crate"
Dim rect As B2PolygonShape
rect.Initialize
rect.SetAsBox(size.X / 2, size.Y / 2)
wrapper.Body.CreateFixture2(rect, 2.0)
End Sub
The main activity is not relevant as it is not cross platform. The main place for your code is the Game class.
In this example there are no other classes.
here's an example of you crate stack and a crate being thrown to it
it's minimal and erel usually uses a class per object type but it doesn't seem needed for just this example.
the minimal code is like this. (it can probably be shorter but I'm still a noob with it)
B4X:Sub Class_Globals Private Ground As X2BodyWrapper Dim blocksize As Int End Sub Public Sub Initialize (Parent As B4XView) Parent.LoadLayout("GameLayout") X2.Initialize(Me, ivForeground) world = X2.World X2.ConfigureDimensions(world.CreateVec2(5, 0),10) X2.EnableDebugDraw StartGame End Sub Public Sub StartGame If X2.IsRunning Then Return X2.UpdateWorldCenter(X2.CreateVec2(X2.ScreenAABB.Width/2,X2.ScreenAABB.Height/2)) X2.Start blocksize=X2.ScreenAABB.Width/10 Dim sprites As List = X2.ReadSprites(xui.LoadBitmap(File.DirAssets, "crate.png"), 1, 1,blocksize,blocksize) X2.GraphicCache.PutGraphic("crate", sprites) CreateGround Create_Bullet For y=0 To 9 Create_Crate(0,y*blocksize) Next End Sub Private Sub CreateGround Dim GroundBox As B2Vec2 = X2.CreateVec2(9.60, 0) Dim bd As B2BodyDef bd.BodyType = bd.TYPE_STATIC bd.Position = X2.CreateVec2(X2.ScreenAABB.Center.X, X2.ScreenAABB.BottomLeft.Y + GroundBox.Y / 2) Ground = X2.CreateBodyAndWrapper(bd, Null, "Ground") Dim rect As B2PolygonShape rect.Initialize rect.SetAsBox(GroundBox.X / 2, GroundBox.Y / 2) Ground.Body.CreateFixture2(rect, 1) End Sub Private Sub Create_Bullet Dim bd As B2BodyDef bd.BodyType = bd.TYPE_DYNAMIC bd.Position = X2.CreateVec2(0,2) Dim wrapper As X2BodyWrapper = X2.CreateBodyAndWrapper(bd,Null, "crate") wrapper.GraphicName = "crate" Dim size As B2Vec2 = X2.GraphicCache.GetGraphicSizeMeters("crate", 0) Dim rect As B2PolygonShape rect.Initialize rect.SetAsBox(size.X / 2, size.Y / 2) wrapper.Body.CreateFixture2(rect, 1) wrapper.Body.ApplyLinearImpulse(X2.CreateVec2(10,10),X2.CreateVec2(0,0)) End Sub Private Sub Create_Crate(x As Int, y As Int) Dim size As B2Vec2 = X2.GraphicCache.GetGraphicSizeMeters("crate", 0) Dim xoffset,yoffset As Double Dim bd As B2BodyDef yoffset=blocksize/2' * size.x xoffset=X2.ScreenAABB.Width-blocksize bd.BodyType = bd.TYPE_DYNAMIC bd.Position = X2.CreateVec2(xoffset + x*size.x,yoffset + y*size.y) Dim wrapper As X2BodyWrapper = X2.CreateBodyAndWrapper(bd,Null, "crate") wrapper.GraphicName = "crate" 'horseShoe.Initialize(wrapper) Dim rect As B2PolygonShape rect.Initialize rect.SetAsBox(size.X / 2, size.Y / 2) wrapper.Body.CreateFixture2(rect, 2.0) End Sub
A minimal example is included in the examples pack. The actual code without the template code is about 10 lines.
how you create the bitmaps or animations using xui2d?
Yes. You can also see it in the tank example.wich one the hello world example?
There are several ways. The simplest one is to set the 'graphic file' property in the Tiled map:how you create the bitmaps or animations using xui2d?
If RightDown Then
Tank.Body.ApplyLinearImpulse(TankMovementForce, Tank.Body.Position)
Tank.SwitchFrameIntervalMs = 100
Else If LeftDown Then
Tank.Body.ApplyLinearImpulse(TankMovementForce.Negate, Tank.Body.Position)
Tank.SwitchFrameIntervalMs = 100
Else
Tank.SwitchFrameIntervalMs = 0
End If
There are several ways. The simplest one is to set the 'graphic file' property in the Tiled map:
You can add more frames by adding more properties:
The 'switch frame inteval (ms)' property sets the automatic switching interval. In the tank example it is set to 0 as we only want it to switch frames while the tank is moving.
This is managed in the code:
If you want to use XUI2D then start with the recommended way and use Tiled. Open X2TileMap and you will see that it is a complex class with many features. No reason to rewrite it yourself.This is a very good approch but i would like to learn how it works manually and not using tiled.
Private Sub LoadMarioGraphics
Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirAssets, "mario_bros.png")
Dim NumberOfSprites As Int = 14
Dim MarioSmall As B4XBitmap = bmp.Crop(80, 32, NumberOfSprites * 16, 16)
Dim AllSmall As List = X2.ReadSprites(MarioSmall, 1, NumberOfSprites, 1, 1)
X2.GraphicCache.PutGraphic("mario small walking", Array(AllSmall.Get(0), AllSmall.Get(1), AllSmall.Get(2)))
X2.GraphicCache.PutGraphic("mario small standing", Array(AllSmall.Get(6)))
X2.GraphicCache.PutGraphic("mario small jumping", Array(AllSmall.Get(4)))
X2.GraphicCache.PutGraphic("mario small strike", Array(AllSmall.Get(5)))
X2.GraphicCache.PutGraphic("mario small change direction", Array(AllSmall.Get(3)))
NumberOfSprites = 19
Dim MarioLarge As B4XBitmap = bmp.Crop(80, 0, NumberOfSprites * 16, 32)
Dim AllLarge As List = X2.ReadSprites(MarioLarge, 1, NumberOfSprites, 1, 2)
X2.GraphicCache.PutGraphic("mario large walking", Array(AllLarge.Get(0), AllLarge.Get(1), AllLarge.Get(2)))
X2.GraphicCache.PutGraphic("mario large standing", Array(AllLarge.Get(6)))
X2.GraphicCache.PutGraphic("mario large jumping", Array(AllLarge.Get(4)))
X2.GraphicCache.PutGraphic("mario large strike", Array(AllLarge.Get(5)))
X2.GraphicCache.PutGraphic("mario large change direction", Array(AllLarge.Get(3)))
X2.GraphicCache.PutGraphic("mario change size", Array(AllSmall.Get(6), AllLarge.Get(6)))
End Sub
Private Sub ChangeSize (ToLarge As Boolean)
x2.SoundPool.PlaySound("powerup")
bw.GraphicName = "mario change size"
MainBC.DrawBitmapCreatorsAsync(Me, "BC", gs.DrawingTasks)
Dim sb As X2ScaledBitmap
sb.Bmp = cvs.CreateBitmap.Crop(0, 0, r.Right, r.Bottom)
sb.Scale = 1
Dim GraphicName As String = x2.GraphicCache.GetTempName
Dim gd As X2SpriteGraphicData = x2.GraphicCache.PutGraphic(GraphicName, Array(sb))
gd.VerticalSymmetry = True
gd.HorizontalSymmetry = True
bw.GraphicName = GraphicName
Good day! Since I require my character to change animation according to the events that occur in the game, I stopped using the graphics files in the tiled, and started loading the sprite arrangements by assigning a name to each animated situation. I followed Mario's example and loaded the image arrangement into the cache using the next structure:If you want to use XUI2D then start with the recommended way and use Tiled. Open X2TileMap and you will see that it is a complex class with many features. No reason to rewrite it yourself.
Everything can be later customized.
Another way to set the graphics, still in a simple way, is demonstrated in the Mario example:
This code reads a sprite sheet and splits it to various groups. The key to each group is the "graphic name" parameter.B4X:Private Sub LoadMarioGraphics Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirAssets, "mario_bros.png") Dim NumberOfSprites As Int = 14 Dim MarioSmall As B4XBitmap = bmp.Crop(80, 32, NumberOfSprites * 16, 16) Dim AllSmall As List = X2.ReadSprites(MarioSmall, 1, NumberOfSprites, 1, 1) X2.GraphicCache.PutGraphic("mario small walking", Array(AllSmall.Get(0), AllSmall.Get(1), AllSmall.Get(2))) X2.GraphicCache.PutGraphic("mario small standing", Array(AllSmall.Get(6))) X2.GraphicCache.PutGraphic("mario small jumping", Array(AllSmall.Get(4))) X2.GraphicCache.PutGraphic("mario small strike", Array(AllSmall.Get(5))) X2.GraphicCache.PutGraphic("mario small change direction", Array(AllSmall.Get(3))) NumberOfSprites = 19 Dim MarioLarge As B4XBitmap = bmp.Crop(80, 0, NumberOfSprites * 16, 32) Dim AllLarge As List = X2.ReadSprites(MarioLarge, 1, NumberOfSprites, 1, 2) X2.GraphicCache.PutGraphic("mario large walking", Array(AllLarge.Get(0), AllLarge.Get(1), AllLarge.Get(2))) X2.GraphicCache.PutGraphic("mario large standing", Array(AllLarge.Get(6))) X2.GraphicCache.PutGraphic("mario large jumping", Array(AllLarge.Get(4))) X2.GraphicCache.PutGraphic("mario large strike", Array(AllLarge.Get(5))) X2.GraphicCache.PutGraphic("mario large change direction", Array(AllLarge.Get(3))) X2.GraphicCache.PutGraphic("mario change size", Array(AllSmall.Get(6), AllLarge.Get(6))) End Sub
Later we can switch groups of frames by changing bw.GraphicName:
B4X:Private Sub ChangeSize (ToLarge As Boolean) x2.SoundPool.PlaySound("powerup") bw.GraphicName = "mario change size"
And now for more advanced ways...
Dim all As List = X2.ReadSprites(xui.LoadBitmap(File.DirAssets, "walking_sprite.png"), 1, 3, 1, 1)
X2.GraphicCache.PutGraphic("walking", Array(all.Get(0), all.Get(1)))
.
.
.
sub do_any_thing
bw.GraphicName = "walking"
end sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?