Ios 雪碧套件不';移动父节点后找不到节点点
你好。 我在雪碧套装里遇到了一个奇怪的问题。我正在使用Ios 雪碧套件不';移动父节点后找不到节点点,ios,objective-c,collision-detection,sprite-kit,Ios,Objective C,Collision Detection,Sprite Kit,你好。 我在雪碧套装里遇到了一个奇怪的问题。我正在使用nodeAtPoint和categoryBitMask来检测玩家在调用跳跃方法时是否触地 一切正常。但是,为了显示抽屉中的一些可选按钮,当我使用SKAction moveTo:CGPoint移动父节点时(我将地面和播放器作为SKNode的子节点),播放器不会跳。我将pointBelowPlayer记录下来,它与以前一样,但是blockNode.physicsBody为空!这可能是Sprite工具包中的一个bug,还是我缺少了一些关于继承和位置
nodeAtPoint
和categoryBitMask
来检测玩家在调用跳跃方法时是否触地
一切正常。但是,为了显示抽屉中的一些可选按钮,当我使用SKAction moveTo:CGPoint
移动父节点时(我将地面和播放器作为SKNode
的子节点),播放器不会跳。我将pointBelowPlayer
记录下来,它与以前一样,但是blockNode.physicsBody
为空!这可能是Sprite工具包中的一个bug,还是我缺少了一些关于继承和位置的基本信息
跳跃的方法:
-(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;
}
}
-(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;
}
移动父节点的方法:
-(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;
}
}
-(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;
}
创建SpriteNodes:
-(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;
}
}
-(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;
}
点上的节点可能是另一个节点(可能是您的背景?)。
使用node.name来命名节点,您可以通过将名称与equalToString:method进行比较来检查节点是否相同。好的,我解决了这个问题。。。 如果您有多个节点,在您点击的位置上,如果最深的节点没有名称,或者最深节点的名称,您将得到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)代码>以获得Z位置以查看“女巫一号”位于顶部。感谢您的快速回复!这是开发的早期阶段,我目前没有任何背景-node.name在父级移动之后返回(null)(之前为“ground”),因此没有其他nodeAtPoint。@Brean它将返回null,因为名称未设置。再次为节点和日志名设置不同的名称,看看会发生什么。地面节点已经命名,正如您从上面的代码中看到的。如果我将节点记录在pointBelowPlayer,它将返回:name:'(null)'frame:{{0,0},{1024,768}
,就好像地面不在那里一样。只是为了澄清一下,nodeAtPoint返回该点最深的节点,nodeAtPoint返回该点所有节点的数组。这解决了我用SK应用程序从iOS7移动到iOS8的问题。谢谢