Objective c 已尝试添加已具有父节点的SKNode
目前正试图找到一个问题的解决方案,该问题正在损害我的应用程序。我想看看是否可以更改以下代码以减少内存需求。现在,我有五种方法,它们都是这样做的Objective c 已尝试添加已具有父节点的SKNode,objective-c,sprite-kit,Objective C,Sprite Kit,目前正试图找到一个问题的解决方案,该问题正在损害我的应用程序。我想看看是否可以更改以下代码以减少内存需求。现在,我有五种方法,它们都是这样做的 -(void)createObstacle0 { int yMin = (CGRectGetMidY(self.frame)+190); int yMax = (CGRectGetMidY(self.frame)+270); CGPoint startPoint = CGPointMake(-20, yMin + arc4random_uni
-(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,但是我得到了上面的错误。有人能告诉我如何更改代码,使图像障碍物只加载一次。从您发布的代码中可以看出,您正在运行一个无休止的循环来创建障碍物对象 尝试删除代码行[self-performSelector:@selectorcreateObstacle0 withObject:nil afterDelay:randomNum] 添加如下计时器功能:
- (void)startTimer
{
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerAction)
userInfo:nil
repeats:YES];
}
-(void)timerAction
{
NSLog(@"do something");
}
您可以这样调用startTimer方法来启动startTimer方法[self startTimer]
您可以从init方法或任何需要的地方执行此操作。这里可能有一个更像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"]; }
是的,我知道,对象一直在生成,直到玩家击中其中一个对象并切换场景,这是故意的。@user3552678-在你移除线后它工作了吗?它工作了,没有崩溃或任何事情,但我的游戏有很多对象从侧面来,没有那条线,它们不会继续来,只有一个会。@user3552678-我已经修改了代码示例,添加了一个计时器,用于替换您删除的计时器。这与我的有什么不同?当我试着运行一个以上的时候,它会运行数百次