在我的应用程序中,我试图访问数组值,但数组返回零值。我现在已经坚持了几个小时,似乎无法解决它。所以任何帮助将不胜感激:
@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 :(得分:0)
数组永远不会被初始化(例如,_touchPoints = [NSMutableArray new];
),因此它是nil
。试图在nil
对象上调用方法什么也不做,也没有抱怨(即不抛出异常)。所以在addTouchPointToMove
方法中:
[_touchPoints addObject:[NSValue valueWithCGPoint:point]];
什么也没做。
在init
方法中,初始化数组。