我正在制作一个游戏,玩家画一条线,然后一个精灵应该跟随并在其上运行。我有一个可变数组和一个效果很好的绘制方法。但我无法找到移动精灵的方法。我尝试了不同的方法,但我无法让迭代器工作。
它应该通过遍历数组来工作。其中填充了先前存储的 CGPoint 位置。我尝试在 ccTouchedEnded 中移动精灵,但它突出显示 [toucharray objectAtIndex:0] 并说“将'id'传递给不兼容类型'CGPoint(又名'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));
}