Ios “enumerateChildNodesWithName”方法有时找不到SpriteNode
我正在用Objective-C的Sprite工具包制作一个侧面滚动视频游戏。 当角色从底部跳下时,我希望它能滑过栅栏。 当它从上面跳起来的时候,我想让它爬上栏杆 但是,它有时不起作用 EnumerateChildNodeWithName方法有时找不到SpriteNodeIos “enumerateChildNodesWithName”方法有时找不到SpriteNode,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我正在用Objective-C的Sprite工具包制作一个侧面滚动视频游戏。 当角色从底部跳下时,我希望它能滑过栅栏。 当它从上面跳起来的时候,我想让它爬上栏杆 但是,它有时不起作用 EnumerateChildNodeWithName方法有时找不到SpriteNode -(void)update:(CFTimeInterval)currentTime { /* Called before each frame is rendered */ // LongBar [se
-(void)update:(CFTimeInterval)currentTime
{
/* Called before each frame is rendered */
// LongBar
[self enumerateChildNodesWithName:LONG_BAR usingBlock:^(SKNode *node, BOOL *stop)
{
SKNode *gameCharacter = [ self childNodeWithName:GAME_CHARACTER];
// The character is over the bar
if( node.position.y < gameCharacter.position.y )
{
node.physicsBody.categoryBitMask = CONTACT_BAR;
node.physicsBody.contactTestBitMask = CONTACT_CHARACTER;
}
// The character is under the bar
else
{
node.physicsBody.categoryBitMask = 0;
node.physicsBody.contactTestBitMask = 0;
}
}];
// ShortBar
[self enumerateChildNodesWithName:SHORT_BAR usingBlock:^(SKNode *node, BOOL *stop)
{
// same as longbar
:
:
}];
}
测试节点是否具有该名称,并作为子节点添加到self中-同时发布您如何创建和添加精灵以及哪个短_栏需要注释。我添加了如何创建和添加短栏。
@property SKSpriteNode *longBar;
@property SKSpriteNode *shortBar;
:
:
#define LONG_BAR @"long_bar"
#define SHORT_BAR @"short_bar"
:
:
- (void) addShortBar
{
size_t y;
float x = 0.6f;
y = ( arc4random() % ( (int)self.size.height / 2 ) )+ ( self.size.height / 3 );
self.shortBar.name = SHORT_BAR;
self.shortBar = [ SKSpriteNode spriteNodeWithImageNamed:BAR_SHORT];
self.shortBar.size = CGSizeMake(60 * x , 28 * x);
self.shortBar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.shortBar.size];
self.shortBar.position = CGPointMake(self.size.width, y);
self.shortBar.physicsBody.affectedByGravity = NO;
self.shortBar.physicsBody.dynamic = NO;
self.shortBar.physicsBody.categoryBitMask = CONTACT_BAR;
self.shortBar.physicsBody.contactTestBitMask = (CONTACT_CHARACTER | CONTACT_POINT);
self.shortBar.physicsBody.restitution = 0;
[self.shortBar runAction:[SKAction moveTo:CGPointMake(BAR_IS_OUT, self.shortBar.position.y) duration:_timeBarPasses]];
[self addChild:self.shortBar];
[self addRareItem:y]; // method that add item
}