在Xcode Spritekit中缩放物理实体
我最近遇到了一个问题,我会增加一个节点的大小,但物理机构将保持相同的大小。 我试图寻找解决scheme,但没有成功。 我怎样才能使节点的大小与人体比例?
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上重现物理人体缩放的代码:
- (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)]; }
如果我记得正确的话,这不是早些时候工作,但我只是试了一下,它看起来像工作。 但是,这还没有完全testing,我不知道这是否会搞乱物理模拟。 接触检测可能会工作。 我只是想表明,当精灵缩放时,实际的物理体将被缩放。 结果如下:
从上面的gif可以看出,物理体与精灵一起缩放(不需要重新创build另一个不同大小的物理体)。 精灵周围的蓝色边界框是精灵物理体的视觉表示(在视图控制器中使用skView.showsPhysics = YES
启用);
这是奇怪的,你可以添加物理主体之前,将其添加到SKNode或SKScene为例如
SKTexture *texture=[_gameTexture textureNamed:@"mysprite"]; SKSpriteNode *bubble=[SKSpriteNode spriteNodeWithTexture:texture]; bubble.anchorPoint=CGPointMake(0.5,0.5); //_bubble.name=[@"bubble" stringByAppendingFormat:@"%d ",index]; bubble.name=newcolor;
// _bubble.userInteractionEnabled = YES; _bubble.position = CGPointMake(newPosition.x,newPosition.y);
// bubble.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:60]; bubble.physicsBody.dynamic=NO; bubble.physicsBody.linearDamping=0; bubble.physicsBody.restitution=0; bubble.physicsBody.density=1; bubble.physicsBody.allowsRotation=NO; bubble.physicsBody.affectedByGravity=NO; //_bubble.physicsBody.velocity=CGVectorMake([self getRandomNumberBetween:-10 to:7], [self getRandomNumberBetween:-10 to:7]); // _bubble.physicsBody.usesPreciseCollisionDetection=YES; bubble.physicsBody.categoryBitMask=ballCategory; bubble.physicsBody.collisionBitMask=ballCategory | pathCategory ; bubble.physicsBody.contactTestBitMask=ballCategory | pathCategory ; [self addChild:_bubble]; bubble.xScale=_bubble.yScale=2; bubble.alpha=0.5;