如何使这个构建块代码更灵活/更有效?

时间:2016-03-07 22:41:04

标签: ios objective-c sprite-kit

下面的代码将砖块叠加在一起。调用TouchesBegin时,会添加新砖。

我是编码的新手。我希望代码更灵活,因此也可以添加不同类型的砖块。

感谢您的帮助。

CODE

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

if (integer == 0) {
    [_previousBrick removeAllActions];
    [self spawnNextBrick];
}
if (integer == 1) {

    [self spawnPrevBrick];
    }
}

-(void)spawnPrevBrick
{
    _previousBrick = [SKSpriteNode spriteNodeWithImageNamed:@"TestBridgePiece"];
    _previousBrick.anchorPoint = CGPointMake(0.5, 0.0);
    _previousBrick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_previousBrick.frame.size];
    _previousBrick.position = CGPointMake(0.0, _nextBrick.position.y + _previousBrick.size.height);
    _previousBrick.physicsBody.categoryBitMask = bridgeCategory;
    _previousBrick.physicsBody.contactTestBitMask = leftEdgeCategory | rightEdgeCategory | newBrickNotificator;
    _previousBrick.physicsBody.collisionBitMask = bridgeCategory;
    _isPreviousBrick = YES;
    [_previousBrickLayer addChild:_previousBrick];
}

-(void)spawnNextBrick
{
    _nextBrick = [SKSpriteNode spriteNodeWithImageNamed:@"TestBridgePiece"];
    _nextBrick.anchorPoint = CGPointMake(0.5, 0.0);
    _nextBrick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_nextBrick.frame.size];
    _nextBrick.position = CGPointMake(self.size.width, _previousBrick.position.y + _nextBrick.size.height);
    _nextBrick.physicsBody.contactTestBitMask = leftEdgeCategory | rightEdgeCategory | newBrickNotificator;
    _nextBrick.physicsBody.collisionBitMask = bridgeCategory;
    _isPreviousBrick = NO;
    [_previousBrickLayer addChild:_nextBrick];
}
-(void)update:(CFTimeInterval)currentTime {
 if (_isPreviousBrick == NO) {
        integer = 1;
    }
    if (_isPreviousBrick == YES) {
        integer = 0;
    }

        NSLog(@"integer = %i", integer);
}

0 个答案:

没有答案