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;
}
}
Related
How can I check if a method is or isn't running, in an if statement? For example-
if ([(UIButton *)sender isEqual:blueButton] && **showBlueText method is running** )
{
Keep playing.
}
else if ([(UIButton *)sender isEqual:blueButton] && **showBlueText method is NOT running** )
{
Game over.
}
-(void)showBlueText
{
blueText.hidden = NO;
[self performSelector:#selector(hideText) withObject:nil afterDelay:textDelay];
[self performSelector:#selector(showGreenText) withObject:nil afterDelay:hideDelay];
}
Just to clarify, 'showBlueText' is a part of its own loop that runs independently of this if statement. I'm just trying to check if showBlueText is currently running.
You want to record state here. Make a new instance variable in this class.
// new iVar
BOOL textIsShowing;
// method
-(void)showBlueText {
textIsShowing = YES;
blueText.hidden = NO;
[self performSelector:#selector(hideText) withObject:nil afterDelay:textDelay];
}
// method
- (void)hideText {
textIsShowing = NO;
blueText.hidden = YES;
}
// button press
- (void)buttonPressed {
if (textIsShowing) {
NSLog(#"Keep playing");
} else {
NSLog(#"Game over");
}
}
Between the time you call this method, and the animation stops, don't think of it as "running". It schedules code to be executed later. Instead you want to be notified after it has finally run.
And in this case it's easiest to keep track of the state yourself. Use a new variable to track the state of things, and change it's value when that state changes.
But can't you just check if (blueText.hidden)? Yeah, you could. But it's bad practice to store state about your program in some obscure property of a random unimportant object.
Examine your state to figure out what you show. Don't examine what's showing to figure out your state.
I suggest replace Keep playing or Game over or Doing stuff with NSLog() statements. I always use it to keep a track of the changes in program if I am getting unexpected result.
So your statement may look like:
NSLog(#"Keep playing");
I hope this helps.
Just check if the text is hidden. No need to store parallel state in your controller - all that does is create the possibility that they'll be out of sync.
So, here's how it goes.
I am currently working on Cocos2d game, which consists of many Obstacles. One obstacle gets added on the screen at an interval of 10 seconds like this.
ObstacleSprite* newObstacle = [ObstacleSprite spriteWithFile:#"Obstacle.png" rect:CGRectMake(0, 0, 20, 20)];
newObstacle.position = ccp(mainPlayer1.position.x,10);
[self addChild:newObstacle];
[self.arrayForObstacles addObject:newObstacle];
Now, I insert these obstacles into the arrayForObstacles because I also want to keep checking whether the Obstacles and MainPlayer don't collide.
I check it with the help of this function.
- (void) checkCollisionWithObstacle
{
if(mainPlayer1.playerActive)
{
for(int i = 0; i < [self.arrayForObstacles count]; i++)
{
ObstacleSprite* newObstacle = [self.arrayForObstacles objectAtIndex:i];
if(newObstacle != nil)
{
if(CGRectIntersectsRect([mainPlayer1 boundingBox], [newObstacle boundingBox]))
{
mainPlayer1.livesLeft--;
}
}
}
}
}
THE ISSUE
Problem is when I get to certain score, one of the Obstacles gets deleted. Removal of Obstacles works as in First In-First Out (FIFO) mode. So, to delete obstacles, I write the following method :
- (void) keepUpdatingScore
{
//update new score
mainPlayer1.score+=10;
//remove obstacle when score increases by 5k
if(mainPlayer1.score > 5000 && mainPlayer1.score > 0)
{
mainPlayer1.playerActive = NO;
if([self.arrayForObstacles count] > 0)
{
CCLOG(#"count is %d",[self.arrayForObstacles count]);
ObstacleSprite* newObstacle = [self.arrayForObstacles objectAtIndex:0];
[self.arrayForObstacles removeObjectAtIndex:0];
[self removeChild:newObstacle cleanup:YES];
CCLOG(#"count is %d",[self.arrayForObstacles count]);
}
mainPlayer1.playerActive = YES;
}
else
{
}
It crashes when score crosses 5000 mark!
UPDATE
Crash happens when it again goes to the method checkCollisionWithObstacle.
This is the THREAD Look.
THis is the line Which crashes.
you seem to be using mainPlayer1.playerActive as a semaphore to block checking the checkCollisionWithObstacle loop from a delete in the keepUpdatingScore method (are they asynchronous ?). Assuming they are, the way you are blocking the loop access wont work if the code enters keepUpdatingScore AFTER the loop started in checkCollisionWithObstacle ... your mileage will vary.
I have an array of CLLocation objects I parsed from a file. I'd want to simulate that the user is moving along that route, and I have implemented this:
for (CLLocation *loc in simulatedLocs) {
[self moveUser:loc];
sleep(1);
}
This is the method called in the loop:
- (void)moveUser:(CLLocation*)newLoc
{
CLLocationCoordinate2D coords;
coords.latitude = newLoc.coordinate.latitude;
coords.longitude = newLoc.coordinate.longitude;
CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:coords];
annotation.title = #"User";
// To remove the previous location icon
NSArray *existingpoints = self.mapView.annotations;
if ([existingpoints count] > 0) {
for (CustomAnnotation *annotation in existingpoints) {
if ([annotation.title isEqualToString:#"User"]) {
[self.mapView removeAnnotation:annotation];
break;
}
}
}
MKCoordinateRegion region = { coords, {0.1, 0.1} };
[self.mapView setRegion:region animated:NO];
[self.mapView addAnnotation: annotation];
[self.mapView setCenterCoordinate:newLoc.coordinate animated:NO];
}
But only last location in the array and its region are displayed in the mapView when running the iPhone simulator. I'd like to simulate that user is "moving" each 1 sec, how could I do that?
Thanks!
Looping through all the locations all at once with a sleep at each iteration won't work because the UI will be blocked until the method the loop is in finishes.
Instead, schedule the moveUser method to be called individually for each location so that the UI is not blocked throughout the sequence. The scheduling can be done using an NSTimer or possibly something simpler and more flexible such as the performSelector:withObject:afterDelay: method.
Keep an index ivar to keep track of which location to move to each time moveUser is called.
For example:
//instead of the loop, initialize and begin the first move...
slIndex = 0; //this is an int ivar indicating which location to move to next
[self manageUserMove]; //a helper method
-(void)manageUserMove
{
CLLocation *newLoc = [simulatedLocs objectAtIndex:slIndex];
[self moveUser:newLoc];
if (slIndex < (simulatedLocs.count-1))
{
slIndex++;
[self performSelector:#selector(manageUserMove) withObject:nil afterDelay:1.0];
}
}
The existing moveUser: method does not have to be changed.
Note that the user experience and code can be simplified if instead of removing and adding the annotation again every time, you add it once at the beginning and just change its coordinate property at each "move".
You should not be using MKAnnotation, but MKPolyline. Check the documentation. Also, check the WWDC MapKit video from 2010. It has a n example of a mutable MKPolyline.
Your problem is that the for loop with the sleep in it, is blocking the main thread until the end of the for loop. That freezes the whole user interface for that whole period, including any changes you do in moveUser.
Instead of the for loop, use an NSTimer that fires every second and does one step each time.
Or possibly, for a smoother effect, setup an animation that moves the position of the annotation along a predefined path.
I want to create my own health indicator by aligning multiple images which represent one percent. So basically, based on the current health, I align as many one-percent parts as needed. However, removing them seems to be a problem.
-(void)updateHealthIndicator:(ccTime)delta{
//getting health and healthReduction (removed for better readability). This part does not affect the functioning of the loop...
if(health-healthReduction > 0 ){
NSLog(#"updatehealthindicator called ! health = %d ", health);
health -= healthReduction;
[self removeChildByTag:1000 cleanup:YES];
for (int i = health; i>0; i--){
onePercent = [CCSprite spriteWithFile:#"onepercentofhi.png"];
onePercent.anchorPoint = ccp(0,0);
onePercent.position = ccp(880+(-onePercent.contentSize.width) * i,712 );
[self addChild:onePercent z:2 tag:1000];
}
}
The health indicator shows up, but it only seems to remove the first "one-percent" piece. Are all sprites with tag 1000 affected by this [self removeChildByTag:1000 cleanup:YES]; ?
Only one view with the given tag is removed.
However, you could extend CCNode with the following code to remove all children
-(void) removeChildrenByTag:(int)aTag cleanup:(BOOL)cleanup
{
NSAssert( aTag != kCocosNodeTagInvalid, #"Invalid tag");
int w=[children count]-1;
while(w>=0){
CocosNode *node=[children objectAtIndex:w];
if( node.tag == aTag ){
[self detachChild:node cleanup:cleanup];
}
w--;
}
}
Note: This is a proposed solution to be integrated into Cocos2D but hasn't made it yet.
I have the following method in my app :
- (void) loadModel {
// Runs in a seperate thread so we need to create an additional NSAutoreleasePool
pool = [[NSAutoreleasePool alloc] init];
NSData *getData = [activeModelInfo getFileData];
if (getData) {
if ([activeModel loadFromFileData:daeData]) {
[activeModel reset];
[mainViewController performSelectorOnMainThread:#selector(showModelView) withObject:nil waitUntilDone:NO];
}
else {
}
}
else {
}
[pool release];
}
The loadFromFileData method calls the following code which loads data. :
- (void) loadVerticesFromSource:(NSString*)verticesSourceId meshNode:(TBXMLElement*)meshNode intoMesh: (Mesh3D*) mesh {
NSLog(#"Getting Vertices for Source %#",verticesSourceId);
FloatArray *floatArray = [self getFloatArrayFromSource: verticesSourceId meshNode: meshNode];
if (floatArray) {
[daeFloatArray addObject:floatArray];
}
if (floatArray) {
if ([floatArray.array count] % 3 != 0) {
NSLog(#"Float array length not divisible by 3!");
}
else {
mesh->verticesCount = [floatArray.array count] / 3;
mesh->vertices = malloc(sizeof(Vector3D) * mesh->verticesCount);
for (int i=0; i<mesh->verticesCount; i++) {
mesh->vertices[i].x = [[floatArray.array objectAtIndex:(i*3)] floatValue];
mesh->vertices[i].y = [[floatArray.array objectAtIndex:(i*3)+1] floatValue];
mesh->vertices[i].z = [[floatArray.array objectAtIndex:(i*3)+2] floatValue];
// update extents information
if (!extents.pointDefined || mesh->vertices[i].x < extents.minX) extents.minX = mesh->vertices[i].x;
else if (!extents.pointDefined || mesh->vertices[i].x > extents.maxX) extents.maxX = mesh->vertices[i].x;
if (!extents.pointDefined || mesh->vertices[i].y < extents.minY) extents.minY = mesh->vertices[i].y;
else if (!extents.pointDefined || mesh->vertices[i].y > extents.maxY) extents.maxY = mesh->vertices[i].y;
if (!extents.pointDefined || mesh->vertices[i].z < extents.minZ) extents.minZ = mesh->vertices[i].z;
else if (!extents.pointDefined || mesh->vertices[i].z > extents.maxZ) extents.maxZ = mesh->vertices[i].z;
if (!extents.pointDefined) extents.pointDefined = YES;
[pointerStorageArray addObject:[NSValue valueWithPointer:mesh->vertices]];
}
}
}
}
Since this method is called several times while the data loads, each time mallocing memory for the mesh->vertices struct, I have created a pointerStorage array where I store the pointer to the malloced memory.
The app then displays a 3D object using OpenGL ES. When the user presses a Main Menu button, I then free up the pointerStorageArray as follows :
- (void) freeUpMallocedMemory
for (NSValue * value in pointerStorageArray) {
free(value);
}
The problem is that the app then crashes during this process. All I get is the EXC_BAD_ACCESSS error message and a pointer to the following line of code in the loadModel method above :
[pool release];
Nothing in the stack trace. I have also tried turning on NSZombie, NSAutoreleaseTrackFreedObjectCheck and NSDebugEnabled, but I still don't get any additional information.
The odd thing is that if I put a delay on the button of 2 seconds (i.e only trigger the freeUpMallocedMemory method after 2 seconds), the app no longer crashes and works fine.
Can anyone suggest what might be causing this - really confused and have already spent a few days troubleshooting.
Thank you !
You are over-releasing... the values in pointerStorageArray are created via a convenience method and as such don't need releasing, simply removing them from the array will dispose of them.