gpt4 book ai didi

ios - 敌人节点偶尔会卡在墙上

转载 作者:行者123 更新时间:2023-11-29 02:41:49 26 4
gpt4 key购买 nike

我的敌人节点在有边缘的平台上生成,我不希望我的敌人变成旅鼠然后掉下来,所以我创建了两个独立的 Sprite 节点作为隐形墙并将墙设置为 连接到平台上的接头,这样当敌人接触到任何一面墙时,他们就会返回。

问题是有时敌方节点会完全忽略接触,只是跑到接触应该起作用的地方。

我使用 NSLogs 来跟踪敌人节点是否接触到墙壁,但就像我说的那样,敌人完全忽略并且我没有收到一条日志说敌人已经接触到墙壁因此不会出现联系方式。

我想通过让墙壁永远重复一个 Action 来纠正这个问题,将它们向后移动一个像素然后向前移动一个像素,但是这些 Action 似乎没有正常工作,所以我不知道如何修复它。任何信息将不胜感激。

平台生成器方法

-(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;
}

}

最佳答案

您在 didBeginContact: 中的检查没有考虑到不能保证 SKPhysicsContact 中的哪个物体是哪个物体。在获取 playerenemycontactLineBody1contactLineBody2 节点时,您似乎已经考虑到了这一点> 不过,我们可以在您的 if 语句中使用它们:

代替:

if (((firstBody.categoryBitMask & fEnemyCategory) != 0) && ((secondBody.categoryBitMask & fContactEnemyCategory1) != 0))

// ...

if (((firstBody.categoryBitMask & fEnemyCategory) != 0) && ((secondBody.categoryBitMask & fContactEnemyCategory2) != 0))

只是做:

if (enemy && contactLineBody1)

// ...

if (enemy && contactLineBody2)

关于ios - 敌人节点偶尔会卡在墙上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25676724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com