gpt4 book ai didi

ios - 移动父 SKNode 后,Sprite 套件找不到 nodeAtPoint

转载 作者:可可西里 更新时间:2023-11-01 05:29:08 25 4
gpt4 key购买 nike

你好。

我在 Sprite Kit 中遇到了这个奇怪的问题。我正在使用 nodeAtPointcategoryBitMask在调用跳跃方法时检测玩家是否接触地面。

一切正常。但是然后 - 为了在抽屉中显示一些可选按钮 - 当我用 SKAction moveTo:CGPoint 移动父节点时(我有地面和玩家作为 SKNode 的 child ),玩家不要跳。我 NSLog pointBelowPlayer , 它和以前一样,但是 blockNode.physicsBody一片空白!这可能是 Sprite Kit 中的错误,还是我遗漏了一些关于继承和位置的基本知识?

跳跃的方法:

-(void)playerJump {

// Player jump if on ground
CGPoint pointBelowPlayer = CGPointMake(_player.position.x, _player.position.y-_player.size.height);
SKNode *blockNode = [self nodeAtPoint:pointBelowPlayer];

if (blockNode.physicsBody.categoryBitMask == groundCategory){
[_player.physicsBody applyImpulse:CGVectorMake(0, 120.0f)];
}
}

移动父节点的方法:

-(void)toggleDrawer {
if (bDrawerVisible) {
[_actionLayer runAction:[SKAction moveTo:CGPointMake(0, 0) duration:1]];
bDrawerVisible = false;
} else {
[_actionLayer runAction:[SKAction moveTo:CGPointMake(0, 200) duration:1]];
bDrawerVisible = true;
}
}

SpriteNodes 的创建:

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {

_actionLayer = [SKNode new];

_player = [self createPlayer];
[_actionLayer addChild:_player];

_ground = [self createGround];
[_actionLayer addChild:_ground];
}

-(SKSpriteNode *)createPlayer {

SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"redSquare.png"];
player.name = @"player";
player.scale = 0.5;
player.position = CGPointMake(screenWidth/3, player.size.height*2);
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.collisionBitMask = groundCategory;
player.physicsBody.contactTestBitMask = noteCategory;

return player;
}

-(SKSpriteNode *)createGround {

SKSpriteNode *ground = [SKSpriteNode spriteNodeWithImageNamed:@"ground.png"];
ground.name = @"ground";
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
ground.physicsBody.categoryBitMask = groundCategory;
ground.physicsBody.collisionBitMask = playerCategory;
ground.xScale = screenWidth/ground.size.width;;
ground.position = CGPointMake(self.size.width/2, 0);

return ground;
}

最佳答案

好的,我解决了这个问题......如果你有一个以上的节点,在你点击的位置,如果最深的节点没有名字,或者最深的节点的名字,你将得到 null。

因此使用它来遍历所有找到的节点:

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
NSArray *buttonNodes = [self nodesAtPoint:location];
for (SKNode *node in buttonNodes)
{
NSLog(@"%@", node.name);
}

此外,您可以使用 NSLog(@"%f", node.zPosition); 获取 zPosition 以查看女巫一号在顶部。

关于ios - 移动父 SKNode 后,Sprite 套件找不到 nodeAtPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23521249/

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