2

領域境界を残した SKSpriteNode を Parent から削除する方法はありますか?

例えば:

-(void)didBeginContact:(SKPhysicsContact *)contact
{
    firstNode = (SKSpriteNode *)contact.bodyA.node;
    if (firstNode.position.y<0) {
        [firstNode removeFromParent];
    }
}

正しい方向に向けてください。四角形をチェックして列挙する更新メソッドですか、それとも適用できるアクションですか。私はドキュメントを調べましたが、それを見つけることができないようですが、メモリを節約するので簡単に実装できると思いました

4

6 に答える 6

6

Update メソッドは、問題なく実行できる場所です。

- (void)update:(NSTimeInterval)currentTime {
    //remove any nodes named "yourNode" that make it off screen
    [self enumerateChildNodesWithName:@"yourNode" usingBlock:^(SKNode *node, BOOL *stop) {

        if (node.position.x < 0){
            [node removeFromParent];
        }
    }];
}

ただし、ノードを削除してもメモリが解放されるとは限らないことに注意してください!!

于 2013-11-24T21:57:20.120 に答える
0

迅速なバージョン

self.enumerateChildNodesWithName("enemy") {
 node, stop in 

 if (node.position.x < 0) {
   node.removeFromParent()
 }

}
于 2016-02-04T05:11:39.373 に答える
0

私は連絡先の処理を好みます。update: の処理と同様に、各フレームで接触テストが行​​われます。しかし、ビット (SKPhysicsBody.categoryBitMask) を比較することによる接触検出は非常に高速で軽量です。

画面を離れるボールを検出して削除する必要があります。そのため、ボールが画面から完全に離れたのを検出するのに十分な大きさのシーンのphysicsBodyとして、目に見えない境界線を設定しました。

物理体なら。node#1 == physicalsBody の TestBitMask に接続しますノード #2 のカテゴリBitMask then didBeginContact: は、両者が接触したときに呼び出されます。

-(void)didMoveToView:(SKView *)view {

    // bitmasks:
    static uint32_t const categoryBitMaskBall = 0x1<<1;
    static uint32_t const categoryBitMaskBorder = 0x1<<6;

    CGFloat ballDiameter = [BallSprite radius] *2;

    // floorShape is my scenes background
    CGRect largeFloorFrame = floorShape.frame;
    largeFloorFrame.origin.x -= ballDiameter;
    largeFloorFrame.origin.y -= ballDiameter;
    largeFloorFrame.size.width += ballDiameter *2;
    largeFloorFrame.size.height += ballDiameter *2;

    CGPathRef pathMainView = CGPathCreateWithRect(largeFloorFrame, nil);
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:pathMainView];
    self.physicsBody.categoryBitMask = categoryBitMaskBorder;
}

- (BallSprite*)addBall {

    // initialize ball with additional configuration...
    BallSprite *ball = [BallSprite ballAtPoint:(CGPoint)p];
    ball.categoryBitMask = categoryBitMaskBall;
    ball.contactTestBitMask = categoryBitMaskBorder;
    [self addChild:ball];
}


- (void)didBeginContact:(SKPhysicsContact *)contact {

    SKPhysicsBody *firstBody, *secondBody;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    /*!
        Check on outside border contact
     */
    if ((firstBody.categoryBitMask & categoryBitMaskBall) != 0 &&
        (secondBody.categoryBitMask & categoryBitMaskBorder) != 0) {

        [firstBody.node removeFromParent];
}
    if ((firstBody.categoryBitMask & categoryBitMaskBorder) != 0 &&
        (secondBody.categoryBitMask & categoryBitMaskBall) != 0) {

        [secondBody.node removeFromParent];
    }
}
于 2015-02-18T14:08:32.253 に答える