Games [BANanoPhaser 0.2+] Exploring tweening with a coin catcher

Mashiane

Expert
Licensed User
Ola

Download

Welcome to game 4. The purpose of this game is to explore more game tricks. For example.

Tweening.gif


1. Instead of adding a collision detector, we return the result of physics.overlap, a boolean.
2. We have removed gravity on the game configuration. This enables us to have the elements not falling off the screen.
3. We set a background color for the game.
4. We use BANano.CallSub to call another method within the game when something happens. This is using the CallBack but we havent used any callbacks on this example.

We keep on improving on the code readability to ensure that transitioning from javascript to BANanoPhaser is simple enough.

Enjoy
 

Mashiane

Expert
Licensed User
1. We use a preload-scene to load our resources, when this is complete it starts the game.

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

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

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

B4X:
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!
 
Top