Platform Generation

Now that we have our Grid Laid out, we can move on with our Platform Generation Methods.

Our platform generation algorithm will break down into 5 Steps:
1. We will create a new sprite node for our Platform.
2. We will use the methods we developed last time to determine the position of our Platform.
3. We will set the Physics attributes of our Platform (in our case we will treat our Platforms like SKPhysicsBody).
4. We will add the Platform to our PlatformNode.
5. We will add the Platform to our Platform Buffer of our PlatformNode.

IMG_0671

Here’s our code for this algorithm:

– (void) generatePlatformAtPosition:(CGPoint) mapPosition
{
// 1. Create a new SKSpriteNode
SKSpriteNode *newPlatform = [SKSpriteNode spriteNodeWithTexture:self.platformBrickTexture];

// 2. Set the Position by converting the grid coordinate system to the coordinate of the Scene
newPlatform.position = [self convertMapCoordinateToWorldCoordinate:mapPosition];

// 3. Set Up the Physics Attributes
newPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.platformBrickTexture.size.width,
self.platformBrickTexture.size.width)];
newPlatform.physicsBody.dynamic = NO;
newPlatform.physicsBody.restitution = 0.0f;
newPlatform.physicsBody.friction = 1.0f;

// 4. Add the SKSpriteNode to our Platform Node
[self addChild:newPlatform];

// 5. Add the SKSpriteNode to our Platform Buffer
[platformBlockBuffer addObject:newPlatform];
}

Now we can add a platform at whatever position we want.

Next time we’ll start adding and removing the platforms dynamically.

Hooray!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>