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

  • Home
  • SEARCH
  • 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 8325661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:30:50+00:00 2026-06-09T00:30:50+00:00

I have a snake game like player and I’m running into some problems when

  • 0

I have a “snake game” like player and I’m running into some problems when removing the end tail piece when the body reaches a certain length. Right now I have this:

if (i > maxHealth) {
    id object = [tail objectAtIndex:i-maxHealth-1];
    [tail removeObject:object];//This part doesn't work, if I comment this line out it works but the object is still in the array 
    [self.tileMap removeChild:object cleanup:YES];
}

Where i is the length of the “snake” when the scene is init i is equal to 0 and the player moves it adds the “tail” image to the scene and to a NSMutableArray called tail. I get this in the debugger:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 4 beyond bounds [0 .. 2]'

Which doesn’t make any sense to me because before I added the line to remove the sprite from the array and just use object to remove the sprite from the scene it worked fine.

  • 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-06-09T00:30:51+00:00Added an answer on June 9, 2026 at 12:30 am

    You should learn how to work with breakpoints to identify the right place in your code, because clearly, your problem is not the line you think it is:

    [__NSArrayM insertObject:atIndex:]: index 4 beyond bounds [0 .. 2]’

    It basically tells you all there is to know: you are trying to insert an object at index 4 of an array that only has three indices (0,1,2).
    So, your call to removeObject is not the reason for the exception you get, but the cause for this exception, happening later, when you try to insert an Object.

    I guess the index is wrong, apparently, because you removed your object at index 3 or 4 in the step before but did not update the valid indices.

    To fix these kinds of problems, I recommend to go to the Breakpoints Navigator in Xcode (cmd+6) and click the small bottem left “+” and “Add Exception Breakpoint” set to “all”. Run your code and the debugger will pop up right where the exception happens.

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

Sidebar

Related Questions

I'm making a snake game like player and I have the body moving fine
Some of you may have heard of the game called Screen Snake (video here
I have some homework for my school and I have to make a snake
i am trying to figure our how to make a snake like tail for
I have made a snake game using javascript and HTML5 Canvas element. It can
I am newbie on Unity 3d.I have started practising with a snake game. I
Found some nice Open Source code of a snake game, but wanting to make
I have implemented game application.In which i want to animate some area of images
In my snake game I have the game in one movie clip and a
I'm trying to learn Python, (i have 2.5.4) by writing a snake game, but

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.