【问题标题】:Drawing line on touches moved in COCOS2D在 COCOS2D 中移动的触摸上画线
【发布时间】:2011-05-31 03:07:38
【问题描述】:

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

其中,当用户将手指从一个点拖动到另一个点时,我需要画一条线。据我所知,我需要在Touches Moved method 中执行此操作,我可以从中获得积分。

但我不知道该怎么做。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 请不要一遍又一遍地问同一个问题,等待您的原始问题得到回答。谢谢。

标签: ios iphone cocos2d-iphone touchmove


【解决方案1】:

起亚奥拉。无聊迫使我就这个话题提供答案。

图层部分(即@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

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

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

记住触摸很容易。触摸时知道自己在做什么不是火箭科学。

最后,如果你不明白我发布的任何内容......请选择烘焙。世界需要更多的巧克力蛋糕生产商。

澄清:

  1. 没有人会从 cut 'n paste 中学到东西~这个 代码 从来没有打算在没有爱抚的情况下工作
  2. 如果你看不到幽默,那你就错了专业

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

“看看广场外面,找到充满知识的圆圈,让生活变得有价值”~Aenesidemus。

【讨论】:

  • 谢谢米拉卡。我已经使用 UIView 的 addLineToPoint 方法完成了这项工作。你知道如何删除使用 addLineToPoint 方法绘制的线吗?
  • inappropriateTouches...哈哈。
  • -1 因为讽刺,再加上你的代码对我不起作用(也许它已经过时了)?另一方面,seenu 的例子效果很好,我敢打赌你是个很棒的面包师......
  • @PeteHerbertPenito 是的......我猜烘焙部分有点过分了
  • seeu 的榜样在哪里? :)
【解决方案2】:
- (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.
}

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

【讨论】:

  • 您好,感谢您的回复。但是cocoas2d画线的问题是你需要在-(void)draw方法中调用ccDrawLine()方法,这个draw()方法需要在CCNodes子类中定义,然后你需要将该节点添加到你的 CCLayer 类..就像我已经这样做了...... [self addChild:[drawLine node]];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多