1
votes

I have a bird class that is called inside MyScene (main game scene). This bird is a randomly moving NPC. It has an image for looking left & that image is flipped horizontally when it's flying right. It has no images for flying up or down. The bird class has a round & very small SKPhysicsBody in comparison to the entire body. This is because later on I want to use SKPhysicsJointPin to attach other small SKPhysicsBodies to it - they will be little worms. I made the bird SKPhysicsBody a rough size of the beak (kept it round) & placed it where the beak is drawn on the image. I did it like so:

minDiam = MIN(self.size.width, self.size.height);
minDiam = MAX(minDiam - 16, 4);
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:minDiam/3.0 center:CGPointMake(-9, -2)];

Inside my MyScene class, during gameplay, I'm trying to change the position of the physicsBody when the bird's left facing image is flipped to the right when it fly's to the right. And also to change it back to CGPointMake(-9, -2) when the bird is flying left again. It's very important that the coordinates of the physicsBody don't just change from CGPointMake(-9, -2) to let's say CGPointMake(9, 2), but actually move/shift with maybe something like [SKAction moveToX:9 duration:0.01]. I have a game development reason for this later on in the future. I am unable to do this. Please help.

EDIT: The bird class is called by a function/method like so inside MyScene:

-(void)addBirdy
{
    _birdy = [Birdy node];
    _birdy.position = CGPointMake(self.size.width/2, self.size.height/2);
    [self addChild:_birdy];
}

EDIT 2: Here is the facing direction code:

if (_birdy.physicsBody.velocity.dx < fX)
{
    BIRDYASFacingDirection facingDir = _birdy.facingDirection;
    facingDir = BIRDYfacingRight;
    _birdy.facingDirection = facingDir;
    //PhysicsBody movement to the right.
    self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:/*HOW TO RETAIN ORIGINAL VALUE?*/ center:/*HOW TO MOVE TO CGPointMake(9, 2)*/];
 }
 else
 {
     BIRDYASFacingDirection facingDir = _birdy.facingDirection;
     facingDir = BIRDYfacingLeft;
     _birdy.facingDirection = facingDir;
     //PhysicsBody movement back to the left.
     self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:/*HOW TO RETAIN ORIGINAL VALUE?*/ center:/*HOW TO MOVE TO CGPointMake(-9, -2)*/];
 }
1
Your question is that you want to move a physics body from point A to point B over a period of time?sangony
You didn't read carefully. I can move a physics body from point A to point B over a period of time. In turn, the sprite moves. I need the sprite to stay in place while its physicsBody moves inside it self from one corner of the sprite image to another (the physicsBody is 1/10 the size of the sprite).Krekin

1 Answers

1
votes

PhysicsBody does not have a position property so it cannot be moved like a node. You can simulate movement by using blocks to nil the current physicsBody and create a new one, in steps, or in one go.

SKAction *wait0 = [SKAction waitForDuration:0.5]; // set to whatever time required

SKAction *block0 = [SKAction runBlock:^{
    myNode.physicsBody = nil;
    myNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 10) center:CGPointMake(0, 0)];
    // complete physicsBody setup
}];

SKAction *block1 = [SKAction runBlock:^{
    myNode.physicsBody = nil;
    myNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 10) center:CGPointMake(2, 0)];
    // complete physicsBody setup
}];
[self runAction:[SKAction sequence:@[wait0, block0, wait0, block1]]];