我有一个方法可以创建一条增加分数的线,并且该方法会在间隔中被调用,并且每当有两条或更多条相同的线时,每当英雄接触最接近他的线时,我都会执行removeFromParent Action和相反,最远的线将被删除。但是,如果在生成其他线之前先接触第一条线,则不会发生此问题。我该如何纠正?线路连接到中间的平台。
platformGenerationMethod
-(void)createPlatform {
switch (arc4random_uniform(2)) {
case (0):
objectTexture = [SKTexture textureWithImageNamed:@"shortPlatform3"];
platformCount ++;
NSLog(@"%li", (long)platformCount);
break;
case (1):
objectTexture = [SKTexture textureWithImageNamed:@"shortPlatform3"];
platformCount ++;
NSLog(@"%li",(long)platformCount);
break;
case (2):
objectTexture = [SKTexture textureWithImageNamed:@"shortPlatform3"];
platformCount ++;
NSLog(@"%li", (long)platformCount);
default:
break;
}
variaPlatform = [SKSpriteNode spriteNodeWithTexture:objectTexture];
variaPlatform.name = @"variaPlatform";
variaPlatform.zPosition = 2;
variaPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:variaPlatform.size];
variaPlatform.physicsBody.friction = 1;
variaPlatform.physicsBody.linearDamping = 1;
variaPlatform.physicsBody.categoryBitMask = fPlatformCategory;
variaPlatform.physicsBody.contactTestBitMask = fPlayerCategory | fEnemyCategory;
variaPlatform.physicsBody.collisionBitMask = fPlayerCategory | fEnemyCategory;
variaPlatform.physicsBody.dynamic = NO;
variaPlatform.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
variaPlatform.position = CGPointMake(700, variaPlatform.position.y - 129);
scoreLine = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(0.5, 700)];
scoreLine.hidden = NO;
scoreLine.physicsBody.dynamic = NO;
scoreLine.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:scoreLine.size];
scoreLine.position = CGPointMake(variaPlatform.position.x, variaPlatform.position.y/2);
scoreLine.physicsBody.categoryBitMask = fScoreCategory;
scoreLine.physicsBody.contactTestBitMask = fPlayerCategory;
scoreLine.physicsBody.collisionBitMask = 0;
[self addChild:scoreLine];
[self addChild:variaPlatform];
[self connectNode1:variaPlatform toNode2:scoreLine];
[variaPlatform runAction:variaActions];
}
didBeginContact
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if (((firstBody.categoryBitMask & fPlayerCategory) !=0) && ((secondBody.categoryBitMask & fScoreCategory) != 0))
{
NSLog(@"score");
currentScore++;
[scoreLine runAction:removeFromParent];
[currentScoreLabel setText:[NSString stringWithFormat:@"%ld", (long)currentScore]];
}
}
意见将不胜感激。
最佳答案
这应该允许您删除正确的分数节点...
// Returns a node that matches the specified category or nil
static inline SKSpriteNode *nodeFromBody(SKPhysicsBody *body1,
SKPhysicsBody *body2,
uint32_t category) {
SKSpriteNode *node = nil;
if (body1.categoryBitMask & category) {
node = (SKSpriteNode *)body1.node;
}
else if (body2.categoryBitMask & category) {
node = (SKSpriteNode *)body2.node;
}
return node;
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
SKSpriteNode *player = nil;
SKSpriteNode *score = nil;
SKSpriteNode *enemy = nil;
firstBody = contact.bodyA;
secondBody = contact.bodyB;
player = nodeFromBody(firstBody, secondBody, fPlayerCategory);
score = nodeFromBody(firstBody, secondBody, fScoreCategory);
enemy = nodeFromBody(firstBody, secondBody, fEnemyCategory);
// Test for contact between nodes
if (player) {
if (score) {
NSLog(@"score");
currentScore++;
// remove the node that made contact with player
[score runAction:removeFromParent];
[currentScoreLabel setText:[NSString stringWithFormat:@"%ld", (long)currentScore]];
}
else if (enemy) {
// Do something
}
}
}
关于ios - removeFromParent操作不执行预期功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25283674/