Fruity Crush Saga App Diary: Day 4

Fruits

The essence of the match-three game is to move fruits such that three fruits of the same type match up. This move is done with a swipe. The Game Scene is the best place for implementing the detection of the player’s swipes that will reposition the fruits into this match pattern. The reposition is called the swap. Recognizing these swipes to swap in SpriteKit is best done with the touchesBegan, touchesMoved, and touchesEnded functions. 

Two optional properties that will aid in the computation required are swipeFromColumn and swipeFromRow. These properties will record the column and row number of the fruit that the player first touched when the swipe movement started. These are initialized to nil in the initializer init(size:).

In touchesBegan() we convert the touch location into either the column or row of the fruit touched by invoking a convertPoint() function that returns a tuple. convertPoint() ensures the CGPoint parameter passed is within the game grid otherwise it returns false. If the point is within the grid then we set the swipeFromColumn and swipeFromRow properties.

Detecting the swipe direction is achieved in the touchesMoved() function. In this function by comparing the new column and row numbers to the previous ones we determine whether the swipe direction is left, right, up, or down. Knowing the direction we then use the trySwap() function passing the horizontal and vertical deltas.

trySwap() is the workhorse of the swipe to swap functions because at this point we only know the direction the player swiped but we need to determine if there are two valid fruits to swap in that direction. We make this determination by calculating the column and row numbers of the fruits to swap with. Eliminating the edge swipes off the board and determining whether there is actually a fruit at the new position ensures that we implement a valid swap. For today I just have a print() of the from Fruit and to Fruit in order to see if this is all working.

Tomorrow, I’ll animate these swipes to swap.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s