Adding the controls

We have the code in place to add and remove platforms based on the user's touch. So it's time to code the touch controls for the game. Let's look at the ccTouchesBegan function in GameWorld.cpp:

void GameWorld::ccTouchesBegan(CCSet* set, CCEvent* event)
{
  // don't accept touch when clown is in these states
  if(clown_->GetState() == E_CLOWN_ROCKET ||
    clown_->GetState() == E_CLOWN_BALLOON ||
    clown_->GetState() == E_CLOWN_UP)
    return;

  CCTouch* touch = (CCTouch*)(*set->begin());
  CCPoint touch_point = touch->getLocationInView();
  touch_start_ = CCDirector::sharedDirector()->
    convertToGL(touch_point);
  
  // remove any previously added platforms
  RemovePlatform();
  // convert touch coordinates with respect to game_object_layer_ and position the platform there
  platform_->setPosition(game_object_layer_->
    convertToNodeSpace(touch_start_));
  platform_->setVisible(true);
  platform_->setScaleX(0);
}

We start by storing the touch received into a variable called touch_start_. We then remove any previously added platforms. Since this is the first touch, we position the platform at the touch location, set it's visibility to true, and scale it down to 0. Notice how we call the convertToNodeSpace function on game_object_layer_. This converts a point in the world coordinates to coordinates local to a given node. We must do this because the touch coordinates will be with respect to the GameWorld layer, whereas platform_ has been added to the game_object_layer_. So when the game_object_layer_ moves, the touch coordinates will have to be offset appropriately.

We must resize and rotate the platform based on the movement of the user's finger, which we'll do in the ccTouchesMoved function as follows:

void GameWorld::ccTouchesMoved(CCSet* set, CCEvent* event)
{
  // don't accept touch when clown is in these states
  if(clown_->GetState() == E_CLOWN_ROCKET ||
    clown_->GetState() == E_CLOWN_BALLOON ||
    clown_->GetState() == E_CLOWN_UP)
    return;

  CCTouch* touch = (CCTouch*)(*set->begin());
  CCPoint touch_point = touch->getLocationInView();
  touch_end_ = CCDirector::sharedDirector()->convertToGL(touch_point);

  // manipulate anchor point so the platform is correctly oriented
  platform_->setAnchorPoint( touch_end_.x >= 
    touch_start_.x ? ccp(0, 0.5f) : ccp(1, 0.5f) );
  float length = ccpDistance(touch_end_, touch_start_);
  // scale the platform according to user input
  platform_->setScaleX(length / platform_->getContentSize().width);
  // manipulate rotation so that platform doesn't appear upside down
  float angle = CC_RADIANS_TO_DEGREES(-1 * ccpToAngle(ccpSub(
    touch_end_, touch_start_)));
  platform_->setRotation( touch_end_.x >= 
    touch_start_.x ? angle : angle + 180 );
}

We begin by updating the touch_end_ variable, which will track the last location of the user's finger on screen. Then, based on whether the finger has moved to the left or right of its initial position, we reset the anchor point. This is followed by setting the appropriate scale and rotation. If you're wondering why we're manipulating the anchor point and the rotation values, this table should clear things up:

Start point

End point

Platform without manipulation

Platform with manipulation

S = (0, 0)

E = (100, 100)

Adding the controls
Adding the controls

S = (100, 100)

E = (0, 0)

Adding the controls
Adding the controls

Adjusting the anchor points basically orients the platform with respect to the player's touch points. If the player drags from left to right, the platform should be shown expanding from left to right. The only way we can achieve that is by setting the x coordinate of the anchor-point to 0. The exact opposite is valid when the player drags from right to left.

In the ccTouchesEnded function, which is not discussed, we simply take the value of touch_start_ and touch_end_ and pass them to the AddPlatform function before resetting their values to CCPointZero.

So, we have written code to set the stage for the clown as well as to create the clown. We also wrote a couple of functions that create platforms for the clown to jump on, based on the user's touch. In its current state, the clown's body will simply bounce off the platform owing to the clown fixture's restitution value of 0.5f.

But we don't want that, do we? The length of the platform drawn by the user plays an important role to the amount the clown will jump. Shorter platforms must throw the clown with greater force than larger platforms. We do this in our next section where we listen for collisions.

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

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