为无尽的跑酷游戏创建动态大小的 SKSpriteNode 平台
Creating a dynamically sized SKSpriteNode platform for endless runner game
我正在为 IOS 制作我的第一个 Endless Runner 游戏,我希望它尽可能地动态。我想创建一个大 "Platform" 图像,然后使用该图像创建各种尺寸的平台。
想法是随机 select 平台宽度的数字,然后生成精灵和 body 以匹配所选尺寸。完成此操作后,通过仅使用图像的一部分来获取图像以填充精灵。
此时我正在使用以下内容,但这会根据 UIImage 的大小创建节点。
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];
所以理想情况下我想
- 根据随机宽度和固定高度创建精灵。
- 使用较大图像的一部分填充精灵。
有什么办法可以做到这一点吗?
谢谢
Create a sprite based upon a random width and fixed height.
为 width
选择一个随机数很简单。您可以使用 并确保选择合理范围内的数字(小于您的平台图像)。
Use part of a larger image to fill in the sprite.
这可以通过使用 textureWithRect:inTexture: 来完成。第一个参数是单位坐标 space 中的一个矩形,指定要使用的纹理部分。第二个参数是创建新纹理的整个平台纹理。
以下是有关如何设置每个平台的 size/portion 的提示:
(0, 0)为整个平台坐标的左下角
x/y坐标范围是0到1,不是平台图片的实际尺寸
给定由平台图像创建的纹理platformAllTexture
和第一步中的随机width
,平台纹理可能是:
// 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];
这样,您就得到了动态尺寸平台的纹理platformTexture
。
在上面的示例中,如果矩形定义为 CGRectMake(0, 0, 1/3.0, 1/2.0)
,您将得到类似的结果:
我正在为 IOS 制作我的第一个 Endless Runner 游戏,我希望它尽可能地动态。我想创建一个大 "Platform" 图像,然后使用该图像创建各种尺寸的平台。
想法是随机 select 平台宽度的数字,然后生成精灵和 body 以匹配所选尺寸。完成此操作后,通过仅使用图像的一部分来获取图像以填充精灵。
此时我正在使用以下内容,但这会根据 UIImage 的大小创建节点。
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];
所以理想情况下我想
- 根据随机宽度和固定高度创建精灵。
- 使用较大图像的一部分填充精灵。
有什么办法可以做到这一点吗?
谢谢
Create a sprite based upon a random width and fixed height.
为 width
选择一个随机数很简单。您可以使用
Use part of a larger image to fill in the sprite.
这可以通过使用 textureWithRect:inTexture: 来完成。第一个参数是单位坐标 space 中的一个矩形,指定要使用的纹理部分。第二个参数是创建新纹理的整个平台纹理。
以下是有关如何设置每个平台的 size/portion 的提示:
(0, 0)为整个平台坐标的左下角
x/y坐标范围是0到1,不是平台图片的实际尺寸
给定由平台图像创建的纹理platformAllTexture
和第一步中的随机width
,平台纹理可能是:
// 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];
这样,您就得到了动态尺寸平台的纹理platformTexture
。
在上面的示例中,如果矩形定义为 CGRectMake(0, 0, 1/3.0, 1/2.0)
,您将得到类似的结果: