2011-11-24 68 views
0

我使用cocos2d中有一個應用程序, 林移動一些精靈和觸摸時有動畫,直到我加載另外的看法,再回來, 然後當我移動精靈一些隨機精靈會得到正常工作複製移動時,cocos2d的複製精靈錯誤

請注意,進入到其他視圖按鈕,

sketchButtonTapped

這裏的一些代碼,

enum { 
easySprite = 0x0000000a, 
mediumSprite = 0x0000000b, 
hardSprite = 0x0000000c, 
backButton = 0x0000000d, 
magneticSprite = 0x0000000e, 
magneticSprite2 = 0x0000000f 
}; 

@implementation HelloWorldLayer 
@synthesize objetosDicto = _objetosDicto; 
@synthesize bgMainScene = _bgMainScene; 

+(CCScene *) scene 
{ 
CCScene *scene = [CCScene node]; 

HelloWorldLayer *layer = [HelloWorldLayer node]; 

[scene addChild: layer]; 

return scene; 
} 
-(id) init 
{ 

if((self=[super init])) { 

    self.objetosDicto = [[[NSMutableDictionary alloc] init] autorelease]; 


    self.bgMainScene = [CCSprite spriteWithFile:@"bgnd.png"];//Background 

    self.bgMainScene.anchorPoint = CGPointMake(0, 0); 
    [self addChild:self.bgMainScene]; 

    isTouchEnabled_ = YES; 

    CCMenuItem *starMenuItem = [CCMenuItemImage 
           itemFromNormalImage:@"boton1.png" selectedImage:@"boton1Selected.png" 
           target:self selector:@selector(starButtonTapped:)]; 

    starMenuItem.position = ccp(49, 299); 
    CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil]; 
    starMenu.position = CGPointZero; 
    [self addChild:starMenu]; 

    //secondo boton 
    CCMenuItem *sketchV = [CCMenuItemImage itemFromNormalImage:@"sketchBoton.png" selectedImage:@"sketchBoton.png" target:self selector:@selector(sketchButtonTapped:)]; 

    sketchV.position = ccp(457, 160); 
    CCMenu *sketchMenu = [CCMenu menuWithItems:sketchV, nil]; 
    sketchMenu.position = CGPointZero; 
    [self addChild:sketchMenu]; 




    //first sprite 
    TSprite *ez = [TSprite spriteWithFile:@"butonA.png"]; //lets create a TSprite, named EZ, and the file is Easy.png. 
    [ez SetCanTrack:YES];//The sprite can be tracked. 

    [self addChild: ez z:2 tag:easySprite]; //lets add a tag to the sprite, in order to identify it later RED 
    ez.position = ccp(290,300);//position of the sprite 
    [TSprite track:ez];//and lets add this sprite to the tracked array. 


    //second sprite 
    TSprite *med = [TSprite spriteWithFile:@"butonB.png"]; //blue 
    [med SetCanTrack:YES]; 
    [self addChild: med z:1 tag:mediumSprite]; 
    med.position=ccp(299,230); 
    [TSprite track:med]; 

    [self.objetosDicto setObject:@"299"forKey:@"hardX"]; //poner a salvar en finish toches?? 
    [self.objetosDicto setObject:@"160" forKey:@"hardY"]; //poner a salvar en finish toches?? 79 

    //third sprite 
    TSprite *har = [TSprite spriteWithFile:@"butonC.png"]; //GREEN 
    [har SetCanTrack:YES]; 
    [self addChild: har z:3 tag:hardSprite]; 
    [har setPosition:ccp(299,160)]; 
    [TSprite track:har]; 

    //magnetic sprite 
    TSprite *mag = [TSprite spriteWithFile:@"magnetic.png"]; 
    [mag SetCanTrack:YES]; //usar no! para ver que pasa! 
    [self addChild:mag z:1 tag:magneticSprite]; 
    [mag setPosition:ccp(299, 180)]; 
    [TSprite track:mag]; 

    TSprite *mag2 = [TSprite spriteWithFile:@"magnetic.png"]; 
    [mag2 SetCanTrack:YES]; //usar no! para ver que pasa! 
    [self addChild:mag2 z:1 tag:magneticSprite2]; 
    [mag2 setPosition:ccp(299, 140)]; 
    [TSprite track:mag2]; 


    [self getChildByTag:magneticSprite].visible = NO; //ojo, para aparece desaparece! 
    [self getChildByTag:magneticSprite2].visible = NO; //ojo, para aparece desaparece! 


} 
return self; 
} 


- (void)sketchButtonTapped:(id)sender { 
NSLog(@"hundio"); 
CCTransitionRotoZoom *transition = [CCTransitionSlideInR transitionWithDuration:1.0 scene: [SketchViewController scene]]; 


// Tell the director to run the transition 

[[CCDirector sharedDirector] replaceScene:transition]; 

[tablaMenuBloq setHidden:YES]; 
}//sketchButtonTapped 

- (void)registerWithTouchDispatcher { 
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
} 


-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 

CGPoint location = [self convertTouchToNodeSpace:touch]; 

NSLog(@"locacion %f %f",location.x, location.y); 


if([TSprite SomethingWasTouched:location]){  
    NSArray * mySprites = [TSprite allMySprites]; 
    NSUInteger i, count = [mySprites count]; 

    for (i = 0; i < count; i++) { 
     TSprite * obj = (TSprite *)[mySprites objectAtIndex:i]; 

     if (CGRectContainsPoint([obj rect], location) && [obj GetCanTrack]) { 
      [obj setPosition :ccp(location.x,location.y)];//position of the spritesssss 
      [self getChildByTag:magneticSprite] 

      if([obj tag] == easySprite){ 



      } 


     } 
    } 
} 


    return TRUE; 
} 
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { 


CGPoint location = [self convertTouchToNodeSpace:touch]; 

if([TSprite SomethingWasTouched:location]){   
    NSArray * mySprites = [TSprite allMySprites]; 
    NSUInteger i, count = [mySprites count]; 

    for (i = 0; i < count; i++) { 
     TSprite * obj = (TSprite *)[mySprites objectAtIndex:i]; 

     if (CGRectContainsPoint([obj rect], location) && [obj GetCanTrack]) { 
      // code here is only executed if obj has been touched 

      [obj setPosition :ccp(location.x,location.y)];//position of the spritesssss 


      [self reorderChild:obj z:3]; //send layer to z front!! 


      int s = [[self.objetosDicto objectForKey:@"hardY"]intValue]; 

      if ((location.y) < s+50) //79 

      { 
       [self getChildByTag:magneticSprite].visible = YES; //ojo, para aparece desaparece! 
       [self getChildByTag:magneticSprite2].visible = NO; //ojo, para aparece desaparece! 


       if ((location.y) < s-20) 
       { 
        [self getChildByTag:magneticSprite].visible = NO; //ojo, para aparece desaparece! 

        [self getChildByTag:magneticSprite2].visible = YES; //ojo, para aparece desaparece! 

       } 



      } 

      if ((location.y) > s+50) //79 
      { 
       [self getChildByTag:magneticSprite].visible = NO; //ojo, para aparece desaparece! 

      } 
      if ((location.y) < s-65) //79 
      { 
       [self getChildByTag:magneticSprite2].visible = NO; //ojo, para aparece desaparece! 

      } 


     } 


       } 
} 



} 

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { 



CGPoint location = [self convertTouchToNodeSpace:touch]; 


if([TSprite SomethingWasTouched:location]){  
    NSArray * mySprites = [TSprite allMySprites]; 
    NSUInteger i, count = [mySprites count]; 

    for (i = 0; i < count; i++) { 
     TSprite * obj = (TSprite *)[mySprites objectAtIndex:i]; 

     if (CGRectContainsPoint([obj rect], location) && [obj GetCanTrack]) { 
      // code here is only executed if obj has been touched 


      [ obj runAction:[CCMoveTo actionWithDuration:0.3 position:ccp(299, 190)]]; 


      if([obj tag] == easySprite){ 



      } 


     } 
    } 
} 


} 

    - (void) dealloc 
{ 

[_bgMainScene release]; 
[_objetosDicto release]; 

[super dealloc]; 
} 
@end 

林還使用TSSprite類,跟蹤的精靈, 但不認爲這會爲這個問題?

回答

2

我會說你的應用程序存在內存管理問題。由於您正在改變視圖(我想CCScenes),並且重複只發生在那之後,原始場景及其子項可能未完全釋放,現在再次顯示,或者某些精靈以某種方式進入新場景。

之一,導致我作出這個假設的東西是這一行:

self.objetosDicto = [[[NSMutableDictionary alloc] init] autorelease]; 

我推測objectosDicto屬性設置爲保留,否則你會遇到系統崩潰。這使autorelease完全是多餘的。這意味着你還沒有完全理解Objective-C內存管理的工作原理。 QED,有點。

有一個簡單的測試,你可以做,它是在HelloWorldLayer dealloc方法中設置一個斷點。如果在切換視圖時應用程序不在那裏,您就會漏掉整個圖層。如果是這樣,你應該對重複的精靈進行同樣的操作,並確保它們都被正確地釋放。

+0

嗨,謝謝,這就是我有很多東西需要學習,釋放後的精靈工作正常;請問一個問題,爲什麼保留屬性會與autorelease混雜在一起?,再次感謝 – MaKo