I have an variable with the datatype CGPoint, named endPosition. When endPosition gets its value inside an if statement, it returns this crazy value: End position: {1.6347776e-33, 1.4012985e-45}.
1. example:
if ([touch view] != background)
{
CGPoint location = [touch locationInView:self.view];
CGPoint endPosition;
if([touch view] == circle){
CGPoint endPosition = {462.5, 98.5};
}
CGFloat xDist = (endPosition.x - location.x);
CGFloat yDist = (endPosition.y - location.y);
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));
NSLog(@"End Position: %@", NSStringFromCGPoint(endPosition));
}
When the CGPoint endPosition is not inside this if statement, i get the right value: End position: {462.5, 98.5}
2. example:
if ([touch view] != background)
{
CGPoint location = [touch locationInView:self.view];
CGPoint endPosition = {462.5, 98.5};
CGFloat xDist = (endPosition.x - location.x);
CGFloat yDist = (endPosition.y - location.y);
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));
NSLog(@"End Position: %@", NSStringFromCGPoint(endPosition));
}
Can anyone tell me what to do? I need this if statement :) Thanks in advance.
That's because in the first case you don't set a value for
endPointif[touch view] != circle.In that case your variable is uninitialized and you get a random value that happens to be there in memory. You have to handle the other case (
else) or initialize your variable to some value when you declare it, likeCGPointZero.