我对SpriteKit有点新意,我在物理方面遇到了一些麻烦,我尝试了很多东西让SKSpriteNode用力或冲动来移动但是它永远不会移动,只有重力会影响它设置它。
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.anchorPoint = CGPointMake(0.5, 0.5);
myWorld = [SKNode node];
[self addChild:myWorld];
SKSpriteNode* map = [SKSpriteNode spriteNodeWithImageNamed:@"grass"];
[myWorld addChild:map];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:map.frame];
self.physicsBody.friction = 0.0f;
self.physicsBody.categoryBitMask = 2;
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
self.physicsWorld.contactDelegate = self;
pointOfView = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithWhite:1 alpha:0.2] size:CGSizeMake(300, 548)];
[myWorld addChild:pointOfView];
pointOfView.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pointOfView.frame.size];
pointOfView.physicsBody.friction = 0.0f;
pointOfView.physicsBody.restitution = 1.0f;
pointOfView.physicsBody.linearDamping = 0.0f;
pointOfView.physicsBody.allowsRotation = NO;
pointOfView.physicsBody.collisionBitMask = 2;
pointOfView.physicsBody.categoryBitMask = 3;
[pointOfView.physicsBody applyImpulse:CGVectorMake(10.0f, -10.0f)];
}
return self;
}
以下是我主要场景中的大部分代码,除了像update或touchesBegan这样的空方法之外别无其他。我错过了什么?我已经尝试了很多东西,我开始失去它哈哈
非常感谢您的帮助!
答案 0 :(得分:1)
现在有点晚了,但答案就在这里:Not able to apply impulse to SKSpriteNode physics body
问题是你用bodyWithEdgeLoopFromRect创建了一个物理体;这会创建一个静态物理主体,它不会响应applyImpulse。您需要使用bodyWithRectangleOfSize:而不是添加物理主体。
答案 1 :(得分:0)
pointOfView.physicsBody.dynamic = YES;
动态属性决定精灵是否应该受到物理力的影响。
答案 2 :(得分:-1)
你写的错误的代码你的pointOfView不受重力影响你只是在它上面施加一个小的impusle。
将此行self.physicsWorld.gravity = CGVectorMake(0.0,0.0)更改为self.physicsWorld.gravity = CGVectorMake(0.0,-9.8);如果你寻找朝向上方的负重力和CGVectorMake(0.0,9.8),如果你寻找向下的正重力。并尝试在间隔后应用脉冲[pointOfView.physicsBody applyImpulse:CGVectorMake(10.0f,-10.0f)];