I'm doing an app where user can click on a map and hear a different sound playing depending on the location clicked. I've got a point in polygon for this and it works perfectly. I've created audio playlist system also that queues audiofiles and plays them in order. This too works. However I'm trying to implement a method that checks if has already previously clicked the area/polygon he just clicked so that the same sound cannot be played again and again. I've got a PlaceInMap class that holds the polygon for an area and does the necessary checks if a clicked point is in the polygon etc etc...
At the moment I've got 2 instances of that class PlaceInMap stored in NSMutableArray and whenever a开发者_运维技巧 click happens I loop through that array and call each instances - (BOOL)checkCollisionWithLat:(NSString *)lat andLon:(NSString *)lon
method. The method return YES or NO depending on if the click is inside the polygon. This works fine (I'm outputing the results on a label). Here's the method:
- (BOOL)checkCollisionWithLat:(NSString *)lat andLon:(NSString *)lon {
if ( [self isLocationInsideX:lat andY:lon] ) {
if ( currentlyHere == 0) {
[[appDelegate audioPlayer] addToQueue:audioFileName];
[[appDelegate audioPlayer] iterateQueue];
}
currentlyHere = 1;
return YES;
}
else {
currentlyHere = 0;
return NO;
}
}
How ever for a some reason the check for currentlyHere
does not seem to work except for the first instance of the class... Partially.
This is what is happening: I get my initial location at infinity loop 1. This is inside instance one, the sound plays. I then click to polygon on instance two and it's sound plays. Now I click again to instance one's polygon and it's sound play byt when I click again to the instance twos polygon the sound does not play. But every time after that when I click to the instance one's polygon the sound plays as it is supposed to...
Now when I remove that if ( currentlyHere == 0 )
the sounds play correctly...
currentlyHere is NSInteger but I've tried it with int and BOOL also...
This is quite strange... The textual output always shows correctly on which polygon I have clicked.
And sorry for the typos my eyes are finnished... =P
Edit:
currentlyHere is defined in the header of the class as an instance variable. This is the init of the class:- (id) init {
if (self = [super init]) {
currentlyHere = 0;
[self setAppDelegate:[[UIApplication sharedApplication] delegate]];
}
return self;
}
It seems like the currentlyHere
variable is not properly set to zero; maybe the hit detection method is not called when your are outside. Add some NSLog
statements before the tests to check the value of the currentlyHere
.
- (BOOL)checkCollisionWithLat:(NSString *)lat andLon:(NSString *)lon {
NSLog("In checkCollisionWithLat (%d)", currentlyHere);
if ( [self isLocationInsideX:lat andY:lon] ) {
NSLog("Inside (%d)", currentlyHere);
if ( currentlyHere == 0) {
[[appDelegate audioPlayer] addToQueue:audioFileName];
[[appDelegate audioPlayer] iterateQueue];
}
currentlyHere = 1;
return YES;
}
else {
NSLog("Outside (%d)", currentlyHere);
currentlyHere = 0;
return NO;
}
}
精彩评论