当我调试以下代码时,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);
解决方法
如果你得到0,你不期望它们,两个常见的原因是:
>整数舍入.例如,如果你做int x = 3 * .25,x将为0.我不认为这是你的问题.>你的链中的一个对象是零.请记住,在Cocoa中,[nil anySelector]是有效的 – 它不会像你期望的那样与nil对象一起崩溃. [nil intValue](或floatValue等)== 0.所以如果你有[[myObject someProperty] someOtherProperty],那么沿途的任何东西都是0会导致结果为零.