Spritekit - Objective C - 将SKShapenodes添加到数组中,然后通过按钮逐个删除节点

时间:2016-08-11 11:13:52

标签: ios objective-c sprite-kit

基本上我通过使用SKShapenode在屏幕上绘制多条线并将其添加到字典中。

我现在想将SKShapenodes存储在一个数组中,并通过一个按钮删除数组中的最后一项。任何想法的人?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
SKLabelNode *touchedNode = (SKLabelNode *)[self nodeAtPoint:positionInScene];

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];
    // Create a mutable path
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:location];
    // Create a shape node using the path
    lineNode = [SKShapeNode shapeNodeWithPath:path.CGPath];
    lineNode.strokeColor = [SKColor blackColor];
    lineNode.glowWidth = 3.0;
    [_gameLineNode addChild:lineNode];
    // Use touch pointer as the dictionary key. Since the dictionary key must conform to
    // NSCopying, box the touch pointer in an NSValue
    NSValue *key = [NSValue valueWithPointer:(void *)touch];
    [lines setObject:lineNode forKey:key];

    _lineNodeArray = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        [_lineNodeArray addObject:lineNode];
    }

    }
}
 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];
    // Retrieve the shape node that corresponds to touch
    NSValue *key = [NSValue valueWithPointer:(void *)touch];
    lineNode = [lines objectForKey:key];
    if (lineNode != NULL) {
        // Create and initial a mutable path with the lineNode's path
        UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:lineNode.path];
        // Add a line to the current touch point
        [path addLineToPoint:location];
        // Update lineNode
        lineNode.path = path.CGPath;
        lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path.CGPath];
        lineNode.physicsBody.categoryBitMask = lineNodeCategory;
        lineNode.physicsBody.contactTestBitMask = bubble1Category | bubble2Category| bubble3Category | bubble4Category | bubble5Category | bubble6Category;
        lineNode.physicsBody.collisionBitMask = ballCategory | ballHalf1Category | ballHalf2Category;
        lineNode.physicsBody.dynamic = YES;
        lineNode.name = lineNodeCategoryName;

        CGPoint _currentPoint = [[touches anyObject] locationInNode:self];
        CGPoint _previousPoint = [[touches anyObject] previousLocationInNode:self];

        _delta = CGPointMake(_currentPoint.x - _previousPoint.x, _currentPoint.y - _previousPoint.y);
        }
    }
}

删除操作 - 这将从数组中删除该行,但如何从游戏视图中删除最后一行?

-(void)deleteLine{
     _lineNodeArray = [NSMutableArray array];
    [_lineNodeArray lastObject ];
    [_lineNodeArray removeLastObject];
}   

0 个答案:

没有答案