DuskFall
DuskFall

Reputation: 422

Where is the source of segmentation fault : 11?

Please help, this is one of 3 faults i have in here, and i simply cannot find where they are : I know they are to do with my struct but i have no idea how to find them... Here is the first section of code with a fault in :

-(BOOL)detectCollisionBetweenEntityAtIndex:(int)index1 andEntityAtIndex:(int)index2
{
  if ((([entityArray objectAtIndex:index1].entityX >= [entityArray objectAtIndex:index2].entityX) && ([entityArray objectAtIndex:index1].entityX <= ([entityArray objectAtIndex:index2].entityX + [entityArray objectAtIndex:index2].entityWidth)) && ([entityArray objectAtIndex:index1].entityY >= [entityArray objectAtIndex:index2].entityY) && ([entityArray objectAtIndex:index1].entityY <= ([entityArray objectAtIndex:index2].entityY + [entityArray objectAtIndex:index2].entityLength))) || (([entityArray objectAtIndex:index1].entityX + [entityArray objectAtIndex:index1].entityWidth >= [entityArray objectAtIndex:index2].entityX) && ([entityArray objectAtIndex:index1].entityX + [entityArray objectAtIndex:index1].entityWidth <= ([entityArray objectAtIndex:index2].entityX + [entityArray objectAtIndex:index2].entityWidth)) && ([entityArray objectAtIndex:index1].entityY + [entityArray objectAtIndex:index1].entityLength  >= [entityArray objectAtIndex:index2].entityY) && ([entityArray objectAtIndex:index1].entityY + [entityArray objectAtIndex:index1].entityLength <= ([entityArray objectAtIndex:index2].entityY+ [entityArray objectAtIndex:index2].entityLength))))
  {
    return TRUE;
  }
  else
  {
    return FALSE;
  }
}

entityArray is an NSMutable array populated with instances of a struct i made :

struct Entity
{
  int entityX;
  int entityY;
  int entityLength;
  int entityWidth;
  int entityType;
  int entitySpeed;
  bool isDead;
};

Upvotes: 1

Views: 793

Answers (2)

Ben Allen
Ben Allen

Reputation: 69

Something along the lines of:

    -(BOOL)detectCollisionBetweenEntityAtIndex:(int)index1 andEntityAtIndex:(int)index2
    {
    bool hasDetected = false;
      if (...)
      {
        hasDetected = TRUE;
      }
        else
      {
        hasDetected = FALSE;
      }
        return hasDetected ;
      }

Should sort it out!

Upvotes: 1

user1203803
user1203803

Reputation:

An NSMutableArray can only store Objective-C objects, not C structs. Wrap the entities in an NSValue object to store them in the array:

Entity entity;
// populate entity...

NSValue *value = [NSValue valueWithBytes:&entity objCType:@encode(struct Entity)];
[array addObject:value];

To get the value back, use -[NSValue getValue:], like this:

NSValue *value = [array objectAtIndex:index];
Entity entity;
[value getValue:&entity];

Upvotes: 3

Related Questions