我仍然是spritekit中的超级菜鸟,并试图在这里和那里得到一些教程..
我试图像这样的火柴人形象 跳到高层建筑物并在撞击地面节点时反弹
当节点是圆形物理体球时,它工作正常
但是当尝试矩形的物理体时......它永远不会反弹..我甚至尝试过这段代码
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)];
重力世界的设置是
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;
但是他正在跳跃,只是保持不动,没有弹跳
我真的无法弄清楚这里有什么不对。非常感谢。
答案 0 :(得分:0)
查看此示例
#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