gpt4 book ai didi

ios - didBeginContact 方法未按预期工作

转载 作者:行者123 更新时间:2023-12-01 18:54:50 24 4
gpt4 key购买 nike

我有两个节点和一个 bool 值。很简单。当节点 A 联系节点 B 并且 bool 值为 0 时,什么也没有发生。但是,如果 bool 值为 1,则通过 didBeganContact 方法删除节点 A。

非常简单,但是当我想要删除节点 A 时,我遇到了一个烦人的问题。

节点 B 是一个矩形,节点 A 是一个位于矩形中间的正方形,当我使用 touchesBegan 方法点击并按住节点 B 时, bool 值被调用并变为 1。现在在节点 A 接触节点 B 之前,我点击并按住节点 B,当节点 A 接触时,它被移除,但是当节点 A 已经在中间时,我点击节点 B,没有任何 react ,我不知道为什么。

矩形法

-(void)rectangle
{
SKSpriteNode *rectangle = [[SKSpriteNode alloc] init];
rectangle = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(75, 150)];
rectangle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
rectangle.name = @"rect";
rectangle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rectangle.size];
rectangle.physicsBody.categoryBitMask = rectangleCategory;
rectangle.physicsBody.contactTestBitMask = fallingSquareCategory;
rectangle.physicsBody.collisionBitMask = 0;

[self addChild:rectangle];
}

touchesBeganMethod
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if ([node.name isEqualToString:@"rect"])
{
radBool = 1;
}
}

touches已结束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if ([node.name isEqualToString:@"rect"])
{
radBool = 0;
}
}

方法
-(void)square
{
SKAction *move = [SKAction moveToY:CGRectGetMidY(self.frame) duration:1.75];

SKSpriteNode *fallingSquare = [[SKSpriteNode alloc] init];
fallingSquare = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(75, 75)];
fallingSquare.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame));
fallingSquare.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:fallingSquare.size];
fallingSquare.physicsBody.categoryBitMask = fallingSquareCategory;
fallingSquare.physicsBody.contactTestBitMask = rectangleCategory
fallingSquare.physicsBody.collisionBitMask = 0;

[self addChild:fallingSquare];
[fallingSquare runAction:move];
}

开始联系
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 *R1 = nil;
SKSpriteNode *fallingS = nil;

firstBody = contact.bodyA;
secondBody = contact.bodyB;

R1 = nodeFromBody(firstBody, secondBody, rectangleCategory);
fallingS = nodeFromBody(firstBody, secondBody, fallingSquareCategory);

if (R1 && fallingS && radBool == 1)
{
[fallingS removeFromParent];
}
}

最佳答案

我相信您的问题是 didBeginContact 的“开始”部分。它只会在他们第一次联系时被调用,而不是每个循环。因为当他们第一次联系时 bool 没有设置为 YES ,所以永远不会再评估它。

我相信我曾经遇到过这个问题,解决方案是当你触摸它时创建一个新的 body 。这个“应该”触发了 didBeginContact 下一个循环。您也许还可以更改物理 body 上的属性,但如果我没记错的话,我没有让它起作用并且不得不初始化一个新的物理 body 。

例如,尝试使用此更新您的 touchesBegan

touchesBegan方法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if ([node.name isEqualToString:@"rect"])
{
radBool = 1;
node.physicsBody = nil;
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rectangle.size];
node.physicsBody.categoryBitMask = rectangleCategory;
node.physicsBody.contactTestBitMask = fallingSquareCategory;
node.physicsBody.collisionBitMask = 0;
}
}

希望这对你有用。

关于ios - didBeginContact 方法未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28260368/

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