I’ve just whittled out loads of erros and now my code compiles fine, however it always crashes in the createEntity method i made. there is seemingly nothing wrong with it, but could someone help me anyway? Anything to suggest?
-(void)createEntityWithX:(int)newEntityX andY:(int)newEntityY withType:(int)newEntityType withWidth:(int)newEntityWidth andLength:(int)newEntityLength atSpeed:(int)newEntitySpeed
{
Entity tmpEntity;
tmpEntity.entityX = newEntityX;
tmpEntity.entityY = newEntityY;
tmpEntity.entityLength = newEntityLength;
tmpEntity.entityWidth = newEntityWidth;
tmpEntity.entityType = newEntityType;
tmpEntity.entitySpeed = newEntitySpeed;
int arrayAmount = [entityArray count];
NSValue *tmp = [NSValue valueWithBytes:&tmpEntity objCType:@encode(struct Entity)];
[entityArray insertObject:tmp atIndex:arrayAmount];
[tmp release];
}
One or both of two things is wrong here. It’s possible that
entityArrayis not anNSMutableArray, but anNSArray, in which case you’re getting an “unrecognized selector” error.After that’s fixed, you need to fix your indexing. You can’t insert an object into an
NSMutableArrayat the index corresponding to itscount, because the last valid index for insertion iscount-1. You should useaddObject:to put something at the end of the array: