Wednesday, 18 September 2013

iphone- App Crashes for No Reason

iphone- App Crashes for No Reason

Here is some background information. I originally developed this game for
the iPhone 3.1 software, but then edited the code to make it compatible
with newer software. My app is a space game.Basically, UIImageViews start
to spawn at the top of the screen and come down, and you have to move your
character (another UIImageview) out of the way. All of the code works
flawlessly on the simulator. It also generally works fine on the device.
However, just on the device, once every five or six times, halfway through
the game the volume shuts off (I have a fire button which not only spawns
a new uiimageview that moves accross the screen, but it also plays an
avaudio noise). When I click the fire button, no avaudio sound is made.
The rest of the game keeps running fine. Then, when I crash into a meteor,
the game crashes. I don't know where or what the problem is because the
game only has the sound turn off and then crash when you hit a meteor like
once out of every six times you play the game, and there is no problems at
all on the simulator, only on the device. If you need any additional
information or have any suggestions, please feel free to ask.
Here is my code for when you crash into a meteor (even if the problem is
here, it does not explain my avaudioplayer malfunctioning only when the
game is about to crash):
if (CGRectIntersectsRect(one.frame, image.frame) && ![meteorsToRemove
containsObject:one]) {
NSURL *url = [NSURL fileURLWithPath:[NSString
stringWithFormat:@"%@/shipExplosion.wav", [[NSBundle mainBundle]
resourcePath ]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
[error release];
background.image = [UIImage imageNamed:@"gameOver.png"];
fire.hidden = YES;
image.hidden=YES;
quit2.hidden=NO;
one.hidden=NO;
highscorePageButton.hidden=NO;
playAgain.hidden=NO;
image.center=CGPointMake(500,500);
secondsTotalCount.hidden=NO;
NSString *newText = [[NSString alloc] initWithFormat:@"You Lasted
%d Seconds", _totalSeconds];
secondsTotalCount.text = newText;
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSNumber *highscore = [[NSNumber alloc]
initWithInteger:_totalSeconds];
NSString *choice = [NSString stringWithFormat:@"%@/userschoice",
documentsDirectory];
NSMutableArray *array;
if ([[NSFileManager defaultManager] fileExistsAtPath:choice]) {
array = [NSMutableArray arrayWithContentsOfFile:choice];
}
else {
array = [[[NSMutableArray alloc] init]autorelease];
}
[array addObject:highscore];
[array writeToFile:choice atomically:YES];
[newText release];
}
Note: one is the name of a meteor, image is the name of your
character.MeteorsToRemove is an array of inactive meteors (that got shot
down by the character). Also, I save the score of the character in a text
file named choice.

No comments:

Post a Comment