Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:44:20+00:00 2026-05-27T01:44:20+00:00

i have an app using cocos2d, Im moving some sprites and having animations when

  • 0

i have an app using cocos2d,
Im moving some sprites and having animations when touched, it works fine until i load another view and come back again,
then when I move the sprites some random sprite will get duplicated when moved,

Please note the button that goes to the other view,

sketchButtonTapped

Here some of the code,

 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

Im also using a TSSprite class, to keep track of the sprites,
but dont think that will make for the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:44:20+00:00Added an answer on May 27, 2026 at 1:44 am

    Out of the blue I would say your app has a memory management issue. Since you’re changing views (CCScenes I suppose) and the duplication only happens after that, the original scene and its children were probably not fully deallocated and are now shown again, or some of its sprites somehow make its way into the new scene.

    One of the things that leads me to make this assumption is this line:

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

    I presume the objectosDicto property is set to retain, otherwise you would experience crashes. That makes the autorelease completely superfluous. Which means you haven’t fully embraced how Objective-C memory management works. QED, sort of.

    There’s one simple test that you can do, which is to set a breakpoint in the HelloWorldLayer dealloc method. If the app doesn’t break there when switching views, you’re leaking the entire layer. If it does, you should try the same for the sprites which are duplicated and make sure that they too are all deallocated properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed my app using core data. It works fine in the simulator.
I am writing an app for the iPhone using cocos2d where I have 4
I am writing an app for the iPhone using cocos2d where I have 4
My app works fine in the simulator, and on other devices. I have an
I am using cocos2d on my app. I am doing a transition to another
I am developing a cocos2d IPhone app using the SimpleAudioEngine to play some mp3
I'm having the following problem. I have an app using a UITableView with a
I have a console app using log4net (via Castle Windsor). Everything logs fine to
I have an app using PHP and the PayPal API. The basic way it
I have an app using a ListView as a main screen. Each row displays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.