对 Objective C 有帮助吗?我的应用程序总是崩溃?

发布于 2024-10-24 06:10:18 字数 1868 浏览 1 评论 0原文

我正在为 Iphone/Itouch 创建一个应用程序,但我不断遇到几个重大漏洞,导致应用程序崩溃。在我的游戏中,当我按下播放键(从主屏幕)时,它会转到另一个包含该游戏的页面。但是,在它出现后,在控制台中我收到一条警告:内存级别=1。可能发生什么情况?这是我的 ViewDidLoad 方法:

-(void)viewDidLoad {

[super viewDidLoad];
array = [[NSMutableArray alloc] initWithCapacity:100];
bulletArray = [[NSMutableArray alloc] initWithCapacity:100];
pos = CGPointMake(0.0,-5.0);


NSString *path = [[NSBundle mainBundle] pathForResource:@"GloriousMorning" ofType:@"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:@"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];

}

另外,第二个问题,由于我的游戏是射击游戏,用户按下了标题为“Fire”的按钮。但是,每次我在设备上测试我的应用程序时,当我按下启动按钮时它就会崩溃。这是我的点火按钮代码。

-(IBAction)Fire {   
NSString *path = [[NSBundle mainBundle] pathForResource:@"gunShot" ofType:@"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:@"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];


//IBOutlet UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
newBullet.frame = CGRectMake(0, 0, 10.0, 10.0);
newBullet.center = CGPointMake(239.0, 236.0);
[bulletArray addObject:newBullet];
[self.view addSubview:newBullet];

首先

,我创建一个声音。然后,我将一颗子弹放在枪当前所在的位置,并将其添加到一个数组中,以便每隔 0.01 秒,在另一段代码中,我可以遍历该数组并检查每颗子弹以检测碰撞。

请告诉我我做错了什么。谢谢!!!

当我单击导致应用程序崩溃的 Fire 按钮时出现的错误是这样的:

GDB: Data Formatters暂时不可用,将在“继续”后重试(加载共享库时出现未知错误“

而且我认为当我尝试时我正在造成巨大的泄漏播放音频,至少有人告诉我(如果是这样,请告诉我如何解决)。

I am creating an app for the Iphone/Itouch, but I keep on running into a couple of major leaks that just crash the app. In my game, right when I press play (from the home screen)It goes to another page that has the game on it. But, right after it appears, ind the console I get a Warning: Memory level=1. What could be happening? Here is my ViewDidLoad Method:

-(void)viewDidLoad {

[super viewDidLoad];
array = [[NSMutableArray alloc] initWithCapacity:100];
bulletArray = [[NSMutableArray alloc] initWithCapacity:100];
pos = CGPointMake(0.0,-5.0);


NSString *path = [[NSBundle mainBundle] pathForResource:@"GloriousMorning" ofType:@"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:@"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];

}

And also, a second question, since my game is a shooting game, the user presses a button titled "Fire". But, every time I test my app on a device, It crashes when I press the fire button. Here is my code for the fire button.

-(IBAction)Fire {   
NSString *path = [[NSBundle mainBundle] pathForResource:@"gunShot" ofType:@"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:@"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];


//IBOutlet UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
newBullet.frame = CGRectMake(0, 0, 10.0, 10.0);
newBullet.center = CGPointMake(239.0, 236.0);
[bulletArray addObject:newBullet];
[self.view addSubview:newBullet];

}

First, I create a sound. Then, I place a bullet right where the gun is currently located, and add it to an array so that every .01 of a second, in another bit of code, I can run through the array and check every bullet to detect collision.

Please tell me what I am doing wrong. Thanks!!!

The error when I click the Fire Button that makes the app crash is this:

GDB: Data Formatters temporarily unavailable, will retry after a 'continue'(unknown error loading shared library "

And Also I think I am making a huge leak when I try to play the audio, at least that's what someone told me. (If that is the case, please tell me how to fix it)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2024-10-31 06:10:18

您分配了音频,因此需要在完成后释放它。将其设为 ivar 可能会更好,这样您就不必在每次按下启动按钮时设置和拆除音频。

You alloc'd theAudio so you need to release it when you are done. Might be better to make that a ivar so you don't have to set up and tear down the audio every time they press the fire button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文