Ios Cocos2d绘图应用程序制作线条

Ios Cocos2d绘图应用程序制作线条,ios,cocos2d-iphone,Ios,Cocos2d Iphone,我正在做一个非常简单的绘图应用程序。我用ccTouchMoved事件得到了要绘制的线。我将所有触摸移动的点放入一个数组中,然后使用for循环在所有点之间画一条线。现在,当我举起手指开始新的线条绘制时,我不想加入这些点。我也让那部分工作,但现在每当我开始一个新的绘图,整个屏幕闪烁 // // HelloWorldLayer.mm // DrawPuppets // // Created by Mohammad Azam on 12/11/12. // Copyright __MyCompa

我正在做一个非常简单的绘图应用程序。我用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
//
//HelloWorldLayer.mm
//拉丝木偶
//
//穆罕默德·阿扎姆于2012年12月11日创作。
//版权所有uu MyCompanyName uuu 2012。版权所有。
//
//导入接口
#导入“DrawPuppetLayer.h”
#导入“AppDelegate.h”
#导入“physicsprite.h”
枚举{
kTagParentNode=1,
};
#pragma标记-HelloWorldLayer
@接口DrawPuppetLayer()
-(d)物理;
-(void)addnewspriteaposition:(CGPoint)p;
-(void)创建菜单;
@结束
@puppetlayer的实现
+(场景*)场景
{
//“场景”是一个自动释放对象。
CCScene*scene=[CCScene节点];
//“层”是一个自动释放对象。
DrawPuppetLayer*层=[DrawPuppetLayer节点];
//将层作为子层添加到场景中
[场景添加子对象:层];
//返回现场
返回场景;
}
-(id)init
{
if((self=[super init])){
//启用事件
self.isTouchEnabled=是;
self.isAccelerometerEnabled=是;
指数=-1;
canvas=[[NSMutableArray alloc]init];
//初始物理
[自始物理];
[自计划更新];
}
回归自我;
}
-(作废)提款
{
如果([lineDrawing.points count]>1)
{

对于(inti=0;i尝试访问所有纹理,会有一段时间点数组变得太大而无法很好地绘制

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

这里有一个为IOSDVUK 2012编写的简单示例,它在Cocos2d v1中使用了GL_点,并且基于我们在开发时所采用的方法

谢谢!但即使我画了两条线,它实际上也会闪烁!这要么是您提供给ccDrawLine的点的问题,要么更可能是openGL状态的变化。