2014-12-30 57 views
0

我試圖在Xcode中編寫一個簡單的應用程序 - fallDown遊戲 - 我需要讓球與平臺相撞。讓碰撞在Apple SpriteKit工作

首先 - 我嘗試使用動畫師,並添加一個對撞機和重力等,但後來我不能讓框架是圓形的(這將是非常有用的,如果有人可以告訴我的話)。

第二 - 因爲上述不起作用(與UIbezierpath有關)我決定使用SpriteKit。用spriteKit製作對象/節點。球和平臺是我需要使用ball.physicsBody.collisionBitMask碰撞skshapenodes但每次我跑球穿過身體

static const uint32_t ballCategory = 0x11 << 1; 
static const uint32_t platformCategory = 0x11 << 3; 

+ (SKNode *) spawnBall 
{ 
    SKShapeNode *node = [[SKShapeNode alloc] init]; 
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, BALL_RADIUS, BALL_RADIUS)]; 

    node.path = [path CGPath]; 
    node.fillColor = [SKColor colorWithRed:0.7 green:0.0 blue:0.0 alpha:1.0]; 
    node.strokeColor = [SKColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]; 
    //node.glowWidth = BALL_GLOW_RADIUS; 

    node.physicsBody.categoryBitMask = ballCategory; 
    node.physicsBody.collisionBitMask = platformCategory; 
    return node; 
} 
+ (NSMutableArray *) risingPlats 
{ 
    // Screen Size 
    CGRect screenBound = [[UIScreen mainScreen] bounds]; 
    CGFloat width = screenBound.size.width; 
    CGFloat height = screenBound.size.height; 

    NSMutableArray *arrayOfShapes = [[NSMutableArray alloc] init]; 
    int numberPieces = arc4random() % 2; 
    if (numberPieces) { 
     SKShapeNode * node = [[SKShapeNode alloc] init]; 

     UIBezierPath *path = [UIBezierPath bezierPathWithRect: 
           CGRectMake((BALL_RADIUS*3-width)/2, -height, width-(BALL_RADIUS*3), BALL_RADIUS)]; 

     node.path = [path CGPath]; 
     node.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0]; 
     node.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]; 

     node.physicsBody.categoryBitMask = platformCategory; 
     node.physicsBody.collisionBitMask = ballCategory; 

     [arrayOfShapes addObject:node]; 
    } else { 
     int locationOfHole = (arc4random() % (int)(width-(BALL_RADIUS*6)))+BALL_RADIUS*1.5; 

     // node1 
     SKShapeNode * node = [[SKShapeNode alloc] init]; 
     UIBezierPath *path = [UIBezierPath bezierPathWithRect: 
           CGRectMake((-width/2), -height, locationOfHole, BALL_RADIUS)]; 

     node.path = [path CGPath]; 
     node.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0]; 
     node.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]; 

     node.physicsBody.categoryBitMask = platformCategory; 
     node.physicsBody.collisionBitMask = ballCategory; 

     [arrayOfShapes addObject:node]; 

     // node2 
     SKShapeNode *node2 = [[SKShapeNode alloc] init]; 
     UIBezierPath *path2 = [UIBezierPath bezierPathWithRect: 
           CGRectMake(locationOfHole+BALL_RADIUS*3-(width/2), -height, width-(locationOfHole+BALL_RADIUS*3), BALL_RADIUS)]; 

     node2.path = [path2 CGPath]; 
     node2.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0]; 
     node2.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]; 

     node2.physicsBody.categoryBitMask = platformCategory; 
     node2.physicsBody.collisionBitMask = ballCategory; 

     [arrayOfShapes addObject:node2]; 
    } 
    return arrayOfShapes; 
} 

我改變了上面contactbitmask,當我實現的方法,兩個機構聯繫它從未打印到控制檯。

回答

3

你的節點沒有分配給它們的物理體。

你指定他們的物理體類別,但他們沒有物理開始。這是零!

你需要做的

yournode.physicsBody = [SKPhysicsBody <some initializer>] 
+0

是否有所作爲,如果我以後再添加物理機構? – Jetstream5500

+0

我在場景中添加它,但這只是產生節點(spawner類) – Jetstream5500

+0

好的,這可能會解決我的問題,因爲我需要在平臺上的一個physicsBody,但我原來從來沒有添加它,因爲他們現在對重力作出反應。我如何關閉一個對象的重力 – Jetstream5500