Cocos2d绘图应用程序制作线

时间:2012-12-11 22:37:30

标签: ios cocos2d-iphone

我正在制作一个非常简单的绘图应用程序。我得到了使用ccTouchMoved事件绘制的线条。我将所有触摸移动的移动点放入一个数组中,然后使用for循环在所有点之间绘制一条线。现在,当我举起手指并开始新的线条画时,我不想加入这些点。我也得到了那个部分,但是现在每当我开始新的绘图时,整个屏幕都会闪烁。

//
//  HelloWorldLayer.mm
//  DrawPuppets
//
//  Created by Mohammad Azam on 12/11/12.
//  Copyright __MyCompanyName__ 2012. All rights reserved.
//

// Import the interfaces
#import "DrawPuppetLayer.h"
#import "AppDelegate.h"
#import "PhysicsSprite.h"

enum {
    kTagParentNode = 1,
};


#pragma mark - HelloWorldLayer

@interface DrawPuppetLayer()
-(void) initPhysics;
-(void) addNewSpriteAtPosition:(CGPoint)p;
-(void) createMenu;
@end

@implementation DrawPuppetLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    DrawPuppetLayer *layer = [DrawPuppetLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{
    if( (self=[super init])) {

        // enable events

        self.isTouchEnabled = YES;
        self.isAccelerometerEnabled = YES;
        index = -1;

        canvas = [[NSMutableArray alloc] init];

        // init physics
        [self initPhysics];

        [self scheduleUpdate];
    }
    return self;
}


-(void) draw
{

    if([lineDrawing.points count] > 1)
    {

    for(int i = 0; i<([canvas count]) ;i++)
    {
        LineDrawing *drawing = (LineDrawing *) [canvas objectAtIndex:i];

        for(int j=0;j<[drawing.points count] - 1;j++)
        {

        LinePoint *firstPoint = (LinePoint *) drawing.points[j];
        LinePoint *secondPoint = (LinePoint *) drawing.points[j + 1];

        CGPoint point1 = [[CCDirector sharedDirector] convertToGL:CGPointMake(firstPoint.x, firstPoint.y)];

        CGPoint point2 = [[CCDirector sharedDirector] convertToGL:CGPointMake(secondPoint.x, secondPoint.y)];

        ccDrawLine(point1, point2);
        }

    }


    }


    //
    // IMPORTANT:
    // This is only for debug purposes
    // It is recommend to disable it
    //
    [super draw];

    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );

    kmGLPushMatrix();

    world->DrawDebugData();

    kmGLPopMatrix();
}




-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    lineDrawing = [[LineDrawing alloc] init];
    lineDrawing.points = [[NSMutableArray alloc] init];

    [canvas addObject:lineDrawing];

}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];

    LinePoint *linePoint = [[LinePoint alloc] init];
    linePoint.x = point.x;
    linePoint.y = point.y;


    [lineDrawing.points addObject:linePoint];

}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Add a new body/atlas sprite at the touched location
    for( UITouch *touch in touches ) {
        CGPoint location = [touch locationInView: [touch view]];

        location = [[CCDirector sharedDirector] convertToGL: location];


    }
}



@end

有人能发现我的错误吗?

1 个答案:

答案 0 :(得分:1)

尝试一直到纹理,你的点数组太大而不能很好地绘制

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get the node space location of our touch
    CGPoint location    = [self getNodeSpaceTouchLocationFromUIEvent:event];

    // draw with our current location and a random colour
    [_canvas begin]; // our rendertexture instance

    // do your drawing here
    [_pen drawPenWithPosition:location andColour:_colour];  

    // end capturing the current pen state
    [_canvas end];
}

这是一个为iOSDevUK 2012编写的简单example project,它在Cocos2d v1中使用GL_POINTS,并基于我们在开发SketchShare

时采用的方法