Games x2 onetime animation?

ilan

Expert
Licensed User
Longtime User
hi

what is the best way to show a one time animation? like an explosion? something that should run one time the whole sprite sheet images?

thanx
 

peacemaker

Expert
Licensed User
Longtime User
Also interesting (but i'm only planning to start using X2, never tried yet).
I guess, onetime animation must be fully asynchronous, like 3 explosions one by one - all must be animated from start to finish together, parallel.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In some of the examples I've used X2.AddFutureTask for the explosion.

B4X:
'space invaders example
Private Sub EnemyHit_WithMissle(ft As X2FutureTask)
    Dim bodies As X2BodiesFromContact = ft.Value
    bodies.OtherBody.Delete(X2.gs) 'delete the missile
    Dim enemy As X2BodyWrapper = bodies.ThisBody
    enemy.CurrentFrame = 2 'set to the explosion frame (no animation in this case)
    enemy.SwitchFrameIntervalMs = 0
    IncrementScore(100)
    X2.AddFutureTask(Me, "Delete_Enemy", 100, enemy) 'delete object after 100ms
    X2.SoundPool.PlaySound2("invaderkilled", 0.3)
End Sub

Private Sub Delete_Enemy(ft As X2FutureTask)
    Dim enemy As X2BodyWrapper = ft.Value
    If enemy.IsDeleted Then Return
    enemy.Delete(X2.gs)
End Sub

In the space shooter example, an explosion object is created, replacing the asteroid and its "time to live" is set in Tiled to 800ms:
B4X:
Sub Destroy_Asteroid (ft As X2FutureTask)
    Dim bc As X2BodiesFromContact = ft.Value
    Dim asteroid As X2BodyWrapper = bc.OtherBody
    If asteroid.IsDeleted Then Return
    Dim explosion As X2BodyWrapper = TileMap.CreateObject2ByName(ObjectLayer, "explosion")
    explosion.Body.SetTransform(asteroid.Body.Position, 0)
    X2.SoundPool.PlaySound("asteroid")
    Dim FireBody As X2BodyWrapper = bc.ThisBody
    FireBody.Delete(X2.gs)
    asteroid.Delete(X2.gs)
    ScoreLabel1.Value = ScoreLabel1.Value + 100
End Sub

So it animates once (8 frames * 100ms) and is removed automatically.

1707028975811.png
 

ilan

Expert
Licensed User
Longtime User
thank you erel, this looks good.
i will need to figure out how to show the animation but keep the last frame.
i want to show a door opening/closing
so there is the animation of the opening door (5 frames) but keep the last frame of the door that is open.

thanx
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It's been a while since I played with X2 :)

B4X:
Public Sub Tick (GS As X2GameStep)
    Dim touch As X2Touch = Multitouch.GetSingleTouch(PanelForTouch)
    If touch.IsInitialized Then
        For Each bw As X2BodyWrapper In X2.GetBodiesIntersectingWithWorldPoint(X2.ScreenPointToWorld(touch.X, touch.Y))
            If bw.Name.StartsWith("door") Then
                Dim dd As DoorData = bw.Tag
                Select dd.State
                    Case DOOR_CLOSED
                        'open door
                        dd.State = DOOR_OPENING
                        bw.SwitchFrameIntervalMs = 50
                        bw.X2.AddFutureTask(Me, "DoorOpened_Completed", bw.SwitchFrameIntervalMs * bw.NumberOfFrames, bw)
                    Case DOOR_OPENED
                        'close door
                        dd.State = DOOR_CLOSING
                        bw.X2.AddFutureTask(Me, "DoorClosing_Process", 50, bw)
                End Select
            End If
        Next
    End If
End Sub

Private Sub DoorClosing_Process (ft As X2FutureTask)
    Dim bw As X2BodyWrapper = ft.Value
    If bw.CurrentFrame = 0 Then
        bw.Tag.As(DoorData).State = DOOR_CLOSED
    Else
        bw.CurrentFrame = bw.CurrentFrame - 1
        bw.X2.AddFutureTask(Me, "DoorClosing_Process", 50, bw)
    End If
End Sub
Private Sub DoorOpened_Completed (ft As X2FutureTask)
    Dim bw As X2BodyWrapper = ft.Value
    bw.Tag.As(DoorData).State = DOOR_OPENED
    bw.SwitchFrameIntervalMs = 0
End Sub

door sprite source: https://opengameart.org/content/animated-bamboo-door-sprite

java_4sMd1a1Z9H.gif


Going backwards is done manually.
 

Attachments

  • Project.zip
    96.3 KB · Views: 120

ilan

Expert
Licensed User
Longtime User
hi again, i just had a look at the example and everything is crystal clear. i can see the simplicity behind the logic of x2 how you created it. it saves lot of code if it is used correctly. thank you again, i understand much more x2 now and can continue :)
 
Top