Mobile controls

We're going to keep our controls really simple for this demo, there won't be a virtual joystick or button in sight. As we only need three functions, namely left, forward, and right, we'll be breaking the screen up in to three sections and using the left section for turning left, the center section for forward, and the right section for turning right. The following is a screenshot of the game with the control areas overlaid for illustration purposes:

Mobile controls

We already have our control script for the game, Sokoban, so go ahead and double-click on it to edit it in MonoDevelop.

We're going to create a new method that will return true or false when we pass it in a string for the control direction we want to check. It should only return true if the tap has just happened, and not keep returning true if the user holds his finger on the screen. The method is as follows:

  // Check if an area of the screen has been
  // touched for the very first time
  bool FirstTouchForControlType(string controlType) {
    
    // Is there only one press?
    if (Input.touchCount == 1) {
      
      // Get the touch out of the touches
      // array as a touch object
      Touch touch = Input.touches[0];
      
      // Has the touch just started?
      if (touch.phase == TouchPhase.Began) {
        
        // Get the screen sections
        float screenSectionLeft = Screen.width / 3;
        float screenSectionCenter = screenSectionLeft + Screen.width / 3;
        float screenSectionRight = Screen.width;
        
        // Check the passed control type
        if (controlType == "left") {
          
          // Have we hit the left section?
          if (touch.position.x <= screenSectionLeft) {
            return true;
          } else {
            return false;
          }
        } else if (controlType == "forward") {
        
        // Have we hit the forward section?
        if (touch.position.x >= screenSectionLeft && touch.position.x <= screenSectionCenter) {
          return true;
        } else {
          return false;
        }
      } else if (controlType == "right") {
        
        // Have we hit the right section?
        if (touch.position.x >= screenSectionCenter && touch.position.x <= screenSectionRight) {
          return true;
        } else {
          return false;
        }
      }
    }
  }
  
  return false;
}

Let's break that code down as there's quite a large amount. Firstly, we check that there is only one finger on the screen, we then get the touch at index 0 and store it in to a variable named touch. As we only want the method to return true when the finger is first placed, we have to check the touch.phase value and make sure it's the same as TouchPhase.Began. After that we break the screen down into three sections, on Android the screen sizes can be variable, so it's best that this is done in code to allow for the variation. We pass in the methods to check whether left, forward, or right have been clicked, so the next check is the value of that string, from there on the code is self-explanatory.

Now that we have the method in place, we need to modify our movement code, it's in the same file so find the method named CheckIfPlayerIsAttempingToMove and find the following code line:

if (Input.GetKeyDown (KeyCode.UpArrow) || OuyaInput.GetButtonDown(OuyaButton.DU, playerNumber)) {

We're going to modify it to be the following code:

if (Input.GetKeyDown (KeyCode.UpArrow) || OuyaInput.GetButtonDown(OuyaButton.DU, playerNumber) || FirstTouchForControlType("forward")) { 

You see we've just added another check to the conditional with the OR operator, we call our method and pass in a string of "forward". Do the same for left and right.

That's it! Give the game a test and try out the new control method. It is pretty impressive that the game now works in the editor, on the Ouya and on Android!

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

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