0

In my android game I'm trying to spawn a projectile that flies across the screen and has an animation using 4 .png files. However whenever a projectile spawns it crashes the entire app. Here's my code:

protected void addProjectile() {
    CCSprite projectile = CCSprite.sprite("projectile1.png");
    CCAnimation projectileAnimation = CCAnimation.animation("", 0.1f);
    for (int i = 1; i <= 4; i++) {
  projectileAnimation.addFrame(CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("projectile" + i + ".png"));
    }
    CCAction projectileAction = CCAnimate.action(1, projectileAnimation, true);
        projectile.setPosition(CGPoint.ccp(winSize.width + (projectile.getContentSize().width / 2.0f), winSize.height / 2));
        addChild(projectile);
        projectile.setTag(1);
        _projectiles.add(projectile);

        int minDuration = 3;
        int maxDuration = 6;
        int rangeDuration = maxDuration - minDuration;
        int actualDuration = rand.nextInt(rangeDuration) + minDuration;

        CCMoveTo actionMove = CCMoveTo.action(actualDuration, CGPoint.ccp(-projectile.getContentSize().width / 2.0f + 320, winSize.height / 2));
        CCCallFuncN actionMoveDone = CCCallFuncN.action(this, "spriteMoveFinished");
        CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);
        projectile.runAction(actions);
        projectile.runAction(projectileAction);
}

Any help would be appreciated.

Akarsh M
  • 1,629
  • 2
  • 24
  • 47
Frozsht
  • 305
  • 1
  • 3
  • 8
  • any logs, error messages? – Kreiri May 08 '13 at 08:56
  • 05-08 20:15:11.981: D/CCSpriteFrameCache(28021): Frame not found: projectile1.png 05-08 20:15:11.981: D/CCSpriteFrameCache(28021): Frame not found: projectile2.png 05-08 20:15:11.981: D/CCSpriteFrameCache(28021): Frame not found: projectile3.png 05-08 20:15:11.981: D/CCSpriteFrameCache(28021): Frame not found: projectile4.png And a bunch of other errors: 05-08 20:15:11.981: W/System.err(28021): Caused by: java.lang.NullPointerException 05-08 20:15:11.981: W/System.err(28021): at com.frozgames.cube_ninja.GameLayer.update(GameLayer.java:490) 05-08 20:15:11.981: W/System.err(28021): ... 8 more – Frozsht May 09 '13 at 03:20
  • You have to load frames from spritesheets into shared frame cache before you can use them. – Kreiri May 09 '13 at 11:07
  • I added: for (int i=1; i<=4; i++) { CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFrames("projectile" + i + ".png"); } right after: CCSprite projectile = CCSprite.sprite("projectile1.png"); and now the projectile won't even spawn. Nothing is happening on the screen, but it's not crashing anymore. – Frozsht May 10 '13 at 03:53
  • I don't see `addSpriteFrames` method in [CCSpriteFrameCache.h](https://github.com/cocos2d/cocos2d-x/blob/master/cocos2dx/sprite_nodes/CCSpriteFrameCache.h) You have to use one of CCSpriteFrameCache methods to load sprites _from spritesheet_. Spritesheet is an image with many sprites within, and comes with a file that specifies where each sprite is. There are many spritesheet tutorials out in the web, don't be afraid to use your preferred web search engine to find them. – Kreiri May 10 '13 at 07:26
  • Thanks for the help. I tried doing it without a spritesheet cause I didn't know how to make one, but I found texture packer and made one with it and now it works fine. – Frozsht May 11 '13 at 23:30
  • can you update the images name which is in Assets , cz Logcat shows that which name you are trying it does not available in the folder – Akarsh M Jul 16 '13 at 06:04

0 Answers0