目前正试图找到一个解决我现在的应用程序问题的解决方案。我想知道我是否可以更改以下代码以减少内存需求。现在,我有五种方法都可以做到以下几点,
-(void)createObstacle0 {
int yMin = (CGRectGetMidY(self.frame)+190);
int yMax = (CGRectGetMidY(self.frame)+270);
CGPoint startPoint = CGPointMake(-20, yMin + arc4random_uniform(yMax - yMin));
SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithImageNamed:@"obstacle"];
obstacle.position = CGPointMake(startPoint.x, startPoint.y);
obstacle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21.5];
obstacle.physicsBody.categoryBitMask = enemyCategory;
obstacle.physicsBody.contactTestBitMask = playerCategory;
obstacle.physicsBody.dynamic = NO;
obstacle.name = @"obstacle0";
[self addChild:obstacle];
[obstacle runAction:[SKAction moveTo:CGPointMake(340, startPoint.y) duration:minTime + arc4random_uniform(maxTime - minTime)]];
float randomNum = arc4random_uniform(3.0) + 0.1;
[self performSelector:@selector(createObstacle0) withObject:nil afterDelay:randomNum];
}
我尝试在我的.h文件中声明一个SKSpriteNode并让每个方法使用它而不是在它们内部声明的SKSpriteNode,但是我得到了上面的错误。有人可以告诉我如何更改我的代码,以便图像"障碍"只加载一次。
答案 0 :(得分:0)
从你发布的代码中看来,你正在运行一个无休止的循环来创建障碍物对象。
尝试删除代码行[self performSelector:@selector(createObstacle0) withObject:nil afterDelay:randomNum];
添加这样的计时器功能:
- (void)startTimer
{
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerAction)
userInfo:nil
repeats:YES];
}
-(void)timerAction
{
NSLog(@"do something");
}
您可以通过调用此startTimer
[self startTimer];
方法
您可以使用init方法或其他任何需要的方法执行此操作。
答案 1 :(得分:0)
这里可能有点像“SpriteKit”这样的回答。您还可以将大量创建过程放入后台线程中的块中,然后在主线程中执行添加和操作,具体取决于应用程序中其他内容的强度。下面的例子的关键是它正在使用动作,并且序列有一个键。
-(void)createObstacleRepeatingMethod {
int yMin = (CGRectGetMidY(thingShowingObstacles.frame)+190);
int yMax = (CGRectGetMidY(thingShowingObstacles.frame)+270);
CGPoint startPoint = CGPointMake(-20, yMin + arc4random_uniform(yMax - yMin));
SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithImageNamed:@"obstacle"];
obstacle.position = CGPointMake(startPoint.x, startPoint.y);
obstacle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21.5];
obstacle.physicsBody.categoryBitMask = obstacleCategory;
obstacle.physicsBody.contactTestBitMask = playerCategory;
obstacle.physicsBody.dynamic = NO;
obstacle.name = @"obstacle0";
[thingShowingObstacles addChild:obstacle];
[obstacle runAction:[SKAction moveTo:CGPointMake(340, startPoint.y) duration:minTime + arc4random_uniform(maxTime - minTime)]];
SKAction *waitAction = [SKAction waitForDuration:1.5 withRange:1.4];
SKAction *callCreateObstacleAgain = [SKAction runBlock:^{
[thingShowingObstacles createObstacleRepeatingMethod];
}];
SKAction *theSequenceForWaitThenCall = [SKAction sequence:@[waitAction, callCreateObstacleAgain]];
[thingShowingObstacles runAction:theSequenceForWaitThenCall withKey:@"createObstacleSequence"];
//the sequence having a key also ensures that there will only ever be one of the sequence actions running
//it also ensures that you have fine grained control over removing the action by calling
[thingShowingObstacles removeActionForKey:@"createObstacleSequence"]; }