2

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

  1. Create a sprite based upon a random width and fixed height.
  2. Use part of a larger image to fill in the sprite.

Any ideas how I can go about doing this?

Thanks

ORStudios
  • 3,157
  • 9
  • 41
  • 69

1 Answers1

3

Create a sprite based upon a random width and fixed height.

To pick a random number for the width is simple. You can use arc4random_uniform and be sure to pick the number in a reasonable range (less than your platform image).

Use part of a larger image to fill in the sprite.

This can be done by using textureWithRect:inTexture:. The first parameter is a rectangle in the unit coordinate space that specifies the portion of the texture to use. And the second parameter is the whole platform texture to create the new texture from.

Here's the hints for how to set the size/portion of your each platform:

  1. (0, 0) is the left-lower corner of the coordinate of the whole platform.

  2. The range for x/y coordinate is from 0 to 1, not the real dimension of the platform image.

Given a texture created by the platform image platformAllTexture and a random width in the first step, the platform texture may be:

// Fixed height is set as half of the platform image
SKTexture *platformTexture = [SKTexture textureWithRect:CGRectMake(0, 0, width/platformAllTexture.size.width, 1/2.0)          
                                              inTexture:platformAllTexture];

In this way, you have got the texture platformTexture for the dynamic sized platforms.

In the example above, if the rectangle is defined as CGRectMake(0, 0, 1/3.0, 1/2.0), you will get this similar result:

enter image description here

Community
  • 1
  • 1
WangYudong
  • 4,335
  • 4
  • 32
  • 54