English 中文(简体)
cocos2d duplicatingspirite bug
原标题:cocos2d duplicating sprites bug

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,

请指出另一个观点的语气,

草图

这里有一些法典,

 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(草图:)];

    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)草图:(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];
}//草图

 - (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?

最佳回答

在蓝色中,我要说,你有记忆管理问题。 由于你们重新改变了看法(CCCScenes I suppose)和重叠只是事后才发生,最初的景象及其孩子可能没有完全被分配,现在又被再次显示,或者一些美术团会让它进入新的舞台。

导致我作出这一假设的因素之一是:

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

我假定要保留标的物,否则就会发生坠毁。 这使得汽车完全多余。 这就意味着,你没有完全接受目标记忆管理是如何运作的。 QED,类型。

你可以做的一个简单检验,就是在Hello World Layer交易方法中设定一个突破点。 如果在交换看法时, app子不停,你将整个层隔开。 如果做的话,你就应当尝试重复的rite,并确保它们也能够适当地分配。

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签