1. We use a preload-scene to load our resources, when this is complete it starts the game.
Sub onPreload
Dim value As Double
Dim onloadCB As BANanoObject = BANano.CallBack(Me, "onProgress", Array(value))
Scene.load.on("progress", onloadCB)
'
Dim onCompleteCB As BANanoObject = BANano.CallBack(Me, "onComplete", Null)
Scene.load.on("complete", onCompleteCB)
'
'start loading the assets
Scene.load.image("player", "./assets/player.png")
Scene.load.image("coin", "./assets/coin.png")
End Sub
2. When the game starts, it fires onCreate, here we place our player and other objects on the arcade. As the player and the coin will collide, onUpdate, we need to trap the cursor movements, we then run .createCursorKeys.
Sub onCreate
'get random x and y values
player = Scene.physics.add.sprite(100, 100, "player")
coin = Scene.physics.add.sprite(300, 200, "coin")
'ensure these dont fall outside the scene
player.SetCollideWorldBounds(True)
coin.SetCollideWorldBounds(True)
'initialie the scoreboard
score = 0
Dim style As Map = CreateMap("font": "20px Arial", "fill": "#fff")
scoreText = Scene.add.text(20, 20, "Score: " & score, style)
'trap keypress
arrow = Scene.input.keyboard.createCursorKeys
End Sub
3. When the game is played, when the player is moved via the arrow keys, detect an overlap between the coin and the player. If there is a collision, fire the hit sub. The movement of the player is ensured that when the keys are pressed down, the x and y positions are incremented and decremented.
Sub onUpdate
If Scene.physics.overlap(player, coin) Then
BANano.CallSub(Me, "hit", Null)
End If
If arrow.right.isDown Then
'increment x of plater
player.IncrX(3)
else if arrow.left.isDown Then
'decrement x of player
player.DecrX(3)
End If
If arrow.down.isDown Then
'increment y of player
player.IncrY(3)
else if arrow.up.isDown Then
'decrement y of player
player.DecrY(3)
End If
End Sub
4. Looking at the hit Sub..
The hit subs generates an x and y random value and moves the player to the new location. Also the score is incremented.
Sub hit
'set x and y values with random numbers
coin.X = Scene.MathBetween(100, 600)
coin.y = Scene.MathBetween(100, 200)
'increment the score board
score = score + 10
scoreText.SetText("Score: " & score)
'add a tween: i.e A Tween allows you to alter one or more properties of a target object over a defined period of time.
Scene.tweens.add(player, 200, 1.2, 1.2, True)
End Sub
As noted, this game also just deals withe the barebones of game development. You can explore the other examples for more functionality especially the Approximately game that has more logic also.
Enjoy!