【问题标题】:Falling sprite after every 5 seconds每 5 秒后下落的精灵
【发布时间】:2011-11-11 07:01:26
【问题描述】:

我想在每 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];
  }

【问题讨论】:

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


    【解决方案1】:

    你不应该在这里释放 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!
    }
    

    【讨论】:

    • 如果我不释放 CurObject.. 问题是一样的。你知道主要问题是什么吗??
    • 检查您的代码是否存在其他内存管理问题。由于没有正确理解内存管理,您可能会遇到各种问题。在 Xcode 中运行“Product -> Analyze”,让编译器帮你找到这样的案例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多