0

我正在使用 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的属性在每次调用选择器时都会重新初始化,我必须遍历场景的子节点才能重新分配它们。

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

4

1 回答 1

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 更有效。

有什么不清楚的可以追问,希望对你有帮助。

于 2014-03-23T15:46:09.557 回答