0

我正在制作一个非常简单的绘图应用程序。我使用 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

谁能发现我的错误?

4

1 回答 1

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 编写的简单示例项目,它在 Cocos2d v1 中使用 GL_POINTS,并且基于我们在开发SketchShare时采用的方法

于 2012-12-12T01:23:53.137 回答