2016-08-05 3 views
0

뱀의 종류를 사용하는 게임을 만들려고합니다. 지금까지 제대로 작동했지만 잘 작동하지만 약간의 시간이 지나면 다소 느려지 며 특히 Iphone 5 이하에서는 느려집니다.DrawRect 최적화 : Snake 종류의 게임

내가 그것을 때문에의 drawRect의 알고 :

-(void) drawRect:(CGRect)rect{ 
    NSDate *start = [NSDate date]; 
    CGFloat rr, rg, rb, ra; 
    [redcolorr getRed:&rr green:&rg blue:&rb alpha:&ra]; 
    CGFloat gr, gg, gb, ga; 
    [greencolorr getRed:&gr green:&gg blue:&gb alpha:&ga]; 
    CGFloat br, bg, bb, ba; 
    [bluecolorr getRed:&br green:&bg blue:&bb alpha:&ba]; 

    if ([myName isEqual:@"Server"]) { 
     for (int i= 0; i<ServerBody.count; i++) { 
      Snake *a; 
      a = [ServerBody objectAtIndex:i]; 

      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, rr, rg, rb, ra); 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x,a.y,7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 

    } 
    else{ 

     for (int i= 0; i<IntelligentServerBody.count; i++) { 
      Snake *a; 
      a = [IntelligentServerBody objectAtIndex:i]; 

      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, rr, rg, rb, ra); 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 
    } 
    if ([myName isEqual:@"Client0"]) { 
     for (int i= 0; i<Client0Body.count; i++) { 
      Snake *a; 
      a = [Client0Body objectAtIndex:i]; 
      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, br, bg, bb, ba); // white color 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 
    } 
    else{ 

     for (int i= 0; i<IntelligentClient0Body.count; i++) { 
      Snake *a; 
      a = [IntelligentClient0Body objectAtIndex:i]; 

      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, br, bg, bb, ba); // white color 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 

    } 
    NSTimeInterval timeInterval = [start timeIntervalSinceNow]; 
} 

가 있다는 사실을 숙지하십시오 :

  1. 의 drawRect이 모두 뱀
  2. 서버가 다시 그릴 때마다 0.05 초라고 먼저 아이폰, 및 클라이언트, 두 번째 아이폰 (멀티 페어)

그럼 지금 알려주세요. 코드 줄을 작성하거나 불필요한 줄이 있습니다. 뱀을 그리는 다른 방법이 있습니까? 나는 뱀이 빛을 내고 싶을 때마다 (색칠 할 때마다 색을 바꾼다.) 어쩌면 "자라다/자라다"(물방울의 크기를 바꾼다.)

+0

'UIGraphicsPush/Pop' 호출은 중복됩니다. 'drawRect :'는 GPU를 활용하지 못하기 때문에 자주 호출 될 때마다 조금 뒤떨어 지지만 말입니다. [Core Animation] (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html) 또는 [Core Animation]을 사용하여 원하는 효과를 다시 만들려고합니다. Sprite Kit] (https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html) 대신 GPU를 활용하므로보다 효율적입니다. – Hamish

+0

그래, 이처럼 반복 그리기 호출을 사용하고 싶지는 않을 것이다. 새로 고침 간격마다 그래픽 컨텍스트를 작성하고있다. 예를 들어, 각 뱀에 대해 작은 "세그먼트"를 만든 다음 SpriteKit을 사용하여 세그먼트 구성을 통해 렌더링하면 더 나은 결과를 얻을 수 있습니다. 그런 식으로 각 세그먼트는 최적의 방식으로 렌더링되는 GPU 텍스처입니다. 이 드로 로직 대신 X, Y 좌표 및 각도를 사용하여 세그먼트를 연결하는 코드를 작성해야합니다. 약간의 리팩토링이 필요하지만 그래픽 서브 시스템을보다 효율적으로 사용할 수 있습니다. – MoDJ

답변

0

나는 경로를 사용하는 방법을 알아 냈다. 효과적인 :. 나는 gameloop 타이머를 증가시킬 수 있다면 한 아이폰에서 다른 위치마다 0.05 초를 보낼 Multipeer 연결을 사용하고

-(void) drawRect:(CGRect)rect{  
     a = [ServerBody objectAtIndex:0]; 
     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     CGContextSetLineWidth(ctx, 7.5*k*sizee); 
     CGContextMoveToPoint(ctx, a.x, a.y); 
     CGContextSetStrokeColorWithColor(ctx, redcolorr.CGColor); 
     for (int i= 0; i<ServerBody.count; i++) { 
      a = [ServerBody objectAtIndex:i]; 
      CGContextAddLineToPoint(ctx,a.x, a.y); 
     } 
     CGContextStrokePath(ctx); 
} 

이제 질문입니다 그러나 IM이 전송 :.

NSMutableArray *Messagee = [[NSMutableArray alloc] initWithCapacity:10]; 
     [Messagee addObject:x]; 
     [Messagee addObject:y]; 
     [Messagee addObject:ang]; 
     [Messagee addObject:ind]; 


     NSData* Message = [NSKeyedArchiver archivedDataWithRootObject:Messagee]; 
     [self.partyTime sendData:Message 
         withMode:MCSessionSendDataUnreliable 
          error:nil]; 

그리고 불행히도 다른 iPhone에서 수신하는 데 시간이 걸립니다. 내 패킷이 너무 큽니까? 스트림을 사용하여 더 빨리 위치를 전송해야합니까? 기타 제안 사항?

답장을 보내 주셔서 감사합니다.