Cocos 2d画线不起作用

时间:2011-11-10 08:09:48

标签: ios ios4 cocos2d-iphone ios5

我想在cocos 2d中用手指触摸画线。

-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
    UITouch *touch = [inappropriateTouches anyObject];

    CGPoint currentTouchArea = [touch locationInView:[touch view] ];
    CGPoint lastTouchArea = [touch previousLocationInView:[touch view]];

    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);  

    glColor4f(0.8, 1.0, 0.76, 1.0);
    glLineWidth(6.0f);
    ccDrawLine(currentTouchArea, lastTouchArea);
}

我使用此代码,但屏幕上没有任何内容。我的代码出了什么问题?

2 个答案:

答案 0 :(得分:4)

您想要在draw方法中执行的所有OpenGL绘图。像这样:

-(void)draw
{
    if(lastTouchArea != 0)
    {
       glColor4f(0.8, 1.0, 0.76, 1.0);
       glLineWidth(6.0f);
       ccDrawLine(currentTouchArea, lastTouchArea);
       lastTouchArea = 0; 
    }
}

答案 1 :(得分:2)

试试这个:在NSMutableArray中保存一行

-(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)];
}

@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);
    }
}


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

希望这个帮助