2014-03-12 21 views
0

應該很簡單。用spritekit和UITouch繪製直線創建多行

我想繪製一條直線,使用UITouch和Spritekit。但是,當touchesmoved被調用時,它會創建多行而不是一行。使用代碼如下:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    positionInScene1 = [touch locationInNode:self]; 

    pathToDraw = CGPathCreateMutable(); 

    selectorLine = [SKShapeNode node]; 
    selectorLine.strokeColor = [SKColor greenColor]; 
    selectorLine.lineWidth = 5; 
    [self addChild:selectorLine]; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    pathToDraw2 = CGPathCreateMutable(); 
    UITouch* touch = [touches anyObject]; 
    positionInScene2 = [touch locationInNode:self]; 

    CGPathMoveToPoint(pathToDraw2, NULL, positionInScene1.x, positionInScene1.y); 
    CGPathAddLineToPoint(pathToDraw2, NULL, positionInScene2.x, positionInScene2.y); 
    CGPathCloseSubpath(pathToDraw); 

    selectorLine.path = pathToDraw; 
} 

如果我提出

CGPathAddLineToPoint(pathToDraw, NULL, positionInScene2.x, positionInScene2.y); 

到touchesEnd,它會創建一個單一的線,但最終觸摸只有用戶後。我希望用戶在觸摸時看到正在繪製的線條。

感謝, 道格

+0

多行從哪裏到哪裏?從起始點到當前觸摸位置的每條附加行,還是之前和當前觸摸位置之間的附加行? – LearnCocos2D

回答

3

創建您的touchesMoved一條新路。您正在修改相同的路徑,只是添加越來越多的線路。

+0

不能相信我錯過了!感謝Theis。希望上面的代碼可以幫助其他新手sprtiekit開發人員 – Doug