如何从另一个类改变SkSpriteNode的颜色?

时间:2016-03-29 02:32:53

标签: ios objective-c sprite-kit

我有一个名为GoodGuy.h /.m的SkSpriteNode子类,我有另一个名为ShopScene的类。在+(id)guy{ GoodGuy *goodGuy =[GoodGuy spriteNodeWithColor:[self choosenColor] size:CGSizeMake(40, 40)]; SKSpriteNode *leftEye = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(5,5)]; [goodGuy addChild:leftEye]; leftEye.position = CGPointMake(-3, 8); SKSpriteNode *rightEye = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(5,5)]; rightEye.position = CGPointMake(13, 8); [goodGuy addChild:rightEye]; goodGuy.name= @"guy"; goodGuy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:goodGuy.size]; goodGuy.physicsBody.categoryBitMask = guyCatergory; goodGuy.physicsBody.categoryBitMask = obstacleCatergory | groundCatergory; return goodGuy; } +(id)choosenColor{ UIColor *color; long blueBought; long redBought; [[NSUserDefaults standardUserDefaults] setInteger:blueBought forKey:@"Blue has been bought"]; [[NSUserDefaults standardUserDefaults] setInteger:redBought forKey:@"Red has been bought"]; if (blueBought > 1) { blueBought=1; redBought = 0; } if (blueBought ==1) { redBought =0; color = [UIColor blueColor]; [[NSUserDefaults standardUserDefaults] setInteger:blueBought forKey:@"Blue has been bought"]; printf("boughtBlue = %li boughtRed = %li \n",blueBought, redBought); } if (redBought == 1) { blueBought=0; printf("boughtBlue = %li boughtRed = %li \n",blueBought, redBought); color = [UIColor redColor]; [[NSUserDefaults standardUserDefaults] setInteger:blueBought forKey:@"Blue has been bought"]; [[NSUserDefaults standardUserDefaults] setInteger:redBought forKey:@"Red has been bought"]; } return color; } 我宣布:

ShopScene.m

我正在尝试将“GoodGuy”的颜色从商店改为多头,但它不起作用。我在-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch=[touches anyObject]; CGPoint location =[touch locationInNode:self]; SKNode *node =[self nodeAtPoint:location]; if ([node.name isEqualToString:@"blue"]) { boughtBlue = 1; boughtRed = 0; printf("boughtBlue = %li boughtRed = %li \n",boughtBlue, boughtRed); [[NSUserDefaults standardUserDefaults] setInteger:boughtBlue forKey:@"Blue has been bought"]; SKTransition *transition =[SKTransition pushWithDirection:SKTransitionDirectionDown duration:1.0]; GameScene *myScene =[[GameScene alloc] initWithSize:CGSizeMake(self.size.width, self.size.height)]; [self.scene.view presentScene:myScene transition:transition]; } if ([node.name isEqualToString:@"red"]) { boughtBlue=0; boughtRed = 1; [[NSUserDefaults standardUserDefaults] setInteger:boughtRed forKey:@"Red has been bought"]; [[NSUserDefaults standardUserDefaults] setInteger:boughtBlue forKey:@"Blue has been bought"]; printf("boughtBlue = %li boughtRed = %li \n",boughtBlue, boughtRed); SKTransition *transition =[SKTransition pushWithDirection:SKTransitionDirectionDown duration:1.0]; GameScene *myScene =[[GameScene alloc] initWithSize:CGSizeMake(self.size.width, self.size.height)]; [self.scene.view presentScene:myScene transition:transition]; } }

中有这个
{{1}}

我可以将颜色更改为蓝色,但不能更改为红色。关于如何改变颜色的任何想法?

1 个答案:

答案 0 :(得分:0)

您是否尝试从if(节点名称为红色)内部删除此行touchesBegan方法:

    [[NSUserDefaults standardUserDefaults] setInteger:boughtBlue forKey:@"Blue has been bought"];

另外,为什么你有这个蓝色而不是红色?

if (blueBought > 1) {
    blueBought=1;
    redBought = 0;
}

更新:

我认为你在choosenColor中将NSUserDefaults值设置为0。在您的原始代码中,尝试更改此类代码(我已添加了有关更改位置的注释):

+(id)choosenColor{

    UIColor *color;
    // Here you were setting your NSUserDefaults to 0 (you had just created the blueBought and redBought)
    // Change to to the following
    long blueBought = [[NSUserDefaults standardUserDefaults] integerForKey:@"Blue has been bought"];
    long redBought = [[NSUserDefaults standardUserDefaults] integerForKey:@"Red has been bought"];
    // The rest is the same as your
    // original code
    if (blueBought > 1) {
        blueBought=1;
        redBought = 0;
    }

    if (blueBought ==1) {
        redBought =0;
        color = [UIColor blueColor];
        [[NSUserDefaults standardUserDefaults] setInteger:blueBought forKey:@"Blue has been bought"];
        printf("boughtBlue = %li boughtRed = %li \n",blueBought, redBought);

    }
    if (redBought == 1) {
        blueBought=0;
        printf("boughtBlue = %li boughtRed = %li \n",blueBought, redBought);

        color = [UIColor redColor];
        [[NSUserDefaults standardUserDefaults] setInteger:blueBought forKey:@"Blue has been bought"];
        [[NSUserDefaults standardUserDefaults] setInteger:redBought forKey:@"Red has been bought"];

    }
    return color;
}