在COCOS2D中移动触摸线

我正在使用COCOS2D为iPhone开发游戏。

在那里,当用户将他的手指从一个点拖到另一个点时,我需要画一条线。 就我的知识而言,我需要在Touches Moved method做到这一点,从我可以得到的点。

但我不知道该怎么做。 有人可以帮我吗?

起亚ora。 无聊迫使我提供一个关于这个话题的答案。

图层部分(即@interface GetMyTouches:CCLayer):

 -(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event { UITouch *touchMyMinge = [inappropriateTouches anyObject]; CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ]; CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]]; // flip belly up. no one likes being entered from behind. currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea]; lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea]; // throw to console my inappropriate touches NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y); NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y); // add my touches to the naughty touch array naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)]; naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)]; } 

节点部分(即@interface DrawMyTouch:CCNode):

 @implementation DrawMyTouch -(id) init { if( (self=[super init])) { } return self; } -(void)draw { glEnable(GL_LINE_SMOOTH); for(int i = 0; i < [naughtyTouchArray count]; i+=2) { start = CGPointFromString([naughtyTouchArray objectAtIndex:i]); end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]); ccDrawLine(start, end); } } @end 

图层部分II(即@interface GetMyTouches:CCLayer):

 -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { DrawMyTouch *line = [DrawMyTouch node]; [self addChild: line]; } 

记住触摸很容易。 知道你在做什么,而不是火箭科学。

最后,如果你不明白我已经发布的东西…采取烘烤。 世界需要更多的巧克力蛋糕生产者。

澄清:

  1. 没有人学习forms切割'粘贴 〜这个代码从来没有工作没有爱抚
  2. 如果你看不到幽默,你就错了

值得注意的是,我喜欢一个好的巧克力蛋糕。 这个世界真的需要更多美妙的面包师。 这不是侮辱,这是鼓励。

“看看广场外面,find充满了让生命值得生活的知识的圈子”〜Aenesidemus。

 - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *theTouch = [touches anyObject]; CGPoint touchLocation = [theTouch locationInView:[theTouch view] ]; cgfloat x = touchLocation.x; cgfloat y= touchLocation.y; printf("move x=%f,y=%f",x,y); } 

试试上面的代码。 在iphone中touches moved时,会得到坐标点。

要画线,使用这样的东西:

 -void draw { here is the code for line draw. } 

在更新方法中更新这个函数。