質問

I must be missing something really obvious here. My objective: Create a box in the center of the screen, with a bouncing ball staying inside that box. I'm trying to create an edge-based SKSpriteNode for the bounding box and an volume-based SKShapeNode for the internal ball. THe ball spawns in the center of the box, falls through the box, falls off screen, apparently hits something below the bottom of the screen, and bounces back above the bottom edge a few times before finally disappearing. All the while, didBeginContact never fires.

Question1: why is my ball falling through the visible box, yet contacting something below the edge of the screen? Is my physicsbody misaligned from the actual sprite?

Question2: why is didBeginContact not firing? [Edit, ok this was simple. Forgot to set physicsworld.delegate]

Thanks!

Scene.h

#define MAX_WORLD_CELLS_X 10
#define MAX_WORLD_CELLS_Y 10
#define BGTILE_SIZE 100

typedef enum : uint32_t {
    TYPE_SUBSPACE       = 1,
    TYPE_WORLD          = 2,
    TYPE_CHILD          = 4
} ColliderTypes;

@interface helloScene : SKScene
                <SKPhysicsContactDelegate>
{
    SKSpriteNode    *layerSubSpace,*layerWorld;  
}
@end

Scene.m, didBeginContact

-(void) didBeginContact:(SKPhysicsContact *)contact
{
    NSLog(@"Function: GameScene.m:didBeginContact");
}

Scene.m, initWithSize

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
        CGSize layerSize = CGSizeMake(MAX_WORLD_CELLS_X * BGTILE_SIZE *2, MAX_WORLD_CELLS_Y * BGTILE_SIZE *2);
        layerSubSpace = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:layerSize];

        //center subSpace on the screen's center 
        layerSubSpace.position = CGPointMake(CGRectGetMidX(self.frame),         
                                         CGRectGetMidY(self.frame));        

        CGRect subspaceRect = CGRectMake(-layerSubSpace.size.width/2, -layerSubSpace.size.height/2, layerSubSpace.size.width, layerSubSpace.size.height);
        layerSubSpace.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:subspaceRect];
        layerSubSpace.physicsBody.affectedByGravity = NO;
        layerSubSpace.physicsBody.categoryBitMask = TYPE_SUBSPACE;
        layerSubSpace.physicsBody.contactTestBitMask = TYPE_WORLD;
        layerSubSpace.physicsBody.collisionBitMask = TYPE_WORLD;
        [self addChild:layerSubSpace];

        SKShapeNode *ball = [[SKShapeNode alloc] init];
        CGMutablePathRef myPath = CGPathCreateMutable();
        CGPathAddArc(myPath, NULL, 0,0, 60, 0, M_PI*2, YES);
        ball.path = myPath;
        ball.lineWidth = 1.0;
        ball.fillColor = [SKColor blueColor];

        ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:60];
        ball.physicsBody.restitution = 1;
        ball.physicsBody.categoryBitMask = TYPE_WORLD;
        ball.physicsBody.collisionBitMask = TYPE_SUBSPACE;
        ball.physicsBody.contactTestBitMask = TYPE_SUBSPACE;
        [layerSubSpace addChild:ball];
    }

    layerSubSpace.xScale = 0.25;
    layerSubSpace.yScale = 0.25;

    return self;
}
役に立ちましたか?

解決

1) The reason your physics simulation isn't lining up with your visual representation is that you've set the x/yScale properties of layerSubSpace to 0.25. These properties affect your sprites' visual representations but not the underlying physics model. The basic issue is that the size of your ball and box are reduced by a factor of four in each dimension, but the distance that the ball drops isn't reduced at all.

I suspect the solution is simply to reduce the size of everything explicitly by a factor of four, rather than relying on the x/yScale properties. When I reduce BGTILE_SIZE to 25 and the radius of the circle, both in CGPathAddArc and bodyWithCircleOfRadius:, to 15, the ball comes to rest at the bottom of the box on the iPad simulator.

2) As you said, you didn't set self.physicsWorld.contactDelegate = self;

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top