如何从另一个类访问更改的变量?(例如:水平计数)

时间:2021-12-29 00:02:46

I have two scenes. Scene A is the game scene, where the level variable of type int changes. In class B I want to get the variable.

我有两个场景。场景A是游戏场景,其中类型int类型的变量变化。在B类中,我想要得到变量。

What I've got now is the following :

我现在得到的是:

--------------------------------- LevelDone.m ---------------------------------

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - LevelDone。m - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

HelloWorldLayer *object = [[HelloWorldLayer alloc]init];

        int pointsForZeroStars = [[requiredPoints objectAtIndex:0] integerValue];
        int pointsForOneStar = [[requiredPoints objectAtIndex:1] integerValue];
        int pointsForTwoStars = [[requiredPoints objectAtIndex:2] integerValue];
        int pointsForThreeStars = [[requiredPoints objectAtIndex:3] integerValue];
        NSLog(@"HEALTH = %d",object.health);
        CCSprite *levelDoneWindow;         
        if ( object.health < pointsForZeroStars){
            NSLog(@"should be 0 stars");
            levelDoneWindow = [CCSprite spriteWithFile:@"leveldonescreen0stars.png"];
        }

NSLog returns the initial value of health which is 100, when actually it is something smaller than 100.

NSLog返回的初始值是100,实际上它小于100。

In the game class I call pushscene in order to call the LevelDone scene:

在游戏课上,我给pushscene打了个电话:

----------------------------- HelloWorldLayer.m (GAME SCENE) -------------------------------------

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - HelloWorldLayer。(游戏场景)- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

else{

    [[CCDirector sharedDirector] pushScene:[LevelDone node]];
    [self generateLevelFromPlist:level];


}

The -generateFromPlist method resets the health to 100. But it is certainly called after the NSLog method in my LevelDone.m class, which nonetheless prints out that health is 100.

-generateFromPlist方法将健康重置为100。但它肯定是在NSLog方法之后调用的。m类,尽管如此,它打印出的健康是100。

How do I retrieve the value of this variable properly ?

如何正确地检索此变量的值?

2 个解决方案

#1


0  

Use a singleton to manage global values.

使用singleton来管理全局值。

http://getsetgames.com/2009/08/30/the-objective-c-singleton/

http://getsetgames.com/2009/08/30/the-objective-c-singleton/

Basically, you can create properties in that singleton class, and it will "persist" throughout the game no matter what scene you are in etc.

基本上,您可以在该单例类中创建属性,无论您在哪个场景中,它都会在整个游戏中“坚持”。

#2


0  

Are you sure the health property of the HelloWorldLayer is actually being updated correctly? If so, I'd suspect (and, again, just guessing, as the error does not appear to be in the provided code itself) that the variable you're changing within the HelloWorldLayer is not the same as the property you're accessing with object.health -- perhaps a scope issue is at play here (ie, the selector is affecting a local health variable, but not the instance variable itself).

您确定HelloWorldLayer的健康属性正在正确地更新吗?如果是这样,我怀疑(而且,仅仅是猜测,因为错误并没有出现在提供的代码本身中),您在HelloWorldLayer中更改的变量与您使用对象访问的属性不一样。健康——可能在这里有一个范围问题(例如,选择器正在影响一个本地健康变量,而不是实例变量本身)。

#1


0  

Use a singleton to manage global values.

使用singleton来管理全局值。

http://getsetgames.com/2009/08/30/the-objective-c-singleton/

http://getsetgames.com/2009/08/30/the-objective-c-singleton/

Basically, you can create properties in that singleton class, and it will "persist" throughout the game no matter what scene you are in etc.

基本上,您可以在该单例类中创建属性,无论您在哪个场景中,它都会在整个游戏中“坚持”。

#2


0  

Are you sure the health property of the HelloWorldLayer is actually being updated correctly? If so, I'd suspect (and, again, just guessing, as the error does not appear to be in the provided code itself) that the variable you're changing within the HelloWorldLayer is not the same as the property you're accessing with object.health -- perhaps a scope issue is at play here (ie, the selector is affecting a local health variable, but not the instance variable itself).

您确定HelloWorldLayer的健康属性正在正确地更新吗?如果是这样,我怀疑(而且,仅仅是猜测,因为错误并没有出现在提供的代码本身中),您在HelloWorldLayer中更改的变量与您使用对象访问的属性不一样。健康——可能在这里有一个范围问题(例如,选择器正在影响一个本地健康变量,而不是实例变量本身)。