I want to make a soccer ball by IOS SpriteKit

淺唱寂寞╮ 提交于 2019-12-13 04:49:58

问题


I want to make a soccer ball by IOS SpriteKit

I was able to create a ball in the code such as the following. However, it does not bounce at all. To make rebound rate, How can I do this?

(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];

}

I have declared like this.   

-(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;

}


回答1:


This looks like a ball to me

- (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;
}

In your example, you use a sprite so for radius use

// ball.size.width * 0.5;


来源:https://stackoverflow.com/questions/19543200/i-want-to-make-a-soccer-ball-by-ios-spritekit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!