在Xcode Spritekit中扩展物理实体 [英] Scaling Physics Bodies in Xcode Spritekit
问题描述
我最近遇到了一个问题,我会增加节点的大小,但是物理主体的大小会保持不变。我试图找到解决方案但没有成功。如何根据节点的大小调整体尺?
I recently came across an issue where I would increase the size of a node, but the physics body would remain the same size. I tried to look up solutions to this with no success. How can I make the body scale with the size of the node?
CGPoint location = CGPointMake(randX, -self.frame.size.height - expander.size.height);
SKAction *moveAction = [SKAction moveTo:location duration:randDuration];
SKAction *expandAction = [SKAction resizeToWidth:(expander.size.width * 1.4) height:(expander.size.width * 1.4) duration:1.0];
SKAction *collapseAction = [SKAction resizeToWidth:(expander.size.width) height: (expander.size.height) duration:1.0];
SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^() {
expander.hidden = YES;
}];
SKAction *expandCollapseAction = [SKAction repeatActionForever:[SKAction sequence:@[expandAction, collapseAction]]];
SKAction *moveExpandAction = [SKAction group:@[moveAction, expandCollapseAction]];
SKAction *moveExpanderActionWithDone = [SKAction sequence: @[moveExpandAction, doneAction ]];
[expander runAction:moveExpanderActionWithDone withKey: @"expanderMoving"];
推荐答案
我认为这不起作用,但看起来就像那个物理身体毕竟是缩放的。这是可以在iOS8上重现物理体缩放的代码:
I thought this won't work, but it looks like that physics body is scaled after all. Here is the code which can reproduce physics body scaling on iOS8 :
- (void)didMoveToView:(SKView *)view {
/* Setup your scene here */
SKSpriteNode *s = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(100,100)];
s.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame)+100);
s.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:s.size];
s.physicsBody.dynamic = YES;
s.physicsBody.affectedByGravity = NO;
[self addChild:s];
[s runAction:[SKAction scaleTo:0.3f duration:5]];
[s.physicsBody applyImpulse:CGVectorMake(0,30)];
}
如果我没记错的话,这在以前没有用,但我只是尝试了它,它看起来像是有效的。尽管如此,这还没有经过全面测试,我不确定这是否会破坏物理模拟。接触检测可能会起作用。我只是想表明,当精灵缩放时,实际的物理实体会被缩放。结果如下:
If I recall correctly , this wasn't worked earlier, but I just tried it and it looks like it work. Still, this is not fully tested and I am not sure if this will mess up the physics simulation. Contact detection will probably work though. I just wanted to show that actual physics bodies are scaled when sprite is scaled . Here is the result:
来自上面的gif可以看出物理体与精灵一起缩放(不重新创建另一个不同大小的物理体)。精灵周围的蓝色边界框是精灵物理体的直观表示(在视图控制器中启用 skView.showsPhysics = YES
);
From the gif above it can be seen that physics body is scaled along with sprite (without re-creating another different sized physics body). The blue bounding box around the sprite is visual representation of sprite's physics body (enabled in view controller with skView.showsPhysics = YES
);
这篇关于在Xcode Spritekit中扩展物理实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!