没有检测精灵之间的接触

时间:2015-01-29 01:16:04

标签: objective-c sprite-kit collision-detection skphysicsbody

在我的应用程序中,我有五个球产生,每秒一个一个地向下移动屏幕。我底部有一个精灵,你必须移动它来躲避球。我当然使用类别位掩码来跟踪对象之间的联系。在我用来创建球的方法中,我有一个动作可以将球向下移动并离开屏幕。我有一份NSLog声明告诉我它是否识别了联系人,但它没有出现。下面的代码是我创造沙子的球,它包括移动和产卵的所有动作。

我认为问题可能是因为球不是动态的,所以我没有参与物理世界,但我不确定替代品是什么。

-(void) addBall:(CGSize) size{
CGFloat xPositions[5] = {8.5,74,138,203,267.5};

int randomIndex = arc4random() % 5;
int randomXPosition = xPositions[randomIndex];

CGRect box = CGRectMake( randomXPosition , self.frame.size.height,    //pos
                        45, 45);   //size
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:box];

SKShapeNode *circle = [SKShapeNode node];
circle.path = circlePath.CGPath;
circle.fillColor = [SKColor colorWithRed:(243.0f/255) green:(134.0f/255) blue:(80.0f/255) alpha:1.0];
circle.strokeColor = nil;
circle.lineWidth = 3;

//add physics to ball
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:circle.frame.size.width/2];
circle.physicsBody.dynamic = NO;
circle.physicsBody.categoryBitMask = ballCategory;
circle.physicsBody.contactTestBitMask = brickCategory;

//appy actions to move sprite
SKAction *move = [SKAction moveToY:-self.size.height - 50 duration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[ move, remove]];
SKAction *repeat = [SKAction repeatActionForever:sequence];

SKAction *spawn = [SKAction performSelector:@selector(addBall:) onTarget:self];
SKAction *delay = [SKAction waitForDuration:.4];
SKAction *spawnThenDelay = [SKAction sequence:@[delay, spawn]];


[self runAction:spawnThenDelay];
[circle runAction:repeat];
[self addChild:circle];
}

当我摆脱runAction重复序列时,我转动重力就会检测到接触,但是如果我重新开启移动动作,它就不会再起作用了。我不能依赖于引力来移动我的精灵,因为它会使x位置出现故障。有什么建议吗?

0 个答案:

没有答案