Yama
Yama

Reputation: 2649

Cocos2d iPhone : Sprite initialized weird issue

I am using the tutorial to make one simple app. And unlike the sample app, I want to show my projectile/object from the very beginning instead of on touch. For the same, I am doing like this :

-(id) init
{
    if((self=[super init]))
    {
        [[SimpleAudioEngine sharedEngine]setEnabled:TRUE];
        winSize = [CCDirector sharedDirector].winSize;
        projectile = [CCSprite spriteWithFile:@"abc.png"];
        projectile.position = spriteOffset;
        self addChild:projectile];
        self.isTouchEnabled = YES;
        //some other code
   }
}

And then on touch of the same object/projectile, I need to move it to the direction i have moved it to. I am doing the below for the same :

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    // Determine offset of location to projectile
    int offX = location.x - projectile.position.x; //Projectile returns 0X0 here
    int offY = location.y - projectile.position.y;

    float scalarX = 1.0f;

    if (offX < 0.0f)
        scalarX = -1.0f;

    int realX = scalarX * (winSize.width + (projectile.contentSize.width/2));//winSize.width + (food.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; // 480pixels/1sec
    float realMoveDuration = length/velocity;

    [projectile runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
                       [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                       nil]];
    [_projectiles addObject:projectile];  //ERROR SIGABRT

}

In the above code, at this line int offX = location.x - projectile.position.x; my sprite returns into 0X0. I am not getting where i am making mistake. At the first, it shows the objects on the screen but on touch event, it gets disappeared. I also have synthesized the CCSprite but in vail. Is there any other way around or any mistake that I am performing? Please help if any idea. Thank you.

ERROR : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'

Upvotes: 0

Views: 165

Answers (2)

YvesLeBorg
YvesLeBorg

Reputation: 9079

Actually, since the projectile sprite is added as child in init, the only way it will be autoreleased is if elsewhere in the code the projectile is removed as child. You have to find where that is happening. Just forcing it to be 'sticky' with a retain may corrupt the logic about the sprite removal reason(s).

Upvotes: 0

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

projectile might be becoming a victim of autorelease pool and hence returning 0x0. as projectile is being initialized as non retained. this is not a COCOS2D related issue. this is basically a Objective-C memory management issue.

do one thing, make projectile as

@property(nonatomic,retain)

then in

-(id) init

make this change:

        self.projectile = [CCSprite spriteWithFile:@"abc.png"];
        self.projectile.position = spriteOffset;
        [self addChild:self.projectile];

hope this will resolve the issue you are facing

Upvotes: 1

Related Questions