Some sort of array problem

Member
Posts: 102
Joined: 2005.01
Post: #1
Im not really sure what is going on an array (amebas) isnt holding the last object stuck into it, i think. After the last object is placed in the array, i can get info about it in that same method, but as soon as i try to call its methods from a different method everything is 0 whereas the other 3 objects are fine.

here is my method for loading the objects into the array

i dont think the beginning code matters much skip down the the note that says setup players

Code:
- (void)setupH:(int)h W:(int)w players:(int)p
{
    width = w;
    height = h;
    srand(time(NULL));
    [board removeAllObjects];
    [players removeAllObjects];
    //setup board
    int x, y;
    for (y = 0 ; y < h ; y++)    {
        for (x = 0 ; x < w ; x++)    {
            Tile *tempTile = [[Tile alloc] init];
            [tempTile setX:x*32];
            [tempTile setY:y*32];
            [tempTile setProtein:0];
            [tempTile setOwner:0];
            if (rand() % (h*w) < (h*w)/3)    {
                [tempTile setProtein:1];
            }
            [board addObject:tempTile];
        }
    }
    //setup players
    for (x = 0 ; x < p ; x++)    {
        Player *newPlayer = [[Player  alloc] init];
        Ameba *firstAmeba = [[Ameba alloc] init];
        switch (x)    {
            case 0:
                [firstAmeba setTileOn:0];
                break;
            case 1:
                [firstAmeba setTileOn:w-1];
                break;
            case 2:
                [firstAmeba setTileOn:(w*h)-w];
                break;
            case 3:
                [firstAmeba setTileOn:(w*h-1)];
                break;
        }
        [firstAmeba setStrength:4];
        [firstAmeba setSpeed:3];
        [firstAmeba setVitality:5];
        [firstAmeba setArmor:0];
        [firstAmeba setOwner:x];
        [amebas addObject:firstAmeba];
        [newPlayer addAmeba:firstAmeba];
        [newPlayer setProtein:20];
        [players addObject:newPlayer];
    }
}


lots of stuff in there that has nothing to do with it its basicallly the last loop

the array amebas is the one that is giving me trouble.

ameba has a method called -owner which returns the player that owns it as long as i am in the above method all 4 amebas in the amebas array tell me correctly which player owns them but as soon as its a diffferent method the last one is 0 for everything

here is the method



simple but if i log right before it and get the ameba n's owner it is -1
Code:
- (id)amebaAt:(int)n
{
    NSLog(@"%d %d", n, [[amebas objectAtIndex:n] owner]);
    return [amebas objectAtIndex:n];
}


Quote:2005-09-02 13:16:06.645 Ameba Wars[911] 0 0
2005-09-02 13:16:06.646 Ameba Wars[911] 1 1
2005-09-02 13:16:06.649 Ameba Wars[911] 2 2
2005-09-02 13:16:06.650 Ameba Wars[911] 3 -1


yes amebas is init'd alloc'd and retain'd at -awakeFromNib
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
First thing that stikes out is
Code:
NSLog(@"%d %d", n, [[amebas objectAtIndex:n] owner]); // owner
    return [amebas objectAtIndex:n];                            // not  owner
So is whats being printed actually what the objects are.

I think id need to see the code for the amoeba class to really know what the problem is.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
That -1 should be a 3 and it does the same thing for all accessor methods of the last added object, owner was just an example, the other 3 objects are fine so it isnt the ameba class.

but here it is anyway

Code:
@implementation Ameba

- (void)setTileOn:(int)n
{
    tileOn = n;
}

- (int)tileOn
{
    return tileOn;
}

- (void)setStrength:(int)n
{
    strength = n;
}

- (int)strength
{
    return strength;
}


- (void)setSpeed:(int)n
{
    speed = n;
}

- (int)speed
{
    return speed;
}


- (void)setArmor:(int)n
{
    armor = n;
}

- (int)armor
{
    return armor;
}


- (void)setVitality:(int)n
{
    vitality = n;
}

- (int)vitality
{
    return vitality;
}


- (void)setOwner:(int)n
{
    owner = n;
}

- (int)owner
{
    return owner;
}


@end


its just a bunch of mutator/accessor methods
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
Ok sorry I dont really see anything wrong.
Is players 4 or 3?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #5
Players 0-2 work fine but the last one in the array (3) is all -1s even if i log it at the end of the mehtod where it is created all 4 are fine, so something is happening to it. But i have no idea what i could try adding another and seeing what it does. Ill do that.

Ok i made it just add an object and it did the same thign the last ogject was the messed up one 3 didnt have any problems. !!!

here is my awake from nib method

Code:
- (void)awakeFromNib
{
    players = [[NSMutableArray alloc] init];
    board = [[NSMutableArray alloc] init];
    amebas = [[NSMutableArray alloc] init];
    [board retain];
    [players retain];
    [amebas retain];
    [self setupH:10 W:20 players:4];
    [view setNeedsDisplay:YES];
}
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #6
Don't alloc and retain, that's causing a memory leak.

Do one of the following but not both:

[[NSArray array] retain]

OR

[[NSArray alloc] init]

"When you dream, there are no rules..."
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
here is my new code, does the exact same thing

Code:
- (void)awakeFromNib
{
    players = [[NSMutableArray alloc] init];
    board = [[NSMutableArray alloc] init];
    amebas = [[NSMutableArray alloc] init];
    [self setupH:10 W:20 players:4];
}


this gets the same thing also if i change it to

amebas = [[NSMutableArray array] retain];

it gets the same thing as well
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Some sort of guide to processors? ia3n_g 1 2,083 Sep 2, 2006 03:44 PM
Last Post: OneSadCookie
  memcpy(stuct array pointer struct array point) unknown 22 10,992 Sep 29, 2005 03:16 PM
Last Post: unknown