我试图随机添加多个图像到我的场景。我有4个名为Gem1,Gem2,Gem3和Gem4的图像。我被卡住了。这是我到目前为止的代码,但有错误。有人能帮帮我吗?
- (void)addGem {
// Create sprite
NSString *base = @"Gem";
uint32_t num = arc4random_uniform(4) + 1; //Generate a random number
NSString *GemName = [base stringByAppendingFormat:@"%d.png", num];
SKSpriteNode * Gem = [SKSpriteNode spriteNodeWithImageNamed:@"GemName"];
_gem = Gem;
Gem.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Gem.size]; // 1
Gem.physicsBody.dynamic = YES; // 2
Gem.physicsBody.categoryBitMask = GemCategory; // 3
Gem.physicsBody.contactTestBitMask = monsterCategory; // 4
Gem.physicsBody.collisionBitMask = 0;
int minx = 200;
int maxx = 1000;
int rangex = maxx - minx;
int actualx = (arc4random() % rangex) + minx;
int minY = 900;
int maxY = 1200;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
Gem.position = CGPointMake(actualx, actualY);
[_background addChild:Gem];
[self runAction:[SKAction sequence:@[
[SKAction waitForDuration:5],
[SKAction performSelector:@selector(addGem) onTarget:self],]]];
}
答案 0 :(得分:0)
只需删除“”即可。 :d
来自
SKSpriteNode * Gem = [SKSpriteNode spriteNodeWithImageNamed:@"GemName"];
要
SKSpriteNode * Gem = [SKSpriteNode spriteNodeWithImageNamed:GemName];
您需要发布“addGem”方法来帮助您解决职位问题;如果你有任何。
答案 1 :(得分:0)
SKSpriteNode * Gem = [SKSpriteNode spriteNodeWithImageNamed:@"GemName"];
此处GemName
是一个变量,它不应该被""
包裹,只需删除它。