The A* algorithm is a generic node-graph traversal routine which can be applied to many abstract problems. In a 2D side-scroller, space is traversed in a complex, non-linear fashion. Actors are expected to run across platforms and jump from platform to platform. With some extra math, we can tailor our A* technique to this problem.
The code listed as follows is heavily edited for the sake of brevity. Please refer to the project RecipeCollection03 for full working code of this recipe.
Execute the following code:
/* SSAStarNode */ @implementation SSAStarNode -(float) costToNeighbor:(SSNeighborNode*)nn { SSAStarNode *node = nn.node; //Here we use jumping/running to determine cost. We could also possibly use a heuristic. CGPoint src = ccp(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO); CGPoint dst = ccp(node.position.x/PTM_RATIO, node.position.y/PTM_RATIO); float cost; if(node.body == self.body){ //Compute simple...