Reputation: 2675
I'm still super noob in spritekit and trying to get some tutorials here and there..
I'm trying to have stickman figure like this
To jump of a high building and bounce when hit the ground node
It works fine when the node is ball with physicsbody of circle
But when trying physics body of rectangle .. it never bounce.. I've even tried this snipt of code
SKSpriteNode * victim = [SKSpriteNode spriteNodeWithImageNamed:@"victim_base"];
victim.position = CGPointMake(5, 500);
victim.zPosition = 2;
victim.name = @"victim";
victim.xScale=0.7;
victim.yScale=0.7;
[self addChild:victim];
CGFloat offsetX = victim.frame.size.width * victim.anchorPoint.x;
CGFloat offsetY = victim.frame.size.height * victim.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 13 - offsetX, 59 - offsetY);
CGPathAddLineToPoint(path, NULL, 43 - offsetX, 58 - offsetY);
CGPathAddLineToPoint(path, NULL, 38 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 7 - offsetX, 3 - offsetY);
CGPathCloseSubpath(path);
victim.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
victim.physicsBody.friction = 0.0f;
// 4
victim.physicsBody.restitution = 1.0f;
// 5
victim.physicsBody.linearDamping = 0.0f;
// 6
victim.physicsBody.allowsRotation = NO;
[victim.physicsBody applyImpulse:CGVectorMake(10.0f, -10.0f)];
And setup of the gravity world is
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
// 1 Create an physics body that borders the screen
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
// 2 Set physicsBody of scene to borderBody
self.physicsBody = borderBody;
// 3 Set the friction of that physicsBody to 0
self.physicsBody.friction = 0.0f;
but he is jumping and just stay still on the ground with no bouncing
I really can't figure what is the wrong here.any help will be appreciated.
Upvotes: 1
Views: 277
Reputation: 701
check out this sample
#import "milkhuntMyScene.h"
@implementation milkhuntMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
myLabel.text = @"";
myLabel.fontSize = 30;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:myLabel];
self.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.gravity=CGVectorMake(0.0, -9.8);
SKSpriteNode *node=[SKSpriteNode spriteNodeWithImageNamed:@"dummy"];
node.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(node.size.width, node.size.height)];
node.position=CGPointMake(200, 50);
node.physicsBody.affectedByGravity=FALSE;
node.physicsBody.dynamic=TRUE;
node.physicsBody.restitution=1;
node.physicsBody.allowsRotation=FALSE;
node.physicsBody.mass=9999999;
[self addChild:node];
}
return self;
}
//
//
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite.xScale=sprite.yScale=0.5;
sprite.position = location;
sprite.physicsBody.allowsRotation=FALSE;
sprite.physicsBody.restitution=1.0;
sprite.physicsBody.angularDamping=0.0;
sprite.physicsBody.linearDamping=0.0;
// SKAction *action = [SKAction rotateByAngle:M_PI duration:1];
//[sprite runAction:[SKAction repeatActionForever:action]];
sprite.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(sprite.size.width, sprite.size.height)];
[self addChild:sprite];
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
@end
Upvotes: 0