每5秒后掉落精灵

时间:2011-11-11 07:01:27

标签: objective-c cocos2d-iphone xcode4.2

我想在每5秒后丢弃一些物体。我的问题是,首先出现在屏幕上的动物会掉下来,但之后的动物不会跌倒,它们会停留在原来的位置。

我放弃动物的代码是:

-(void)dropAnimal
{  
    [self performSelector:@selector(dropAnimal) withObject:nil afterDelay:5];
    prevojectIndex=objectIndex;
    prevIndex=currentIndex;

    float padding = sw*128/768;
    float x = (float)(arc4random()%(int)(sw-padding*2))+padding;

    if([SpritesARRAY count]>0)
    {
        objectIndex=arc4random()%[SpritesARRAY count];
        object=[SpritesARRAY objectAtIndex:objectIndex];
        object.falling = YES;
        currentIndex=arc4random()%[animalsArray count];
        [object initWithSpriteFrameName:[animalsArray objectAtIndex:currentIndex]];
        object.position = ccp(x, sh*31/32-self.position.y);

        objectsDictionary=[NSMutableDictionary dictionary];
        [objectsDictionary setObject:object forKey:[[NSNumber numberWithInt:objectIndex] stringValue]];
        [objectsDictionary retain];
        [SpritesARRAY removeObjectAtIndex:objectIndex];
        [self  animateAnimal];

    }    
}

-(void) animateAnimal 
{
  FallAnimal *CurObject=[objectsDictionary objectForKey:[[NSNumber numberWithInt:objectIndex] stringValue]];
   [CurObject runAction:[CCMoveTo actionWithDuration:2 position:CGPointMake(CurObject.position.x,90)]];
      [CurObject release];
  }

1 个答案:

答案 0 :(得分:0)

你不应该在这里发布CurObject:

-(void) animateAnimal 
{
  FallAnimal *CurObject=[objectsDictionary objectForKey:[[NSNumber numberWithInt:objectIndex] stringValue]];
  [CurObject runAction:[CCMoveTo actionWithDuration:2 position:CGPointMake(CurObject.position.x,90)]];
  [CurObject release];  // <-- makes no sense, you did not retain it!
}