下面的代码将砖块叠加在一起。调用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);
}