I'm making a game where the player draws a line and then a sprite should follow and run on it. I have a mutable array and also a draw method which works well. but I'm having trouble figuring out a way to move the sprite. I have tried different approaches but I can't get the iterator to work.
It's supposed to work by iterating through the array. which is populated with CGPoint locations previously stored. I try to move the sprite in ccTouchedEnded
but it highlights [toucharray objectAtIndex:0]
and says
passing 'id' to parameter of incompatible type 'CGPoint (aka 'struct CGPoint')
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//remove objects each time the player makes a new path
[toucharray removeAllObjects];
}
-(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);
}
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// here is the line I can't get to work to move the sprite
_sprite.position = ccpAdd(ccpMult([toucharray objectAtIndex:0], progress), ccpMult([toucharray objectAtIndex:0+1], 1-progress));
}
Record the positions of the path in an array or a list and iterate through that to move your sprite along the trail. I did this in a game I made to create a particle trail behind the player. I used a size 20 array and iterated through that on an interval, updating the array at the iterator's position with the location of my character and then moving the particle effect to the location stored in the array at the position of the iterator plus 1.
You'll need to seed the array with the starting location so that you don't have null values and you'll need a special case for when you're at the end of the array because you don't want to read from an out of bounds location, instead have your code read from the 0 position.