iOS 8如何防止Spritekit帧速率下降
在我创建的一个游戏中,我创建了一个游戏模式,玩家有30秒的时间来击中尽可能多的目标。我在-(void)update:(CFTimeInterval)currentTime方法中添加了一个30秒的倒计时计时器。当开始倒计时时,帧速率显著下降,并且每当对象与目标碰撞时,都会使用99%的cpu。计时器未运行时不会发生这种情况。是否有其他方法添加倒计时计时器?如果没有,我如何减少cpu使用并保持帧速率不变?我使用的是Xcode 6.1,代码如下。这也是iOS 8的一个问题。在iOS 7.1上运行良好iOS 8如何防止Spritekit帧速率下降,ios,objective-c,timer,sprite-kit,frame-rate,Ios,Objective C,Timer,Sprite Kit,Frame Rate,在我创建的一个游戏中,我创建了一个游戏模式,玩家有30秒的时间来击中尽可能多的目标。我在-(void)update:(CFTimeInterval)currentTime方法中添加了一个30秒的倒计时计时器。当开始倒计时时,帧速率显著下降,并且每当对象与目标碰撞时,都会使用99%的cpu。计时器未运行时不会发生这种情况。是否有其他方法添加倒计时计时器?如果没有,我如何减少cpu使用并保持帧速率不变?我使用的是Xcode 6.1,代码如下。这也是iOS 8的一个问题。在iOS 7.1上运行良好 -
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
//reset counter if starting
int highscoret = [[NSUserDefaults standardUserDefaults] integerForKey: @"highScoret"];
if (startGamePlay){
startTime = currentTime;
startGamePlay = NO;
}
countDown.text = [NSString stringWithFormat:@"Start!"];
int countDownInt = 30.0 -(int)(currentTime-startTime);
if(countDownInt>0){ //if counting down to 0 show counter
countDown.text = [NSString stringWithFormat:@"%i", countDownInt];
}else if(countDownInt==0) { //if not show message, dismiss, whatever you need to do.
countDown.text=@"Time's Up!";
self.highScoret.text = [NSString stringWithFormat:@"%d Baskets",highscoret];
SKScene *endGameScene = [[GameOverT alloc] initWithSize:self.size];
SKTransition *reveal = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:endGameScene transition:reveal];
[self saveScore2];
if (self.points > highscoret) {
self.highScoret.text = [NSString stringWithFormat:@"%d Points",highscoret];
[self saveScore];
self.points = 0;
self.scoreLabel.text = [NSString stringWithFormat:@"%d Points",_points];
}
else {
self.points = 0;
self.scoreLabel.text = [NSString stringWithFormat:@"%d Points",_points];
self.highScoret.text = [NSString stringWithFormat:@"%d Points",highscoret];
}
}
}
这是其他相关代码
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(countDown.frame, location)){
startGamePlay = YES;
self.baskett = 0;
[self reset];
}
}
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKSpriteNode * object = [SKSpriteNode spriteNodeWithImageNamed:@"object.png"];
object.position = self.object2.position;
object.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];
object.physicsBody.dynamic = YES;
object.physicsBody.categoryBitMask = objectCategory;
object.physicsBody.contactTestBitMask = targetCategory;
object.physicsBody.collisionBitMask = 0;
object.physicsBody.usesPreciseCollisionDetection = YES;
CGPoint offset = rwSub(location, object.position);
if (offset.y <= 0) return;
[self addChild:object];
CGPoint direction = rwNormalize(offset);
CGPoint shootAmount = rwMult(direction, 1000);
CGPoint realDest = rwAdd(shootAmount, object.position);
float velocity = 200/1.0;
float realMoveDuration = self.size.width / velocity;
SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[object runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
}
-(void)object:(SKSpriteNode *) object didCollideWithTarget:(SKSpriteNode *) target {
[object removeFromParent];
[[self scene] runAction:[SKAction playSoundFileNamed:@"effect2.wav" waitForCompletion:NO]];
self.points++;
self.scoreLabel.text = [NSString stringWithFormat:@"%d Points",_points];
if (self.points == 3) {
[self obstacle];
}
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
/*当触摸开始时调用*/
用于(UITouch*触摸屏){
CGPoint位置=[触摸位置Innode:self];
if(CGRectContainsPoint(countDown.frame,location)){
startGamePlay=是;
self.baskett=0;
[自复位];
}
}
UITouch*touch=[触摸任何对象];
CGPoint位置=[触摸位置Innode:self];
SKSpriteNode*对象=[SKSpriteNode SPRITENODEWITHIMAGENAME:@“object.png”];
object.position=self.object2.position;
object.physicsBody=[SKPhysicsBody-withcircleofradius:ball.size.width/2];
object.physicsBody.dynamic=是;
object.physicsBody.categoryBitMask=objectCategory;
object.physicsBody.contactTestBitMask=targetCategory;
object.physicsBody.collisionBitMask=0;
object.physicsBody.usesprecisecollisondetection=YES;
CGPoint offset=rwSub(位置,object.position);
如果在更新函数中使用(offset.y计时器是个坏主意,那么像这样使用带有waitForDuration的skaction可能会解决您的问题
SKAction *updateTime= [SKAction sequence:@[
///fire function after one second
[SKAction waitForDuration:1.0f],
[SKAction performSelector:@selector(callFunction)
onTarget:self]
]];
//where 30 is number of time you want’s to call your function
[self runAction:[SKAction repeatAction:updateTime count:30] ];
-(void) callFunction
{
//what ever you want to do
}
使用仪器来找出到底是什么消耗了CPU时间,否则这只是猜测,可能需要比学习使用仪器花费的时间更长的时间才能弄清楚。这会更好!是否有任何方法可以使计时器可见?创建一个变量来保存计时器执行的时间数(NSinteger t=100)同时创建一个SKLabelNode,将其赋值为t,并每次减少t值一-(void)callFunction excuteI已经尝试过类似的操作,但是LabelNodes显示在彼此的顶部。我尝试创建可以删除和添加新LabelNodes的SKActions,但是效果不好,所以我决定不使用它。感谢您的帮助!为什么创建serval synodes只需在callFunction中创建一个更新其skiable.text属性