“没有碰撞”太过沉重

时间:2014-02-19 10:37:11

标签: performance collision-detection sprite-kit

我打算做一个滚动浏览器,所以我只需要碰撞有大量子弹的玩家。子弹不会与自己发生碰撞。当两个不应该碰撞的精灵通过时,我很失望地看到fps是如何下降的。我已使用此代码段进行了测试:

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
        self.physicsWorld.gravity = CGVectorMake(0, 0);
     }
    return self;
}

-(void)processTouch:(UITouch*)touch
{
    if(!(touch.phase == UITouchPhaseEnded || touch.phase == UITouchPhaseCancelled))
    {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(10, 10)];
        sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size]; //CGSizeMake(1, 1)
        sprite.physicsBody.collisionBitMask = 0;
        sprite.physicsBody.categoryBitMask = 0;
        sprite.physicsBody.contactTestBitMask = 0;

        sprite.name = @"yourNode";

        sprite.position = location;
        [self addChild:sprite];

        double delayInSeconds = 0.1;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));

        __block MyScene *blockSelf = self;
        __block UITouch *blockTouch = touch;

        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [blockSelf processTouch:blockTouch];
        });
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        [self processTouch:touch];
    }
}

结果是:

All nodes are in the same place enter image description here

那我该怎么办?我想我可能会遇到许多节点将在同一个地方(例如,产卵点)的情况。我不应该使用物理机构而只是自己编写碰撞检测吗?

0 个答案:

没有答案