0

我想用 IOS SpriteKit 做一个足球

我能够在代码中创建一个球,如下所示。但是,它根本不会反弹。为了使反弹率,我该怎么做?

(UITouch *touch in touches) {

 SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Ball"];
 sprite.position = location;
 const CGFloat radius = 20.0f;
 SKPhysicsBody *pbody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
 sprite.physicsBody = pbody;
 sprite.size = (CGSize){radius * 2, radius * 2};
 sprite.physicsBody.linearDamping=0.12f;
 [self addChild:sprite];

}

我是这样声明的。   

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:(CGRect){CGPointZero, size}];

    self.physicsWorld.gravity=CGVectorMake(0, -100);
}

return self;

}

4

1 回答 1

1

这对我来说就像一个球

- (void) soccerBallExample
{
    // the world bounds
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0, 0, self.size.width, self.size.height)];

    // the ball
    SKShapeNode *ball;
    ball = [[SKShapeNode alloc] init];
    CGMutablePathRef myPath = CGPathCreateMutable();
    CGPathAddArc(myPath, NULL, 0,0, 30, 0, M_PI*2, YES);
    ball.path = myPath;
    CGPathRelease(myPath);
    ball.fillColor = [SKColor blueColor];
    ball.position = CGPointMake(200, 200);
    [self addChild:ball];

    // set the physics
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
    ball.physicsBody.restitution = 0.8;
}

在您的示例中,您使用精灵以便半径使用

// ball.size.width * 0.5;
于 2013-10-25T13:08:04.593 回答