Scale is only a visual property, it does not apply to physics bodies. There is no way to scale a physics body other than creating a new body with an appropriately scaled shape.
SKPhysicsBody size with resizeToHeight
-
11-10-2022 - |
Question
I have a problem with size of physics body after resize of his skspritenode.
My code:
backTop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:backTop.size];
[backTop.physicsBody setAffectedByGravity:NO];
[backTop.physicsBody setDynamic:NO];
[backTop.physicsBody setCategoryBitMask:kPipeCategory];
[backTop.physicsBody setCollisionBitMask:kPlayerCategory];
backTop.physicsBody.usesPreciseCollisionDetection = YES;
//SKAction
SKAction *scaleBackTopActionStart = [SKAction resizeToHeight:backTop.size.height+kPipeGap duration:kPipeScale];
SKAction *scaleBackTopActionEnd = [SKAction resizeToHeight:pipeTop.size.height duration:kPipeScale];
SKAction *scaleBackTopSequence = [SKAction sequence:@[[SKAction waitForDuration:kPipeScaleStartWait],scaleBackTopActionStart, scaleBackTopActionEnd, [SKAction waitForDuration:kPipeScaleWait]]];
Problem:
the skspritenode is resized but his physics body do not follow. Have you got a idea?
Solution
OTHER TIPS
With @LearnCocos2D comment, i have found this change (change height element and move it:
SKSpriteNode *backTop = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(pipeTop.size.width,pipeTop.size.height+kPipeGap)];
[backTop setCenterRect:CGRectMake(26.0/kPipeWidth, 26.0/kPipeWidth, 4.0/kPipeWidth, 4.0/kPipeWidth)];
[backTop setPosition:CGPointMake(self.size.width+(pipeTop.size.width/2), self.size.height-(pipeTop.size.height/2)+(kPipeGap/2))];
SKAction *scaleBackTopActionStart = [SKAction moveToY:backTop.position.y-kPipeGap/2 duration:kPipeScale];
SKAction *scaleBackTopActionEnd = [SKAction moveToY:backTop.position.y duration:kPipeScale];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow