2

有什么方法可以从 Parent 中删除具有左侧区域边界的 SKSpriteNode?

例如:

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

只是指出我正确的方向。是通过检查其矩形来枚举更新方法,还是您可以应用它们的操作。我浏览了文档似乎找不到它,但我认为这将是一个简单的实现,因为它可以节省内存

4

6 回答 6

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

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

于 2013-11-24T21:57:20.120 回答
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];
            }
        }
    }];
}
于 2014-03-15T17:25:56.387 回答
1

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

- (void)didEndContact:(SKPhysicsContact *)contact
{
   //Find out your object (Remember it could be bodyA or bodyB) and remove it here
}
于 2014-07-29T13:21:23.337 回答
0

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

我需要检测并移除离开屏幕的球。因此,我设置了一个不可见的边框,因为场景物理体刚好足够大,可以检测到完全离开屏幕的球。

如果物理体。联系节点#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];
    }
}
于 2015-02-18T14:08:32.253 回答
0

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

于 2014-05-14T01:34:25.587 回答
0

斯威夫特版本

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

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

}
于 2016-02-04T05:11:39.373 回答