使用未声明的标识符winSize

时间:2012-11-06 19:20:23

标签: objective-c xcode

我不确定为什么我收到此错误但是我查看了.h文件,我似乎无法想出xCode抛出此错误的原因:

// on "init" you need to initialize your instance
-(id) init
{
if ((self=[super init])) {
    _batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Sprites.pvr.ccz"]; //1
    [self addChild:_batchNode]; //2
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Sprites.plist"]; //3

    _ship = [CCSprite spriteWithSpriteFrameName:@"SpaceFlier_sm_1.png"];  //4
    CGSize winSize = [CCDirector sharedDirector].winSize; //5
    _ship.position = ccp(winSize.width * 0.1, winSize.height * 0.5); //6
    [_batchNode addChild:_ship z:1]; //7
}
return self;
// 1) Create the CCParallaxNode
_backgroundNode = [CCParallaxNode node];
[self addChild:_backgroundNode z:-1];

// 2) Create the sprites we'll add to the CCParallaxNode
_spacedust1 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];
_spacedust2 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];
_planetsunrise = [CCSprite spriteWithFile:@"bg_planetsunrise.png"];
_galaxy = [CCSprite spriteWithFile:@"bg_galaxy.png"];
_spacialanomaly = [CCSprite spriteWithFile:@"bg_spacialanomaly.png"];
_spacialanomaly2 = [CCSprite spriteWithFile:@"bg_spacialanomaly2.png"];

// 3) Determine relative movement speeds for space dust and background
CGPoint dustSpeed = ccp(0.1, 0.1);
CGPoint bgSpeed = ccp(0.05, 0.05);

// 4) Add children to CCParallaxNode
[_backgroundNode addChild:_spacedust1 z:0 parallaxRatio:dustSpeed positionOffset:ccp(0,winSize.height/2)];
[_backgroundNode addChild:_spacedust2 z:0 parallaxRatio:dustSpeed positionOffset:ccp(_spacedust1.contentSize.width,winSize.height/2)];
[_backgroundNode addChild:_galaxy z:-1 parallaxRatio:bgSpeed positionOffset:ccp(0,winSize.height * 0.7)];
[_backgroundNode addChild:_planetsunrise z:-1 parallaxRatio:bgSpeed positionOffset:ccp(600,winSize.height * 0)];
[_backgroundNode addChild:_spacialanomaly z:-1 parallaxRatio:bgSpeed positionOffset:ccp(900,winSize.height * 0.3)];
[_backgroundNode addChild:_spacialanomaly2 z:-1 parallaxRatio:bgSpeed positionOffset:ccp(1500,winSize.height * 0.9)];

}

1 个答案:

答案 0 :(得分:1)

首先,除非C2D以某种方式神奇地改变了return的工作方式,否则你会在return之后继续做很多事情,这是永远无法达到的。你只是return ed;你退出了这个方法。这就像告诉画家离开他正在工作的房间并回家一天......然后尖叫到虚无他应该把墙壁漆成红色。实际上,你根本不会尖叫。这是一个例子:

- (void) foo {
    int x = 10;
    return; //return control to caller
    self->someValue = x; //never done; we've returned
}

x将被分配十,然后你会回来。之后的任何行都没有意义。其次,这里有这条线。

 CGSize winSize = [CCDirector sharedDirector].winSize;

它出现在if声明中。变量winSize只能在您声明它的if语句的约束条件下使用。它超出范围"其他任何事情。例如,以下内容将给出相同的警告。

- (void) bar {
    if (0){
        int x = 1;
    }
    int y = x;
}

为什么不在return循环中将所有内容放在if循环中,就在它关闭之前?