2014-09-13 28 views
0

在我的應用程序中,我試圖訪問數組值,但數組返回零值。我一直堅持這幾個小時,似乎無法解決它。因此任何幫助,將不勝感激:在其他實例中訪問數組值

@implementation MyScene{ 
NSMutableArray *_touchPoints; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint touchPoint = [[touches anyObject] locationInNode:self.scene]; 
    [self clearTouchPoints]; 
    [self addTouchPointToMove:touchPoint]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint touchPoint = [[touches anyObject] locationInNode:self.scene]; 
    CGPoint targetPoint = CGPointMake(touchPoint.x , touchPoint.y - delta.y); 
    [self addTouchPointToMove:touchPoint]; 
    NSLog(@"Value: %lf", touchPoint.y); 
} 

- (void)move:(NSNumber *)_tempTime{ 
    CGPoint currentPoint = [_touchPoints[0] CGPointValue]; 
    CGPoint targetPoint = [_touchPoints[1] CGPointValue]; 
    NSLog(@"Check Value: %lf", targetPoint.y); 
} 

- (void)addTouchPointToMove:(CGPoint)point { 
    [_touchPoints addObject:[NSValue valueWithCGPoint:point]]; 
} 

- (void)clearTouchPoints { 
    [_touchPoints removeAllObjects]; 
} 

回答

0

陣列被從未初始化(例如,_touchPoints = [NSMutableArray new];),所以它是nil。嘗試調用nil對象上的方法什麼也不做,也不會抱怨(即不會引發異常)。所以在addTouchPointToMove方法:

[_touchPoints addObject:[NSValue valueWithCGPoint:point]]; 

什麼都不做。

init方法中,初始化該數組。

相關問題