【问题标题】:cocos2d position coordinatecocos2d 位置坐标
【发布时间】:2011-03-14 18:53:11
【问题描述】:

当我调试以下代码时,xPos = 0,yPos = 0,很奇怪

CCNode* node = [birdsAlone objectAtIndex:nextInactiveBird];
NSAssert([node isKindOfClass:[BirdEntity class]], @"not a bird entity");
BirdEntity* bird = (BirdEntity*) node;
float birdHeight = CGRectGetHeight([bird boundingBox]);
float xPos = ([[CCDirector sharedDirector] winSize].width - 100) * CCRANDOM_0_1() + 50;
float yPos = ([[CCDirector sharedDirector] winSize].height + birdHeight / 2.0f);

CGPoint location = ccp(xPos, yPos);

【问题讨论】:

  • [[CCDirector sharedDirector] winSize] 和 birdHeight 的值是多少?
  • 在调试器中找不到。现在没事了。我使用clang调试和分析,结果birdsAlone是nil,node是垃圾......我使用[CCArray arrayWithCapacity:20]而不是[[CCArray alloc] initWithCapacity],输入太快^_^
  • 你好,你是在“debug”还是“release”模式下调试?您不应该得到birdsAlone == nil ...另外,请指定放置这段代码的函数。birdsAlone 是类成员吗?输入参数?
  • 我在调试模式下调试它。我不认为这是代码的问题。 BirdAlone 是一组鸟类(因为在这个游戏模式中,没有鱼。)

标签: iphone objective-c xcode cocos2d-iphone


【解决方案1】:

当您在意想不到的地方得到 0 时,两个常见原因是:

  • 整数舍入。例如,如果您执行 int x = 3 * .25,则 x 将为 0。我认为这不是您的问题。

  • 链中的一个对象是 nil。请记住,在 Cocoa 中,[nil anySelector] 是有效的——它不会像您期望的那样与 nil 对象一起崩溃。 [nil intValue](或floatValue等)== 0。因此,如果您有[[myObject someProperty] someOtherProperty],那么沿途的任何东西都将导致结果为零。

    李>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多