Sprite disappears suddenly Cocos2d

亡梦爱人 提交于 2019-12-13 03:21:38

问题


i have this game like doodlejump

i inserted some monsters and bullets on the player

the player fires a bullet and deletes the target

i inserted a nslog to both so i know if the bullet fires and the target shows

all is working smooth in the simulator ios 4.0

but when everytime i install it on the device

the bullet and the target seems to be not appearing(yes only those two the game is still running smooth)

but everytime i check the console i can see the nslog of the bullet and target

i tried it on 3 different phones ios 4.0, 5.0 and 4.2

now im confused if what seems to be the problem

i checked for same issues at google but i cant seem to find one

i also checked for spellings and mini problems but still it is occurring.

What seems to be causing this issue?


PS:the app im working on is outdated,it is old version of COCOS2d. does this affect it? i tried on upgrading but i have so many errors that i dont know thats why i go back to the default.


The Monsters are appearing for about .5 sec then will disappear suddenly is this because of the spriteMoveFinished?

here are the codes(target):

-(void)addTarget {

    Sprite *target =[Sprite spriteWithFile:@"him.png"];


    CGSize winSize = [[Director sharedDirector]winSize];
    int minX = winSize.width/6;
    int maxX = winSize.width - target.contentSize.width/2;
    int rangeX = maxX - minX;
    int actualX = (arc4random() % rangeX) + minX;

    target.position = ccp(actualX,500);
    NSLog(@"Location:%f",target.position);
    [self addChild:target];

    int minDuration = 2.0; int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration)+minDuration;

    id actionMove = [MoveTo actionWithDuration:actualDuration position:ccp(actualX, -target.contentSize.height/2)];
    id actionMoveDone = [CallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
    [target runAction:[Sequence actions:actionMove,actionMoveDone, nil]];   
    target.tag = 1;
    [_targets addObject:target];
}

(bullets):

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    AtlasSpriteManager *spriteManager = (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];
    AtlasSprite *bird = (AtlasSprite*)[spriteManager getChildByTag:kBird];

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[Director sharedDirector]convertCoordinate:location];


    Sprite *projectile = [Sprite spriteWithFile:@"psn.png"];

    projectile.position = ccp(bird.position.x,bird.position.y);

    CGSize winSize = [[Director sharedDirector]winSize];

    int offX = location.x - projectile.position.x;
    int offY = location.y - projectile.position.y;

    [self addChild:projectile];

    float scalarX = 1.0f;
    if(offX < 0.0f) scalarX = -1.0f;
    int realX = scalarX * (winSize.width + (projectile.contentSize.width/2));
    float ratio = (float) offY / (float) offX;
    int realY = (realX *ratio) +projectile.position.y;
    CGPoint realDest = ccp(realX,realY);

    int offRealX = realX - projectile.position.x;
    int offRealY = realY - projectile.position.y;
    float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
    float velocity = 480/1;
    float realMoveDuration = length/velocity;

    [projectile runAction:[Sequence actions:[MoveTo actionWithDuration:realMoveDuration position:realDest],
                           [CallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)], nil]];
    NSLog(@"Shoot!");
    projectile.tag = 2;
    [_projectiles addObject:projectile];


}

Thanks i hope this will be resolved...


回答1:


Odd. Things to try quickly would be:

  • Delete the app from the device itself (hold on the app icon until the 'x' appears and delete it) then do a clean build. This will force all assets to be replaced on the phone.

  • Do you have retina support enabled? If so, is there a file in your resources called 'him-hd.png' that is corrupt? It would try to load that one first if a retina device is detected.

  • Does it fail on the simulator for a retina device? You can change the device being simulated in the iOSSimulator menu bar.

  • When you added the images to the project, did you select 'Copy items into destination group's folder (if needed)? This is important as otherwise the images won't be included with the bundle

  • Finally, did you check the Target Membership for the images so it is set to the app target? You can check this by selecting the image in xCode and looking at the File Inspector->Target Membership. There should be a check next to your application.




回答2:


One advice that hasn't been mentioned before: make sure the filename's case matches!

For example, if the filename is "Him.png" and you load the image as "him.png" in cocos2d, this will work on the Simulator but fail on the device because iOS devices have a case-sensitive filesystem.

This is not only limited to iOS devices by the way. Mac OS X users also have the option to install a case-sensitive filesystem which may pose the same problem.

For that reason it's good practice to avoid uppercase letters in filenames altogether.




回答3:


I've had a similar issue, check the Z index of the objects your creating.

What could be happening is you are rendering them but you are rendering them behind your background because your background has a higher (negative number closer to 0) then the bullets and targets.

Check what your _targets & _projectiles are doing they might be altering the Z values

Also for Good Measure do a Clean & Clean Build

Clean

Products > Clean

Clean Build

Products > Hold option (on your keyboard) & Click on Clean (should change to Clean Build)




回答4:


ISSUE SOLVED:

i dont think this is the right way... but i tried removing the actionMoveDone line

it seems to be causing this problem when i removed it the sprites are working fine..

i still want to learn why this is happening...

Thanks to all those who replied...




回答5:


I know this issue was solved, but this might help others... my problem was that the code that was adding the sprites to the scene was also setting the box2d body so when the world stepped the sprites were moved to a location off screen. Fix was to set the position of the bodies when I placed the sprite in the scene.



来源:https://stackoverflow.com/questions/8829693/sprite-disappears-suddenly-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!