触摸的绘制线在COCOS2D中移动

Sha*_*kti 3 iphone cocos2d-iphone ios touchmove

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

在那里,当用户将手指从一个点拖到另一个点时,我需要绘制一条线.据我所知,我需要Touches Moved method从中获得积分.

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

Sri*_*Sri 6

- (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);   
}
Run Code Online (Sandbox Code Playgroud)

试试上面的代码.touches moved在iphone中它会得到坐标点.

要画线,请使用以下内容:

-void draw
{
here is the code for line draw.
}
Run Code Online (Sandbox Code Playgroud)

在update方法中更新此函数.


TCP*_*TCP 6

起亚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)];
}
Run Code Online (Sandbox Code Playgroud)

节点部分(即@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
Run Code Online (Sandbox Code Playgroud)

第二部分(即@interface GetMyTouches:CCLayer):

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

记住触摸很容易.在触摸时知道你在做什么并不是火箭科学.

最后,如果你不理解我发布的任何内容......请继续烘焙.世界需要更多的巧克力蛋糕生产商.

澄清:

  1. 没有人学习形式切割'粘贴〜这个代码从来没有打算在没有爱抚的情况下工作
  2. 如果你没有看到幽默,那你就是错误的职业

值得注意的是,我喜欢一个好的巧克力蛋糕.世界确实需要更多出色的面包师.这不是侮辱,而是鼓励.

"在广场外看,找到充满了让生活变得有价值的知识的圈子"~Aenesidemus.

  • -1因为讽刺,加上你的代码对我不起作用(也许它已经过时了)?另一方面,seeu的例子运作得很好,我打赌你是一个伟大的面包师...... (19认同)
  • 不恰当的事......哈哈. (3认同)