Game, multiple object. explanation inside.

Nibbie
Posts: 1
Joined: 2009.04
Post: #1
Hi, this is my first post here.
I am currently trying to develop a game, my first game for the iPhone (I developed a few small apps, kind of utility apps for the iPhone/iPod). Anyways in my game I have multiple object, 10 bullets at max and like 5 more enemy objects.
The object are represented on screen as UIImageViews, I hold all of the object in arrays... an array for bullets and an array for enemies.
I am manipulating my arrays via a timer, 1/60 timer that updates the position for each object, bullets and enemies, and checks for collisions.
The moment there is a collision I remove the collided images from the view, remove them from the arrays and then release - but in the removing method I get obj_error... I tried debugging it and it removes and releases the bullet perfectly but when I remove and release the enemy object, it get the error when I release it.
Any ideas? the methods are written quite the same just with diff objects.

enemy release code.
Code:
- (void)meteorClean:(NSInteger)place {
    Meteor *justM = [meteorArray objectAtIndex:place];
    [justM.meteorImage removeFromSuperview];
    justM = nil;
    [justM release];
    [meteorArray removeObjectAtIndex:place];
}

bullet release code.

Code:
- (void)cleanUp:(NSInteger)place {
    ball *just = [bulletArray objectAtIndex:place];
    [just.ballImage removeFromSuperview];
    [bulletArray removeObjectAtIndex:place];
    just = nil;
    [just release];
}


enemy is meteor...
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
You do realize you're calling [nil release] in both of those cases, right? Due to the Objective-C runtime checking for nil selectors, this won't actually crash, but it won't do what you want it to either. I don't understand why you'd explicitly set a local variable to nil at the end of a function, either.

Also, NSMutableArray retains items that are added to it and releases them when removed, so it generally makes more sense to allow your array to be the sole owner of your object by releasing it immediately after adding it. When you remove it from the array, it gets released and deallocated without you having to release it again yourself.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Downloading the same game multiple times EndeavorBros 0 2,659 Nov 17, 2009 12:57 AM
Last Post: EndeavorBros
  Taking Pictures of Game State inside an app Bersaelor 7 5,350 Nov 3, 2009 05:07 AM
Last Post: Bersaelor
  OpenGL ES 2d game - multiple EAGL Views or not? Elphaba 4 4,905 Aug 4, 2009 10:42 AM
Last Post: maximile
  Logic Based Multiple View Game sum2cubes 1 2,837 Jul 2, 2009 08:30 AM
Last Post: bruss14
  How do I use a mutable array of custom object 'enemies' in my game? hybryd 3 3,112 Apr 20, 2009 12:22 PM
Last Post: Josh