Time for action — enhancing the OnObjectTouch method

The OnObjectTouch method is updated as follows:

  1. First add two FLOAT variables at the start which will store the difference factor regarding objects positions.
    Method OnObjectTouch:Int(obj:ftObject, touchId:Int)
    Local xdiff:Float
    Local ydiff:Float
    
  2. Check if game mode is equal to the constant g.gmMenu.
    If g.gameMode = g.gmMenu Then
    
  3. Start the Select statement on the object's Tag property.
    Select obj.GetTag()
    
  4. Check against the constant g.btnPlay (the play button). If yes, then start a new game.
    Case g.btnPlay
    g.StartNewGame()
    
  5. Check against the constant g.btnScore (the score button). If yes, then show the high-score screen.
    Case g.btnScore
    g.ShowScore
    
  6. Check against the constant g.btnExit (the exit button). If yes, then exit the app.
    Case g.btnExit
    g.eng.ExitApp(True)
    
  7. Close the Select statement. After that, play the g.sndSelect sound and close the IF statement.
    End
    g.sndSelect.Play()
    Endif
    
  8. Now comes the part where we check if a gem was selected and if it can move.
  9. Start an IF check. Check that the game mode is set to g.gmPlay, that there are no transitions in the game layer, that the touch ID is not 99, and that justStarted is not True.
    If g.gameMode = g.gmPlay And g.layerGame.GetObjTransCount() = 0 And touchId <> 99 And g.justStarted = False Then
    
  10. Check if the first tile was selected before.
    If g.firstTile <> Null Then
    Get the X/Y distance of the first tile and the current one
    xdiff = g.firstTile.GetPosX() - obj.GetPosX()
    ydiff = g.firstTile.GetPosY() - obj.GetPosY()
    
  11. Check if the tile is the neighboring one. We do this by comparing the absolute differences between the x and y positions.
    If ((Abs(xdiff)<80.0) And (Abs(ydiff)<2.0)) Or ((Abs(ydiff)<80.0) And (Abs(xdiff)<2.0)) Then
    
  12. Set the second tile to the current object.
    g.secondTile = obj
    
  13. Now, check if there are at least two neighboring tiles of the same kind.
    If (g.CountGems(g.firstTile, g.secondTile)>1) Or (g.CountGems(g.secondTile, g.firstTile)>1) Then
    
  14. Initialize both tile slots
    g.SetSlotTile(g.GetSlotX(g.secondTile), g.GetSlotY(g.secondTile),-1)
    g.SetSlotTile(g.GetSlotX(g.firstTile), g.GetSlotY(g.firstTile),-1)
    
  15. Transform these tiles to their opposite coordinates and close the IF check.
    g.firstTile.CreateTransPos(g.secondTile.GetPosX(),g.secondTile.GetPosY(),300,False,99)
    g.secondTile.CreateTransPos(g.firstTile.GetPosX(),g.firstTile.GetPosY(),300,False,99)
    Endif
    
  16. Initialize the first and second tile objects.
    g.secondTile = Null
    g.firstTile = Null
    
  17. Place the tile selector outside the map so it can't be detected by the touch check.
    g.selector.SetPos(0, 0)
    g.selector.SetVisible(False)
    Else
    
  18. There are no neighboring tiles, so initialize the first tile and make the selector invisible.
    g.firstTile = Null
    g.selector.SetPos(0, 0)
    g.selector.SetVisible(False)
    
  19. Play the g.sndFalse sound.
    g.sndFalse.Play()
    Endif
    Else
    
  20. The first tile was selected, so make the selector visible and set it to the position of the current tile. After that, close the two IF checks.
    g.firstTile = obj
    g.selector.SetPos(obj.GetPosX(), obj.GetPosY())
    g.selector.SetVisible(True)
    Endif
    Endif
    
  21. If the game mode is g.gmPlay and the touch ID is 99, then raise the value of score by 100, spawn an explosion, and remove the tile.
    If g.gameMode = g.gmPlay And touchId = 99 Then
    g.score += 100
    SpawnExplosion(7,obj.GetPosX(), obj.GetPosY())
    obj.Remove()
    Endif
    
  22. Tell the game that the first frame was displayed. This is important because it somehow will set the selector wrong.
    g.justStarted = False
    
  23. Check if the Back button was selected.
    If obj.GetTag() = g.btnBack Then g.ShowMenu()
    Return 0
    End
    

What just happened?

Now that we have handled the touch events, all buttons are functional, and you can start a new game. You can also select the first two tiles. We still need to handle the transition and the winning conditions of the game, so let's go further.

We've reached our parking position… a transition is done

After both the selected gems have moved, something has to happen. That would be setting the slot IDs for the new gems and also checking if matching gems can be removed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.98.13