物理机构没有反弹?

时间:2014-02-24 07:34:17

标签: objective-c sprite-kit

我的代码非常简单:

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor whiteColor];
        self.anchorPoint = CGPointMake(0.5, 0.5);
        self.physicsWorld.gravity = CGVectorMake(0, -1.0);
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        self.physicsBody.dynamic = YES;
        self.physicsBody.affectedByGravity = NO;
        self.physicsBody.categoryBitMask = wallCategory;
        self.physicsBody.collisionBitMask = nodeCategory;
        self.physicsBody.contactTestBitMask = nodeCategory;
        self.physicsWorld.contactDelegate = self;
        SKSpriteNode *node1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)];
        SKSpriteNode *node2 = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(20, 20)];
        node2.position = CGPointMake(0, -node1.frame.size.height/2+node2.frame.size.height/2);
        node1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node1.frame.size];
        node1.physicsBody.dynamic = YES;
        node1.physicsBody.categoryBitMask = nodeCategory;
        node1.physicsBody.collisionBitMask = wallCategory;
        node1.physicsBody.contactTestBitMask = wallCategory;
        node1.physicsBody.restitution = 1.0;
        node1.physicsBody.friction = 0.0;
        node1.physicsBody.linearDamping = 0.0;
        node1.physicsBody.angularDamping = 0.0;

        [node1 addChild:node2];
        [self addChild:node1];
    }
    return self;
}

然而,节点1只是通过(受重力影响)屏幕而没有从底部反弹。我做错了什么?

1 个答案:

答案 0 :(得分:0)

给node1一个位置:

node1.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/2);