iOS SKSpriteNode - 离开屏幕

时间:2013-11-24 11:15:13

标签: sprite-kit skspritenode

有没有办法从Parent中删除已离开区域边界的SKSpriteNode?

例如:

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

请指出正确的方向。是通过检查他们的rects枚举更新方法还是他们可以应用的操作。我已经完成了文档似乎无法找到它,但我认为这将是一个简单的工具,因为它节省了内存

6 个答案:

答案 0 :(得分:6)

更新方法是你可以做到的地方:

- (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];
        }
    }];
}

虽然注意删除节点并不能保证释放内存!!

答案 1 :(得分:2)

以下是测试节点是否已离开屏幕边缘的方法。在更新循环中,遍历图层的所有子对象。测试对象是否属于特定类型。然后测试节点的帧是否在屏幕的每个边缘之外。如果是这样,请调用removeFromParent。请注意,由于节点的位置来自其中心,因此您需要考虑该位置。

-(void)update:(CFTimeInterval)currentTime 
{
    [_gameLayer.children enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) 
    {
        if ([obj isKindOfClass:[MyNode class]])
        {
            MyNode *myNode = (MyNode *)obj;

            if (myNode.position.x + myNode.size.width/2 < 0 ||
                myNode.position.x - myNode.size.width/2 > self.size.width ||
                myNode.position.y + myNode.size.height/2 < 0 ||
                myNode.position.y - myNode.size.height/2 > self.size.height)         
            {
                [myNode removeFromParent];
            }
        }
    }];
}

答案 2 :(得分:1)

您还可以检测与背景SKSpriteNode的联系并为其设置类别。 然后,实现didEndContact:方法来删除对象

- (void)didEndContact:(SKPhysicsContact *)contact
{
   //Find out your object (Remember it could be bodyA or bodyB) and remove it here
}

答案 3 :(得分:0)

您可以使用此功能。 每个帧都会调用此函数。(ps:我的英语很差) -(void)didSimulatePhysics

答案 4 :(得分:0)

我更喜欢联系处理。接触测试在每个帧中完成,就像更新处理一样:但通过比较位(SKPhysicsBody.categoryBitMask)进行的接触检测非常快且轻量级。

我需要检测并移除离开屏幕的球。 所以我设置了一个不可见的边框作为场景physicsBody,其大小足以检测完全离开屏幕的球。

如果物理实体。联系节点#1的TestBitMask == physicsBody。类别节点#2的BitMask然后当两者都联系时调用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];
    }
}

答案 5 :(得分:0)

Swift版

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

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

}
相关问题