2012-11-26 102 views
0

我使用cocos2d 2.0和Xcode 4.5。我正在努力學習如何畫線。我可以繪製一條線,但是在我畫了幾行之後,模擬器上出現了嚴重的性能問題。Cocos2d ccDrawLine性能問題

模擬器開始凍結,繪製線條非常非常緩慢,最糟糕的是,我猜是因爲-(void)draw被稱爲每一幀,畫面上的標籤成爲大膽

線前

enter image description here

行後;

enter image description here

我用下面的代碼: .M

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


     CCLabelTTF *label = [CCLabelTTF labelWithString:@"Simple Line Demo" fontName:@"Marker Felt" fontSize:32]; 
     label.position = ccp(240, 300); 
     [self addChild: label]; 

     _naughtytoucharray =[[NSMutableArray alloc ] init]; 

     self.isTouchEnabled = YES; 


    } 
    return self; 
} 

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    BOOL isTouching; 
    // determine if it's a touch you want, then return the result 
    return isTouching; 
} 


-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [ touches anyObject]; 
    CGPoint new_location = [touch locationInView: [touch view]]; 
    new_location = [[CCDirector sharedDirector] convertToGL:new_location]; 

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view]; 
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation]; 
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation]; 
    // add my touches to the naughty touch array 
    [_naughtytoucharray addObject:NSStringFromCGPoint(new_location)]; 
    [_naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)]; 
} 
-(void)draw 
{ 
    [super draw]; 
    ccDrawColor4F(1.0f, 0.0f, 0.0f, 100.0f); 
    for(int i = 0; i < [_naughtytoucharray count]; i+=2) 
    { 
     CGPoint start = CGPointFromString([_naughtytoucharray objectAtIndex:i]); 
     CGPoint end = CGPointFromString([_naughtytoucharray objectAtIndex:i+1]); 
     ccDrawLine(start, end); 

    } 
} 
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    ManageTraffic *line = [ManageTraffic node]; 
    [self addChild: line z:99 tag:999]; 
} 

只見幾個空中交通管制的遊戲,如飛行控制,ATC瘋狂作品真的很好。

由於CCDrawLine/UITouch *touch或者它是一個常見問題,是否會發生此性能問題? 什麼飛行控制,ATC瘋狂可能用於畫線?

在此先感謝。

編輯::::

OK我想問題不是ccDrawLine,問題是我打電話ManageTraffic *line = [ManageTraffic node];每個觸摸結束它的時候調用節點的init所以它覆蓋場景

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

    ManageTraffic *line = [ManageTraffic node]; 
    [self addChild: line z:99 tag:999]; 
} 
+0

你不能在模擬器中真正判斷性能。您需要在設備上進行測試。 –

回答

1

有三樣東西去上:

  1. 您評估在模擬器上的性能。按照Ben的說法,在設備上測試它。
  2. 您將點存儲爲字符串並將字符串轉換回CGPoint。這是非常低效的。
  3. ccDrawLine不完全有效。對於幾十個線段來說沒關係。在你的情況下可能不會(見下文)。

對於#2,創建一個只有CGPoint屬性的點類,並使用它來將點存儲在數組中。刪除字符串轉換或打包到NSData中。

對於#3,確保僅在新點與前一點相距至少n點時才添加新點。例如,距離10應該減少點數,同時仍然允許相對較細的線條細節。

關於#3,我注意到您將當前和前一個點添加到數組。爲什麼?您只需添加新點,然後從索引0到1,從1到2等等繪製點。你只需要測試只有1分的情況。先前的觸摸事件的位置始終是下一個觸摸事件的previousLocation。所以你需要存儲兩倍的點數。

+0

CGPoint需要單身嗎? –