Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How to make a sprite follow a path Cocos2d

Hi, I have been working on a game where the player should be able to make a path or a line that a sprite should follow.

I already have a somewhat working code I have a mutable array (to record touch locations) and a draw method (to draw the line) It works but I just can't get a line of code working which is supposed to be where the magic happens to move the sprite through the locations recorded in the array please take a look at my code thanks.

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  { 
//remove objects each time the player makes a new path
[toucharray removeAllObjects];
}

here I record the touch locations in the array
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {    
UITouch *touch = [ touches anyObject];
CGPoint new_location = [touch locationInView: [touch view]];
new_location = [[CCDirector sharedDirector] convertToGL:new_location];

CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

// add touches to the touch array
[toucharray addObject:NSStringFromCGPoint(new_location)];
[toucharray addObject:NSStringFromCGPoint(oldTouchLocation)];

}


-(void)draw
{
glEnable(GL_LINE_SMOOTH);

for(int i = 0; i < [toucharray count]; i+=2)
{
CGPoint start = CGPointFromString([toucharray objectAtIndex:i]);
CGPoint end = CGPointFromString([toucharray objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}


this is the part I'm having problems with. this line should be able to move the sprite but I always get a message "Passin 'id' to parameter of incompatible type 'CGPoint' aka 'struct CGPoint'" it highlights [toucharray objectAtIndex:0]
I don't understand if my draw method uses my array and works perfectly why won't this line read from my array too?
moreover I don't really ger the ccpAdd(ccpMult) I don't understand well what it does if someone would help me or point me in the right direction I would appreciate it a lot!

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

// HERE IS THE LINE

_sprite.position = ccpAdd(ccpMult([toucharray objectAtIndex:0], progress), ccpMult([toucharray objectAtIndex:0+1], 1-progress));
Post edited by gab20 on

Replies

Sign In or Register to comment.