【发布时间】:2012-05-26 09:24:53
【问题描述】:
我正在制作一个游戏,其中玩家画一条线,然后一个精灵应该跟随并在其上运行。我有一个可变数组和一个效果很好的绘制方法。但我无法找到移动精灵的方法。我尝试了不同的方法,但无法让迭代器工作。
它应该通过遍历数组来工作。其中填充了先前存储的 CGPoint 位置。我尝试在 ccTouchedEnded 中移动精灵,但它突出显示 [toucharray objectAtIndex:0] 并说“将 'id' 传递给不兼容类型 '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));
}
【问题讨论】:
-
也许我的 Line-Drawing Starterkit 你会感兴趣?对象遵循使用 cocos2d 动作绘制的路径。 learn-cocos2d.com/store/line-drawing-game-starterkit
标签: iphone ios cocos2d-iphone touches trail