在COCOS2D中移动的触摸上的绘制线 [英] Drawing line on touches moved in COCOS2D

查看:150
本文介绍了在COCOS2D中移动的触摸上的绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



因此,当用户将手指从一个点拖动到另一个点时,我需要绘制一条线。至于我的知识是关注,我需要在触摸移动方法这里可以得到的点。



但我不知道该怎么做。任何人都可以帮助我吗?

解决方案

起亚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]];

//翻转肚腩。没有人喜欢从后面进入。
currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];

//调用控制台不适当的触摸
NSLog(@current x =%2f,y =%2f,currentTouchArea.x,currentTouchArea.y);
NSLog(@last x =%2f,y =%2f,lastTouchArea.x,lastTouchArea.y);

//将我的触摸添加到淘气触摸数组
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: *)事件
{
DrawMyTouch * line = [DrawMyTouch node];
[self addChild:line];
}

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



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



澄清:


  1. 从未想过是否可以在没有爱抚的情况下工作

  2. 如果你看不到幽默,


  3. 注意,我喜欢一个好的巧克力蛋糕。世界真的需要更多的梦幻般的面包师。这不是侮ult,这是鼓励。



    看看广场外面,找到充满了让生活变得生活的知识的圆圈〜Aenesidemus。

    b $ b

    I'm developing a game for iPhone using COCOS2D.

    In that, I need to draw a line when user drag his finger from a point to another. As far as my knowledge is concern I need to do this in Touches Moved method from where I can get the points.

    But I don't know how to do this. Can anybody help me on this?

    解决方案

    Kia ora. Boredom compels me to provide an answer on this topic.

    Layer part (i.e. @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)];
    }
    

    Node part (i.e. @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
    

    Layer part II (i.e. @interface GetMyTouches : CCLayer):

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

    Remember touching is easy. Knowing what you're doing while touching isn't rocket science.

    Finally, if you don't understand anything i've posted ... take up baking. The world needs more chocolate cake producers.

    Clarification:

    1. No one learns form cut 'n paste ~ this code was never meant to work without caressing
    2. If you fail to see the humour, you're in the wrong profession

    Of note, I love a good chocolate cake. The world really does need more fantastic bakers. That's not an insult, that's encouragement.

    "Look outside the square, to find the circle filled with the knowledge that makes life worth living" ~ Aenesidemus.

    这篇关于在COCOS2D中移动的触摸上的绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆