2011-03-24 28 views
1

我正在構建一個遊戲,所以在我的結構中,我有Game類請求元素繪製自己。這是通過從UIView的繼承一個類調用Draw方法在遊戲類的實例進行UIGraphicsGetCurrentContext()有什麼問題?

// jeu的是遊戲的istance

- (void)drawRect:(CGRect)rect { 
    // Drawing code 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [jeu draw:context]; 
} 

//所以遊戲方法要求所有的對象畫自己

-(void) draw: (CGContextRef) gc{ 
    GameElement* element ; 

    for(element in drawArr) 
    { 
     [element draw:(CGContextRef) gc]; 
    } 
} 

一些元素(如球和兩個邊界它們都繼承了父類GameElement)正確繪製但有些邊界不

這裏的醫生邊框

- (void) draw: (CGContextRef) gc 
{  
    CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f}; // R G B + Alpha 
    CGPoint p [2]; 
    p[0].x = xStart ; 
    p[0].y = yStart ; 
    p[1].x = xEnd ; 
    p[1].y = yEnd ; 
    CGContextSetStrokeColor(gc, black); //Definie la couleur 
    CGContextStrokeLineSegments(gc, p, 2) ; 
} 

的AW方法所以在控制檯日誌我讀:

Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextDrawImage: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0 
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0 

而且我不知道什麼是地獄這意味着想法。如果在ViewDesigner中創建的圖形上下文對於所有爲什麼某些對象自己繪製而其他人不繪製都是相同的?

謝謝大家

回答

0

我們知道您的上下文變得無效。檢查每個GameElement繪製方法中的gc == nil,直到找到第一個具有無效上下文的方法。

UIView將在調用drawRect之前設置當前上下文:但不要手動調用drawRect,而是使用setNeedsDisplay方法。

其他方式的背景下可以死,呼籲UIGrhicsPushContext()

我想確定在那裏的無效,你就會有一個線索後。