gpt4 book ai didi

ios - Sprite Kit-使用Switch Case将随机的Sprite节点添加到场景中

转载 作者:行者123 更新时间:2023-12-01 19:02:45 25 4
gpt4 key购买 nike

我有以下代码创建随机对象并将其添加到场景中。最后,在随机延迟后重复该方法。

-(void)createObjects {

// Create random start point
float randomStartPoint = arc4random_uniform(4) * 64 + 32;
CGPoint startPoint = CGPointMake(self.size.width + 50, randomStartPoint);

// Create random object and add to scene
switch (arc4random_uniform(2)) {
case 0:
{
SKSpriteNode *crate = [SKSpriteNode spriteNodeWithImageNamed: @"crate"];

crate.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:crate.frame.size];
crate.physicsBody.dynamic = YES;
crate.physicsBody.affectedByGravity = NO;
crate.physicsBody.categoryBitMask = objectCategory;
crate.physicsBody.collisionBitMask = 0;
crate.physicsBody.contactTestBitMask = playerCategory;

crate.position = startPoint;
crate.name = @"object";
crate.zPosition = 20;

[self addChild:crate];
break;
}

case 1:
{
SKSpriteNode *cactus = [SKSpriteNode spriteNodeWithImageNamed: @"cactus"];

cactus.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cactus.frame.size];
cactus.physicsBody.dynamic = YES;
cactus.physicsBody.affectedByGravity = NO;
cactus.physicsBody.categoryBitMask = objectCategory;
cactus.physicsBody.collisionBitMask = 0;
cactus.physicsBody.contactTestBitMask = playerCategory;

cactus.position = startPoint;
cactus.name = @"object";
cactus.zPosition = 20;

[self addChild:cactus];
break;
}

default:
break;
}

// After adding child, call same method at random delay
float randomNum = arc4random_uniform(4) * 0.4 + 0.25;
[self performSelector:@selector(createObjects) withObject:nil afterDelay:randomNum];
}

实际上,由于每个Sprite节点都有相同的设置,因此我有更多的情况,并且重复了很多相同的代码。有没有一种方法可以创建一个Sprite节点,并在每种情况下为该节点设置不同的图像,然后将其添加到场景中?

最佳答案

您可以在开关情况下创建SKTexture,然后仅创建具有该纹理的节点。

像这样:

-(void)createObjects {

// Create random start point
float randomStartPoint = arc4random_uniform(4) * 64 + 32;
CGPoint startPoint = CGPointMake(self.size.width + 50, randomStartPoint);

// Create random object and add to scene
SKTexture* objectTexture;
switch (arc4random_uniform(2)) {
case 0:
objectTexture = [SKTexture textureWithImageNamed:@"crate"];
break;
case 1:
objectTexture = [SKTexture textureWithImageNamed:@"cactus"];
break;
default:
break;
}
SKSpriteNode *object = [SKSpriteNode spriteNodeWithTexture:objectTexture];

object.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:crate.frame.size];
object.physicsBody.dynamic = YES;
object.physicsBody.affectedByGravity = NO;
object.physicsBody.categoryBitMask = objectCategory;
object.physicsBody.collisionBitMask = 0;
object.physicsBody.contactTestBitMask = playerCategory;

object.position = startPoint;
object.name = @"object";
object.zPosition = 20;

[self addChild: object];
}

另一种方法是在切换大小写之前创建对象,然后简单地创建纹理并使用setTexture在切换大小写中设置它:

关于ios - Sprite Kit-使用Switch Case将随机的Sprite节点添加到场景中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21780165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com