硬币对象未遵循函数集



我有一个NSMutableArray(命名硬币),它不遵循randomPoint方法。当我去iOS模拟器时,硬币没有显示(因为没有图像),它们停留在屏幕中央而不是随机分配的点。这就是应该制作新硬币的原因:

if ([coins count] == 0){
    CGSize windowSize = [CCDirector sharedDirector].winSize;
    CGPoint randomPointOnScreen = ccp((float)(arc4random() % 100) / 100 * windowSize.width, (float)(arc4random() % 100) / 100 * windowSize.height);
   randomCoinType = arc4random() % kNumberOfCoinTypes + 1; // I have this because my files are named like "coin.1.png" and such

kNumberOfCoinType被定义为10,给我一个数字1-10。这是应该使硬币的相应空隙:

-(void)createCoinsAt:(CGPoint)position withCointype:(int)type{
NSString *imageFile;
switch (type) {

    case 1:
        imageFile = @"coin.1.png";
        break;
    case 2:
        imageFile = @"coin.2.png";
        break;
    case 9:
    case 3:
        imageFile = @"coin.3.png";
        break;
    case 7:
    case 8:
    default:
    case 4:
        imageFile = @"coin.4.png";
        break;
    case 5:
        imageFile = @"coin.5.png";
        break;
    case 6:
        imageFile = @"coin.6.png";
        break;
      }
Coins *c = [Coins spriteWithFile:imageFile];
//Coins *c = [Coins spriteWithFile:[NSString stringWithFormat:@"coin.%d.png", arc4random() % 5 + 1 ]];
c.type = type;
c.position = position;
c.velocity = ccp(0,0);
[coins addObject:c];
[self addChild:c z:2];

}

我有多个案例遵循一个命令,因为我不知道如何使一个数字比另一个数字出现得更频繁。

您显示的代码一切都很好。你的问题可能出在你的Coin课上。

Coin课上要检查的事项:

  • velocity设置为 {0,0} 是否会无意中更改position
  • 您覆盖了哪些CCSprite方法?确保调用这些方法的super

其他要尝试的操作:

  • 你能只使用CCSprite类让一个coin.1.png明显地出现在随机点吗? 如果没有...
    • 确保物理coin.1.png文件已正确添加到项目中。
    • 检查父对象:它们是否标记为不可见?它们是否添加到场景中?

更新:避免子类化CCSprite

@interface Coin : NSObject 
@property (nonatomic, retain) CCSprite *sprite;
@property (nonatomic, assign) CGPoint velocity; 
@property (nonatomic, assign) int type;
@end

--

-(void)createCoinAt:(CGPoint)position withCointype:(int)type{
    NSString *imageFile;
    ...
    //imageFile switch statement here
    ...
    Coin *c = [[Coin alloc] init];
    c.sprite = [CCSprite spriteWithFile:imageFile];
    c.sprite.position = position;
    c.type = type;
    c.velocity = ccp(0,0);
    [coins addObject:c];
    [parentNode addChild:c.sprite z:2]; //make sure to add to correct parent CCNode
}

--一旦给硬币一个速度:

//duration assumes velocity is pixels-per-second
CCAction *moveSprite = [CCRepeatForever actionWithAction:[CCMoveBy actionWithDuration:1.0 position:c.velocity]];
[c.sprite runAction:moveSprite]; 

一旦硬币消失/死亡/其他什么:

[c.sprite stopAllActions];
[c.sprite removeFromParentAndCleanup:YES];
c.sprite = nil;
[coins removeObject:c];

最新更新