我使用cocos2d 2.0和Xcode 4.5。我正在努力學習如何畫線。我可以繪製一條線,但是在我畫了幾行之後,模擬器上出現了嚴重的性能問題。Cocos2d ccDrawLine性能問題
模擬器開始凍結,繪製線條非常非常緩慢,最糟糕的是,我猜是因爲-(void)draw
被稱爲每一幀,畫面上的標籤成爲大膽
:
行後;
我用下面的代碼: .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];
}
你不能在模擬器中真正判斷性能。您需要在設備上進行測試。 –