我有敌人的节点在有边缘的平台上产生,我不希望我的敌人成为旅行者并且脱落,所以我创建了两个独立的精灵节点作为隐形墙,并将墙设置为将关节连接到平台,以便当敌人接触任何一面墙时,它们会转回。
问题是,有时候敌人的节点完全忽略了联系,只是恰好在联系人应该工作的地方运行。
我使用NSLog来跟踪敌人的节点是否与墙壁接触,但就像我说的那样,敌人完全忽略那个并且我没有收到日志说敌人已经联系了墙壁所以联系方式不会发生。
我想通过让墙壁永远重复一个动作将它们向后移动一个像素然后向前移动一个像素来纠正这个问题,但是这些动作似乎没有正常工作,所以我不知道如何修复它。任何信息都将很高兴。
平台生成器方法
-(void)createPlatform {
SKAction *shake1 = [SKAction moveByX:1 y:0 duration:.1];
SKAction *shake2 = [SKAction moveByX:-1 y:0 duration:.1];
SKAction *shake3 = [SKAction sequence:@[shake1, shake2]];
contactLineShake = [SKAction repeatActionForever:shake3];
variaPlatform = [SKSpriteNode spriteNodeWithImageNamed:@"shortPlatform3-Checkered"];
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);
contactLine1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(5, 21)];
contactLine1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:contactLine1.size];
contactLine1.hidden = NO;
contactLine1.position = CGPointMake(variaPlatform.position.x - 107, variaPlatform.position.y + 36);
contactLine1.physicsBody.categoryBitMask = fContactEnemyCategory1;
contactLine1.physicsBody.contactTestBitMask = fEnemyCategory | fMainPlatformCategory;
contactLine1.physicsBody.collisionBitMask = 0;
contactLine2 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(5, 21)];
contactLine2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:contactLine2.size];
contactLine2.hidden = NO;
contactLine2.position = CGPointMake(variaPlatform.position.x + 107, variaPlatform.position.y + 36);
contactLine2.physicsBody.categoryBitMask = fContactEnemyCategory2;
contactLine2.physicsBody.contactTestBitMask = fEnemyCategory | fMainPlatformCategory;
contactLine2.physicsBody.collisionBitMask = 0;
[self addChild:contactLine1];
[self addChild:contactLine2];
[self addChild:variaPlatform];
[self connectNode1:variaPlatform toNode2:contactLine1];
[self connectNode1:variaPlatform toNode2:contactLine2];
[variaPlatform runAction:variaActions];
[contactLine1 runAction:contactLineShake];
[contactLine2 runAction:contactLineShake];
}
联系方式
// Returns a node that matches the specified category.
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 *enemy = nil;
SKSpriteNode *contactLineBody1 = nil;
SKSpriteNode *contactLineBody2 = nil;
firstBody = contact.bodyA;
secondBody = contact.bodyB;
player = nodeFromBody(firstBody, secondBody, fPlayerCategory);
enemy = nodeFromBody(firstBody, secondBody, fEnemyCategory);
contactLineBody1 = nodeFromBody(firstBody, secondBody, fContactEnemyCategory1);
contactLineBody2 = nodeFromBody(firstBody, secondBody, fContactEnemyCategory2);
if (((firstBody.categoryBitMask & fEnemyCategory) != 0) && ((secondBody.categoryBitMask & fContactEnemyCategory1) != 0)) {
[enemy removeAllActions];
[enemy runAction:runReverseForeverAnimation];
[enemy runAction:enemyMoveRight1];
contactLeft++;
if (contactLeft >= 1) {
[enemy removeAllActions];
[enemy runAction:runReverseForeverAnimation];
[enemy runAction:enemyMoveRight1];
[enemy.physicsBody applyImpulse:CGVectorMake(1, 0)];
}
NSLog(@"contact Left %i", contactLeft);
contactRight = 0;
}
if (((firstBody.categoryBitMask & fEnemyCategory) != 0) && ((secondBody.categoryBitMask & fContactEnemyCategory2) != 0)) {
[enemy removeAllActions];
[enemy runAction:runForeverAnimation];
[enemy runAction:enemyMoveLeft1];
contactRight++;
if (contactRight >= 1) {
[enemy removeAllActions];
[enemy runAction:runForeverAnimation];
[enemy runAction:enemyMoveLeft1];
[enemy.physicsBody applyImpulse:CGVectorMake(-1, 0)];
}
NSLog(@"contact Right %i", contactRight);
contactLeft = 0;
}
}
答案 0 :(得分:1)
didBeginContact:
中的结帐不会导致我们无法保证SKPhysicsContact
中哪个机构是哪个机构。看起来您已经在获取player
,enemy
,contactLineBody1
和contactLineBody2
的节点时将其考虑在内,因此我们可以在{ {1}}陈述:
而不是:
if
只是做:
if (((firstBody.categoryBitMask & fEnemyCategory) != 0) && ((secondBody.categoryBitMask & fContactEnemyCategory1) != 0))
// ...
if (((firstBody.categoryBitMask & fEnemyCategory) != 0) && ((secondBody.categoryBitMask & fContactEnemyCategory2) != 0))