Spritekit激烈的帧速率下降

时间:2014-04-23 23:19:43

标签: ios sprite-kit frame-rate skphysicsbody

我尽力让这个问题尽可能简单。我的游戏中有一个硬币对象:

@implementation
-(CollectableCoin*)initWithLocation:(CGPoint) Location andValue: (int) val
{
self = [super initWithImageNamed:@"coin"];
[self setScale:.35];
_value = val;
_collected = false;

self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.categoryBitMask = APAColliderTypeCoin;
self.physicsBody.collisionBitMask = APAColliderTypeBall;
self.physicsBody.mass = 0.00009;
self.physicsBody.restitution = .35;
self.position = Location;
self.name = @"collectableCoin";
return self;
}
@end

我还有一个货架对象:

@implementation Shelf

-(Shelf*)initWithLocation:(CGPoint) location andWidth:(NSInteger) width
{
self = [super initWithImageNamed:@"shelf"];
if(self)
{
    self.size = CGSizeMake(width, HEIGHT);

    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
    self.physicsBody.dynamic = false;
    self.physicsBody.restitution = 0;
    self.position = location;
    self.name = @"shelf";

    SKSpriteNode* topOfShelf;

    if(width > 5)
        topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width-2, 1)];
    else
        topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width, 1)];

    topOfShelf.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:topOfShelf.size];
    topOfShelf.physicsBody.restitution = 1;
    topOfShelf.physicsBody.dynamic = false;
    topOfShelf.position = CGPointMake(0, location.y + self.size.height/2);
    NSLog([NSString stringWithFormat:@"%f", location.y + self.size.height/2]);
    NSLog([NSString stringWithFormat:@"%f", location.y]);
    topOfShelf.name = @"shelf";

    [self addChild:topOfShelf];
}
return self;
}

@end

我创建了一个这样的场景:

-(id)initWithSizeTest:(CGSize)size
{

self.physicsWorld.gravity = CGVectorMake(0, 0);
_gameState = READYTOSTART;
self.physicsWorld.contactDelegate = self;

if (self = [super initWithSize:size])
{
    self.physicsWorld.gravity = CGVectorMake(0, 0);

    for(int i = 0; i < 25; i++)
    {
        CollectableCoin* orb = [[CollectableCoin alloc] initWithLocation:CGPointMake(i*10, self.size.height*.75) andValue:1];
        [self addChild:orb];
    }

    Shelf* shelf = [[Shelf alloc] initWithLocation:CGPointMake(self.size.width/2, self.size.height/2) andWidth:self.size.width];
    [self addChild:shelf];
}
return self;

}

这是touchesBegan方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if(_gameState == READYTOSTART)
{
    self.physicsWorld.gravity = CGVectorMake(0, -2.0);
    _gameState = PLAYING;
    [[self childNodeWithName:@"taptostart"] removeFromParent];
}

当场景开始时,我有一排硬币悬停在架子上方,重力被禁用,我有一个稳固的60fps。当我点击屏幕时,touchesbegan功能使重力和硬币落在架子上,帧速率下降到5fps。不会调用didBeginContact函数,因为shelf对象不是动态的,也没有接触或碰撞位掩码,所以我很确定它不会因为对didBeginContact的无关调用而过载。这种情况发生在iPad mini和iPhone 4s上,但不适用于任何模拟器。这是我遇到的实际问题的一个非常简单的例子。有人有任何见解吗?

0 个答案:

没有答案