[英]how to randomly add sprites to scene
Im trying to add multiple images to my scene randomly. 我试图随机添加多个图像到我的场景。 I have 4 images named Gem1, Gem2, Gem3, and Gem4.
我有4个名为Gem1,Gem2,Gem3和Gem4的图像。 I'm stuck.
我被卡住了。 Here's the code i have so far but there are errors.
这是我到目前为止的代码,但是有错误。 Can someone help me please?
有人能帮助我吗?
- (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],]]];
}
Just remove the "". 只需删除“”即可。 :D
:D
From 从
SKSpriteNode * Gem = [SKSpriteNode spriteNodeWithImageNamed:@"GemName"];
To 至
SKSpriteNode * Gem = [SKSpriteNode spriteNodeWithImageNamed:GemName];
Need you to post the "addGem" method to help you with the position issue; 需要您发布“ addGem”方法来帮助您解决职位问题; if you are having any.
如果有的话。
SKSpriteNode * Gem = [SKSpriteNode spriteNodeWithImageNamed:@"GemName"];
这里GemName
是一个变量,不应将其用""
包裹,只需将其删除即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.