ios如何删除 SKSpriteNode 从父,当背景相同的颜色,Sprite?

标签: Xcode sprite-kit ios
发布时间: 2017/3/28 20:32:17
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

Im 制作一个游戏在哪里的一个正方形的颜色将会改变每一秒和背景也会改变颜色每一秒,用户不得不点击广场,当它是作为背景相同的颜色和分数会增加。但是我弄不懂如何做到这一点。 这是我目前为止的代码︰

#import "MyScene.h"

@implementation MyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
            [self performSelector:@selector(backgrounds) withObject:nil ];
            [self performSelector:@selector(createSquare) withObject:nil afterDelay:0];
            [self performSelector:@selector(createPSquare) withObject:nil afterDelay:0];

        }
    return self;

}

-(void) backgrounds {
    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"blueOutline"];
    background.name = @"blueOutline";
    background.size          = CGSizeMake(320, 480);
    background.position      = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    [self addChild:background];

    //meathod sequence at interval

}

-(void) createSquare {
    SKSpriteNode *blueSprite = [SKSpriteNode spriteNodeWithImageNamed:@"blue"];
    blueSprite.name = @"blueSprite";
    blueSprite.size          = CGSizeMake(50, 50);
    blueSprite.position      = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    [self addChild:blueSprite];

    //meathod sequence at interval

}

-(void) createPSquare {
    SKSpriteNode *pinkSprite = [SKSpriteNode spriteNodeWithImageNamed:@"pink"];
    pinkSprite.name = @"pinkSprite";
    pinkSprite.size          = CGSizeMake(50, 50);
    pinkSprite.position      = CGPointMake(CGRectGetMidX(self.frame)+10,CGRectGetMidY(self.frame));
    [self addChild:pinkSprite];

    //meathod sequence at interval

}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];


    if ([node.name isEqualToString:@"blueSprite"]) {
        [node runAction:[SKAction removeFromParent]]; //Removes Sprite from parent
    }


    if ([node.name isEqualToString:@"pinkSprite"]) {
        [node runAction:[SKAction removeFromParent]]; //Removes Sprite from parent
    }

}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

解决方法 1:

我建议你推广创建正方形和背景,以简化将更多的颜色添加到你的游戏的代码。这里是如何做到这一点的示例︰

定义一个标识的Sprite节点来创建的类型的类型

typedef NS_ENUM (NSInteger, SpriteType) {
    SpriteTypeBackground,
    SpriteTypeSquare
};

此方法向现场每个与随机选定的颜色添加一个正方形和一个背景

- (void) addSquareAndBackground {
    _background = [self createSpriteWithType:SpriteTypeBackground];
    _square = [self createSpriteWithType:SpriteTypeSquare];
}

这从场景中移除的广场和背景

- (void) removeSquareAndBackground {
    [_background removeFromParent];
    [_square removeFromParent];
}

这将创建一个正方形或基于指定类型的背景Sprite

-(SKSpriteNode *) createSpriteWithType:(SpriteType)type {
    // Select a color randomly
    NSString *colorName = [self randomColorName];
    SKSpriteNode *sprite;
    if (type == SpriteTypeBackground) {
        NSString *name = [NSString stringWithFormat:@"%@Outline",colorName];
        sprite = [SKSpriteNode spriteNodeWithImageNamed:name];
        sprite.name = name;
        sprite.size = CGSizeMake(320, 480);
    }
    else {
        sprite = [SKSpriteNode spriteNodeWithImageNamed:colorName];
        sprite.name = [NSString stringWithFormat:@"%@Sprite",colorName];
        sprite.size = CGSizeMake(50, 50);
    }
    sprite.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    [self addChild:sprite];

    return sprite;
}

随机选择一种颜色名称

// Set the total number of colors here
#define kNumberOfColors     2

- (NSString *) randomColorName {
    NSString *colorName;
    switch (arc4random_uniform(kNumberOfColors)) {
        case 0:
            colorName = @"blue";
            break;
        case 1:
            colorName = @"pink";
            break;
        // Add more colors here
        default:
            break;
    }
    return colorName;
}

添加到您的 touchesBegan 方法来测试颜色匹配

    if (node == _square) {
        // Extract the color name from the node name
        NSArray *squareNameParts = [node.name componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
        // Extract the color name from the node name
        NSArray *backgroundNameParts = [_background.name componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
        // Compare if the colors match
        if ([backgroundNameParts[0] isEqualToString: squareNameParts[0]]) {
            NSLog(@"Score");
        }
    }

剩下的就是创建调用 addSquareAndBackground,等待一秒钟,然后调用 removeSquareAndBackground SKAction。肥皂、 漂洗、 重复 !

编辑︰添加这上面你 @implementation MyScene 声明︰

@interface MyScene()

@property SKSpriteNode *background;
@property SKSpriteNode *square;

@end
赞助商