Drawing line on touches moved in COCOS2D

后端 未结 2 1152
谎友^
谎友^ 2020-12-29 00:51

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 kno

相关标签:
2条回答
  • 2020-12-29 01:10
    - (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);   
    }
    

    Try the above code. It will get the coordinate points when touches moved in iphone.

    To draw line, use something like this:

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

    Update this function in update method.

    0 讨论(0)
  • 2020-12-29 01:12

    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 from 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.

    0 讨论(0)
提交回复
热议问题