Hi,
I've got a few months in with cocos2d, but have just started with animations and I am stuck. I've tried boards and tutorials, but cant find my answer, so I thought I would reach out to the group. If someone has the time to help, it would be appreciated.
I'm using cocos2d v0.7.2 (i realize it is old). The problem is that in my animation, I cannot seem to get the current position of the sprite. I always get the original position of the sprite when it started the animation run.
my animation code:
CGPoint balloonposition1 = ccp(190, 45);
mgr1 = [AtlasSpriteManager spriteManagerWithFile:item01 capacity:22];
[self addChild:mgr1 z:1 tag:11];
id float1 = [MoveBy actionWithDuration:3 position: ccp(5, 325)];
id float1reset = [MoveBy actionWithDuration:0 position: ccp(-5, -325)];
id float1_seq = [Sequence actions: float1, float1reset, nil];
float1_forever = [[RepeatForever actionWithAction:float1_seq] retain];
balloon1 = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 105, 165) spriteManager: mgr1];
balloon1.position = ccp(balloonposition1.x, (balloonposition1.y));
[mgr1 addChild:balloon1 z:1];
[mgr1 runAction: float1_forever];
So this works fine and puts the balloon image in the correct place and animates it nicely. However, when I try to grab the moving sprite, it only sees the original "ballonposition1" as location of the balloon. I can grab the balloon and move it, but only if I touch its starting position. Somehow it has an offset, dependent upon where the balloon is in its animation sequence. I would do simple math on the offset, but I cannot find the position of the balloon's current position.
touch code:
- (BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
point = [[Director sharedDirector] convertCoordinate: point];
float x = [balloon1 position].x - ([balloon1 contentSize].width)/2;
float y = [balloon1 position].y - ([balloon1 contentSize].height)/2;
float w = [balloon1 contentSize].width;
float h = [balloon1 contentSize].height;
CGRect touchArea1 = CGRectMake(x, y, w, h);
if (CGRectContainsPoint(touchArea1, point)) { whoisheld=1;
if(whoisheld==1) {
[mgr1 stopAction:float1_forever];
[self reorderChild:mgr1 z:5]; //make balloon1 go to top
[balloon1 setPosition:(point)]; //this should jump object center to touch point
}
return kEventHandled;
}
I have similar code on move, etc, which is working fine. The balloon follows the touch point, *but is off by the original offset from the initial touch*.
I must be missing something simple, but I cannot get past this issue.
Thank you for any guidance you can give me.