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用に作成された簡単なサンプルプロジェクトです。Cocos2dv1でGL_POINTSを使用し、SketchShareの開発時に採用したアプローチに基づいています。

于 2012-12-12T01:23:53.137 に答える