In the last article we introduced the effects and animations using the Cocos2d framework. In this article we will take a step further and move the sprite to random positions. The article will also demonstrate how to detect collision when the user touches the sprite on the screen.
It is highly recommended to read the previous articles about iPhone development which will set up the stage for this article. The link to the articles is given below:
Introduction to Cocos2d for iPhone
Sprites and Animation in Cocos2d Application
Continuous Movement of the Sprite:
Previously we learned how to move the sprite from one location to the other by using actions. We also learned how to invoke multiple actions at the same time using CCSPawn class. Our actions were time based which means that actions were stopped after the allotted time was reached. In this section we will make our sprite to move continuously until the user touches the sprite which will make it fade away.
Since, we will be tracking the movement of a single sprite it is a good idea to expose
the sprite as a property. The header file defines the property as shown below:
The implementation file exposes the ball property as shown below:
The addSprite method calls the startAnimation method with sprite as the argument
The startAnimation method is responsible for animating the sprite to the
The last line uses the CCSequence action to run multiple actions. Currently, the
only action invoked is actionMove. The actionMoveDone is a callback method
which is fired when the actions are completed. The finishedAnimation method is
The finishedMoving method simply calls the startAnimation again which moves
the sprite to a new position. This process continues until it is touched by the user.
The touch will be implemented later in this article.
The generateRandomCoordinates method is responsible for creating a random
position where the sprite will be moved. The getRandomCoordinates is implemented
There are several methods of creating random numbers which include rand, random and arc4random. The arc4random provides the most precision. The following code generates the random number from 0 to x-1.
To generate a random number from 1 to x add 1 to the equation as shown below:
Run the above code and you will notice that now the ball moves continuously to
random positions on the screen.
You might be wondering that why are we talking about collision detection since there is no other body in motion. The moving sprite can collide with the user
touch coordinates. In order to detect collision we must travel back in time when we
were in high school learning about Pythagoras theorm. For those of you
currently in high school you do not have to worry about charging the flux capacitor.
The ccTouchesBegin event is implemented below:
If the user touches the ball successfully then the ball stops and fades away. To
have better understanding of the code you can download the project files and execute
This article focused on the random and continuous animation of sprites. The article
also demonstrated how to perform collision detection using touches. In the next
article we will learn how to add sound effects and background sounds to the