SKSpriteNode丢失了physicsBody属性

时间:2014-03-22 21:54:14

标签: ios iphone sprite-kit skspritenode

我正在使用SpriteKit创建一个游戏,其中地图底部的对象会移动。 物体是鳄鱼和硬币。

场景使用NSTimer调用此选择器:

timer  = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(generateBottomSprite) userInfo:nil repeats:YES];


-(void) generateBottomSprite{

int random = (arc4random() % difficulty+3) + difficulty-3;
if (counter >random){
    SKSpriteNode *bottomSprite;
    if (random>difficulty){
        bottomSprite  = [SKSpriteNode spriteNodeWithImageNamed:@"crocodile"];
        bottomSprite.size = CGSizeMake(70, 120);
        bottomSprite.physicsBody.categoryBitMask = crocMask;

    }else{
        bottomSprite = [SKSpriteNode spriteNodeWithImageNamed:@"coin"];
        bottomSprite.size = CGSizeMake(50, 50);
        bottomSprite.physicsBody.categoryBitMask = coinMask;

    }

    //create a crocodile
    bottomSprite.position = CGPointMake(self.frame.size.width,bottomSprite.frame.size.height/2);
    bottomSprite.name = @"bottomSprite";



    [self addChild: bottomSprite];

    counter = 0;

        [self enumerateChildNodesWithName:@"bottomSprite" usingBlock: ^(SKNode *node, BOOL *stop) {
            node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node.frame.size];
            node.physicsBody.affectedByGravity = NO;
            node.physicsBody.mass = 8000.0;
            node.physicsBody.friction = 2.50;
            node.physicsBody.usesPreciseCollisionDetection = YES;

        }];

}

}

问题:每次调用选择器时都会重新初始化physicsWorld的所有属性,我必须遍历场景的子节点才能重新分配它们。

为什么属性会重新初始化?

1 个答案:

答案 0 :(得分:0)

在创建skphysicsbody本身之前设置categorybitmask

bottomSprite  = [SKSpriteNode spriteNodeWithImageNamed:@"crocodile"];
bottomSprite.size = CGSizeMake(70, 120);
//add the following
bottomSprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bottomSprite.frame.size];
///////////////
bottomSprite.physicsBody.categoryBitMask = crocMask;

将所有物理设置移到枚举之外,如此

if (counter >random){
    SKSpriteNode *bottomSprite;
int currentBitMask;
if (random>difficulty){
    bottomSprite  = [SKSpriteNode spriteNodeWithImageNamed:@"crocodile"];
    bottomSprite.size = CGSizeMake(70, 120);
    currentBitMask = crocMask;
}else{
    bottomSprite = [SKSpriteNode spriteNodeWithImageNamed:@"coin"];
    bottomSprite.size = CGSizeMake(50, 50);
    currentBitMask  = coinMask;

}

**bottomSprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bottomSprite.frame.size];**
bottomSprite.physicsBody.categoryBitMask = currentBitMask ;
bottomSprite.physicsBody.affectedByGravity = NO;
bottomSprite.physicsBody.mass = 8000.0;//way too high
bottomSprite.physicsBody.friction = 2.50;//probably high
bottomSprite.physicsBody.usesPreciseCollisionDetection = YES;//overkill, set only if critical


//create a crocodile
bottomSprite.position = CGPointMake(self.frame.size.width,bottomSprite.frame.size.height/2);
bottomSprite.name = @"bottomSprite";

[self addChild: bottomSprite];

你也可能想利用skscene和nstimeintervals中的update方法来测量时间,直到你传递半秒然后调用generateBottomSprite方法,它应该比使用额外的NSTimer更有效。

随意询问是否有任何不清楚的地方,希望这会有所帮助。