Frage

I have the following code

static const CGFloat kPipeWidth=56;

(void)topObstacle { 

    Obstacle *pipeTop = [Obstacle spriteNodeWithImageNamed:@"pipetop-1"];

    [pipeTop setCenterRect:CGRectMake(26.0/kPipeWidth, 26.0/kPipeWidth, 4.0/kPipeWidth, 4.0/kPipeWidth)];

    [pipeTop setYScale:pipeTopHeight/kPipeWidth];   
    [pipeTop setPosition:CGPointMake(self.size.width+(pipeTop.size.width/2),
        self.size.height-(pipeTop.size.height/2))];   
    [self addChild:pipeTop];

    pipeTop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pipeTop.size];   
    [pipeTop.physicsBody setAffectedByGravity:NO];   
    [pipeTop.physicsBody setDynamic:NO];

    [pipeTop.physicsBody setCategoryBitMask:kPipeCategory];  
    [pipeTop.physicsBody setCollisionBitMask:kplayerCategory]; 
}

The pipe picture is displayed correctly, however the collision area is not correctly set up. What is wrong with my code!

Thanks for your help

War es hilfreich?

Lösung

There are a few things that could be causing this:

  1. I believe the yScale will scale the image down but not change the value when later used to set the physics body size. (essentially the physics body = original image size)

  2. I do not see a contactBitMask which will allow you to check when your (flappy felipe or mario?) comes in contact with the pipe.

To fix this try:

  • Draw a rectangle using the same variables you currently use and color it black. This will show you where the physics body is being drawn.

  • set them to have a contact event and then log their contact

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top