I am creating my first Endless Runner game for IOS and I want it to be as dynamic as possible. I would like to create one large "Platform" image and then use that one image to create platforms of various size.
The idea is to randomly select a number for the width of the platform and then generate the sprite and body to match the chosen size. After this is done get the image to fill in the sprite by using only part of the image.
At the minute I am using the following but this creates the node based upon the size of the UIImage instead.
SKSpriteNode *spritePlatform = [[Platform alloc] initWithImageNamed:@"Platform"];
[spritePlatform setPosition:CGPointMake(self.frame.size.width + (spritePlatform.frame.size.width / 2), 200)];
spritePlatform.name = @"Platform";
spritePlatform.physicsBody = [SKPhysicsBody bodyWithTexture:spritePlatform.texture size:CGSizeMake(300, 40)];
spritePlatform.physicsBody.affectedByGravity = NO;
spritePlatform.physicsBody.dynamic = NO;
// 1
spritePlatform.physicsBody.usesPreciseCollisionDetection = YES;
// 2
spritePlatform.physicsBody.categoryBitMask = CollisionCategoryPlatform;
spritePlatform.physicsBody.contactTestBitMask = CollisionCategoryPlayer;
[self addChild:spritePlatform];
[self movePlatform:spritePlatform];
So ideally I would like to
- Create a sprite based upon a random width and fixed height.
- Use part of a larger image to fill in the sprite.
Any ideas how I can go about doing this?
Thanks