问题描述
我最近遇到了一个问题,我会增加节点的大小,但物理体的大小会保持不变.我试图寻找解决方案,但没有成功.怎样才能让body按照节点的大小进行缩放?
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)];
}
如果我没记错的话,这不是以前的工作,但我只是尝试过,看起来它可以工作.尽管如此,这还没有经过全面测试,我不确定这是否会破坏物理模拟.不过,接触检测可能会起作用.我只是想表明,当 sprite 缩放时,实际的物理物体会被缩放.结果如下:
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 可以看出,物理体与精灵一起缩放(没有重新创建另一个不同大小的物理体).sprite 周围的蓝色边界框是 sprite 物理体的可视化表示(在视图控制器中使用 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 中缩放物理体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!