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 8282791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:29:54+00:00 2026-06-08T10:29:54+00:00

I am working on an rpg type game for the iphone. What I want

  • 0

I am working on an rpg type game for the iphone. What I want to do is have an inventory that will only allow you to hold a specific amount of only a specific type of items. I think to do this I’m just going to extend the NSMutableArray and add limits to it. I can’t figure out the best way to do this though. Here’s the idea I have in my head…

Backpack.h

@interface Backpack : NSMutableArray {
  Class * arrayClass;
  NSMutableArray * array;
  int limit;
}

-(id) initWithClass:(Class) type andLimit:(int) num;

@end

Backpack.m

@implementation Backpack

  -(id)initWithClass:(Class) type andLimit:(int) num {

    arrayClass = type;
    limit = num;
    array = [NSMutableArray new];
    return self;

  }

  -(void)insertObject:(id) object atIndex:(int) index {

    if([object isKindOfClass:arrayClass] && index < limit) {
      // Insert it
    } else {
      // Throw Exception
    }

  }

@end
  • 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-08T10:29:56+00:00Added an answer on June 8, 2026 at 10:29 am

    Rather than extend NSMutableArray, just create your own class that uses an instance of NSMutableArray to hold items. Your class can have an add method or methods that require items being added to be of a certain class type (or which implement a specific interface). As far as limiting the number of items, in side your addItem method, first check the number of items being held. If it is greater than your limit, then don’t allow the item to be added. Finally, your class can expose a size method (e.g. getSize()) which tells you how many items are already being held internally in the instance of NSMutableArray, that way you can prevent items form being added when your backpack is “full”.

    Generally speaking, composition (i.e. composing your own classes from member instances of other classes) is better than extending classes and using inheritance to accomplish the same behavior. If you extend NSMutableArray, for example, then the resulting class is going to expose all of the public methods of that class, in addition to your methods, which leads to a messy class from a code reuse and documentation standpoint.

    Finally, your question really isn’t about Java so perhaps this should not be tagged with the Java tag.

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

Sidebar

Related Questions

Working on an RPG type flash, and I have to be able to control
Hi I'm currently working on an iPhone game, top-down Strategy RPG (kinda like Fire
Working on game where plates will be falling from top to bottom. Some plates
Working with a Lucene index, I have a standard document format that looks something
I'm working on an RPG-style game in XNA and I'm working on implementing a
I have been working on creating an RPG combat sim for code-practice. A single
I'm working on the website for a mmo-rpg game-based group, and I'm having trouble
I'm working on a Java RPG. And now I want to play background music
I'm working on an kind of an RPG game. And I'm trying to figure
Working with Sharepoint 2010, I have a class that inherits SPPersistedObject with various settings:

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.