Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6228917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:28:19+00:00 2026-05-24T09:28:19+00:00

I am working on a game and in my init method it gives EXC_BAD_ACCESS

  • 0

I am working on a game and in my init method it gives EXC_BAD_ACCESS when I do [self loadGravityCenters]. Here is the definition of that method:

-(void) loadGravityCenters {

    //These P1, P2, etc. are for seeing where the EXC_BAD_ACCESS is in the method.
    printf("P1\n");
    //Get and open the file with coordinates
    NSString *filePath =  [[NSBundle mainBundle] pathForResource: @"gravitycenter" ofType: @"ballpoint"];
    FILE *fileHandle = fopen([filePath cStringUsingEncoding:NSASCIIStringEncoding],"r");
    //First array for storing the coordinates from the file.
    NSMutableArray *coord;
    //Vanilla C doesn't have an NSMutableArray, use tempCoord to place it in the NSMutableArray.
    int tempCoord;
    //i is used to keep track of where to assign stuff in coord).
    int i = 0;
    while (!feof(fileHandle)) { //Read file.
        //Next check point.
        printf("P2\n");
        //Assign tempCoord a number from file.
        fscanf(fileHandle, "%d", &tempCoord);
        //Convert the integer to NSNumber (so I can put it in coord.
        NSNumber *intToNSNumber = [NSNumber numberWithInt:tempCoord];
        [coord insertObject:intToNSNumber atIndex:i];
        i++;
    }
    for (int j = 0; j < [coord count]; j+=2) { //Make the coord array into an array of CGPoints
        printf("P3\n"); //Check point.
        //Assign a gravityCenters (declared in header) a series of points.
        [gravityCenters addObject:[NSValue valueWithCGPoint:CGPointMake([[coord objectAtIndex:j] floatValue], [[coord objectAtIndex:j+1] floatValue])]];
    }

    for (int j = 0; j < [gravityCenters count]; j++) {
        printf("P4\n"); //Last checkpoint.
        //gravityWells (declared in header) is used to store CCSprites, make a bunch of sprites with the positions stored in gravityCenters.
        [gravityWells addObject:[CCSprite spriteWithFile:@"GravityCenter.png"]];
        [[gravityWells objectAtIndex:j] setPosition:[[gravityCenters objectAtIndex:j] CGPointValue]];
        printf("gravityWell=%f,%f\n", ((CCSprite *)[gravityWells objectAtIndex:j]).position.x, ((CCSprite *)[gravityWells objectAtIndex:j]).position.y);
        [self addChild:[gravityWells objectAtIndex:j]];
    }
}

To summarize:

Make an array of intgers that stores coordinates. Convert it so it can be placed in an array of CGPoints. Make a bunch of CCSprites and give them the coordinates from the array filled with CGPoints.

The file I am using looks like this:

70 100
80 89
46 545

The program fails at checkpoint P3. I’ve tried replacing the line:

    for (int j = 0; j < [gravityCenters count]; j++) {

With:

    for (int j = 0; j < [coord count]-2; j+=2) {
    //And:
    for (int j = 0; j < [coord count]-3; j+=2) {

Wondering it is only an out-of-bounds thing. But doing what I did above doesn’t seem to change anything. Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:28:20+00:00Added an answer on May 24, 2026 at 9:28 am

    You need to allocate the array first before you can use it. Try:

    NSMutableArray *coord = [[NSMutableArray alloc] init];
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a game that uses local area network. Like most of multiplayer
Basic summary: Working on a game that generates a 15x15 grid for the player
I'm working on a game that allows players to click on a card and
I am working on a game that will have a two dimensional board of
I'm working on a C project that consists of a simple game using the
I'm working on an iphone game that I plan to submit to the app
I've been trying to get this Sudoku game working, and I am still failing
I'm working on a game engine in C++ and I have methods like setColour
I am working on a game for iOS and I have to schedule an
I'm working on a game where multiple players should be able to play at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.