1
@implementation demoScene{

-(void) initializeScene {
moon *_moon=[[moon alloc]init];
}
-(void) updateBeforeTransform: (CC3NodeUpdatingVisitor*) visitor {

    deltaTime =deltaTime+visitor.deltaTime;
    NSLog(@"delta time=%0.12f",deltaTime);
    [_moon print:deltaTime/100000];
}
@end

这是我的问题。

我想在方法中从月球类创建一个对象,initializeScene并且我想在方法中向该对象发送消息updateBeforeTransform

当我输入这样的代码时,我无法向_moon对象发送消息并获得“未使用的变量”警告消息。

我知道该对象超出范围,但如果我需要从updateBeforeTransform方法发送消息。方法在updateBeforeTransform一秒钟内被调用 60 次。所以我不想在一秒钟内创建一个对象 60 次。

任何建议将不胜感激。

4

1 回答 1

2

您需要一个实例变量,而不是在initializeScene方法中创建一个新变量:

@implementation demoScene {
    moon *_moon; // You may already have this in the .h file - just have it in 1 place.
}

- (void)initializeScene {
    _moon = [[moon alloc] init]; // assign to ivar
}

- (void)updateBeforeTransform:(CC3NodeUpdatingVisitor*) visitor {
    deltaTime = deltaTime + visitor.deltaTime;
    NSLog(@"delta time=%0.12f", deltaTime);
    [_moon print:deltaTime / 100000];
}

@end

旁注 - 类名应以大写字母开头。变量和方法名称以小写字母开头。

于 2012-12-21T00:32:17.617 回答