Swift game Development Tetris: No.9 Add click and Move Events

Source: Internet
Author: User

After the content of this section, our program will run like this:

Let's take a step at a point, first add a click event:

You should be familiar with the steps of iOS development, we need to add uitapgesturedetector to view.

Open Main.storyboard, the interface should resemble the following


Find the Tap Gesture recognizer in the bottom right corner and drag it into the gameviewcontrollerscene.


Next open the Assistant Editor, the interface looks like this:


Hold down CTRL and drag the tap Gesture recognizer into the Gameviewcontroller on the right


Make sure your connection type is Action,name is Didtap, type is UITapGestureRecognizer, then click Connect, and the following code should be added in the Gameviewcontroller class:

This function will be executed when the Click event is recognized. Next we right-click on the tap Gesture recognizer in this interface to assign outlets delegate to the game View Controller


Just like adding swiftris proxies to Gameviewcontroller, let's add gestures to the proxy.


Next Run our program and see if we can rotate the Tetris after clicking on the screen. Here's what our program should look like:


Next, we need to add two gestures, adding the same process as the tap Gesture.

Let's start by adding a pan, which is moving, to make sure your didpan function is the same as the following:



#1 we define a panpointreference point to track the position of the pan gesture.

#2 We record the starting point position and then calculate the current position in # # there is no absolute difference from the starting point (ABS) more than 0.9 blocks, if it exceeds, execute the move command

#4可以通过velocityInView来判断手势的方向, the positive value is to the right, the negative value is to the left, and we move the Swiftris to the corresponding direction. and set the current position to the starting position, so that the user can move several cells at a time.

But I found in later games that it was easy to move more than I could imagine, and if it was easy to start a pan in the back of the swipe action, because 0.9 blocksize was actually a small position. But this is not a problem, we know the principle, how to adjust the random!

Then run the program and see if that's what we think it is.


Next we complete the swipe gesture, swipe is the time we move, the block can move directly in this direction quickly. Repeat the same steps to build the swipe Gesture recognizer,name for Didswipe, which takes one more step, as shown in


Make sure the swipe direction is down.

Next we start to modify Gameviewcontroller


We re-enter the next # # and # # without showing the full code

Func Gesturerecognizer (gesturerecognizer:uigesturerecognizer!, Shouldrecognizesimultaneouslywithgesturerecognizer othergesturerecognizer:uigesturerecognizer!) -Bool {         return true     }//#2     func gesturerecognizer (gesturerecognizer:uigesturerecognizer!, Shouldberequiredtofailbygesturerecognizer othergesturerecognizer:uigesturerecognizer!) -Bool {         if let Swiperec = Gesturerecognizer as? Uiswipegesturerecognizer {             if let Panrec = Othergesturerecognizer as? Uipangesturerecognizer {                 return False             }         } else if let Panrec = Gesturerecognizer as? Uipangesturerecognizer {             if let Taprec = Othergesturerecognizer as? UITapGestureRecognizer {                 return True             }         }         return False     }

#1 section allows our gestures to execute simultaneously, of course, sometimes our gestures may conflict, so we need to make some adjustments

Notice that in # # if the current gesture is swipe and the panrec gesture is othergesturerecognizer, I need to return false in my code, because I'm just beginning to see that if it's return ture, Then the swipe gesture was not recognized because it was covered by a pan. and changed to False after the normal. This is the 2nd mistake in the original textbook.

You can try it.


Swift game Development Tetris: No.9 Add click and Move Events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.