13 Replies - 1525 Views - Last Post: 27 July 2011 - 10:51 AM

#1 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

help needed writing my pickupItem method in my code?

Posted 25 July 2011 - 08:11 PM

I writing this program where the player have to pick up items in it. I have the the drop item to work but i can't get the pick up item to work.
this is what i have
[outside dropItem:[[[Item alloc] initWithName:@"Bicycle"] autorelease]];
		
[outside pickupItem:<#(NSString *)theItem#>];


i just would like some help with this.
Is This A Good Question/Topic? 0
  • +

Replies To: help needed writing my pickupItem method in my code?

#2 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

Re: help needed writing my pickupItem method in my code?

Posted 25 July 2011 - 09:29 PM

i was tried this
[outside pickupItem:[initWithName: @"bag"]
but it is not working. it will build but it will not pick up the item.
Was This Post Helpful? 0
  • +
  • -

#3 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: help needed writing my pickupItem method in my code?

Posted 25 July 2011 - 10:38 PM

You realize that one function call lifted from its context is not much to go on? Could you post at least the entire function in which that call resides? Also, we have no idea how you've implemented your inventory system, so we can't tell you how to fix it.
Was This Post Helpful? 0
  • +
  • -

#4 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

Re: help needed writing my pickupItem method in my code?

Posted 25 July 2011 - 10:52 PM

i can't figure out how to write the inventory class. if you can i would like some ideas on how to write it
-(id)init
{
	return [self initWithDescription:@"No Description"];
}

-(id)initWithDescription:(NSString *)aDescription
{
	self = [super init];
	
	self.description = aDescription;
	exits = [[NamedObjectContainer alloc] initWithCapacity:10];
	exits.name = @"Exits";
	items = [[NamedObjectContainer alloc] initWithCapacity:10];
	items.name = @"Items";
	image = @"room.jpg";
	
	return self;
}

-(id)copyWithZone: (NSZone *) zone
{
	Room *newRoom = [[Room allocWithZone:zone] initWithDescription:self.description];
	
	newRoom.exits = exits;
	return newRoom;
}

-(void)setExit:(NSString *)exit:(Room *)theRoom
{
	[exits setObject:theRoom forKey:exit];
}

-(Room *)getExit:(NSString *)exit
{
	return [exits objectForKey:exit];
}

-(NSUInteger)hash
{
	return [description hash];
}

-(BOOL)isEqual:(id)object
{
	return [description isEqual:[object description]];
}

-(NSString *)name
{
	return description;
}

-(void)dropItem:(Item *)anItem
{
	[items setObject:anItem forKey:[anItem name]];
}

-(Item *)pickupItem:(NSString *)theItem
{
	return [[[Item alloc] init] autorelease];
}

-(void)dealloc
{
	[description release];
	[exits release];
	[items release];
	[image release];
	
	[super dealloc];
}

@end

Was This Post Helpful? 0
  • +
  • -

#5 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: help needed writing my pickupItem method in my code?

Posted 26 July 2011 - 11:41 AM

The easiest way to have an inventory would be to have an NSArray. It looks like you already have something like that. Is your NameObjectContainer a small wrapper around NSArray?
Looking at your code I can see that when someone picks up an item it's not removed from the Room. You'll probably have to set the corresponding index of your container to NIL unless you have a special sentinel value for empty.
Was This Post Helpful? 0
  • +
  • -

#6 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

Re: help needed writing my pickupItem method in my code?

Posted 26 July 2011 - 07:12 PM

So i can use my NameObjectContainer for inventory:
#import "NamedObjectContainer.h"


@implementation NamedObjectContainer

@synthesize name;

-(id)initWithCapacity:(NSUInteger)numItems
{
	theContainer = [[NSMutableDictionary alloc] initWithCapacity:numItems];
	sortedKeys = [[NSMutableArray alloc] initWithCapacity:numItems];
	
	return self;
}

-(void)setObject:(id)anObject forKey:(id)aKey
{
	[sortedKeys addObject:aKey];
	[theContainer setObject:anObject forKey:aKey];
}

-(id)objectForKey:(id)aKey
{
	return [theContainer objectForKey:aKey];
}

-(NSUInteger)count
{
	return [theContainer count];
}

-(NSArray *)allKeys
{
	return sortedKeys;
}

-(NSString *)keyForRow:(NSInteger)row
{
	return [sortedKeys objectAtIndex:row];
}



#pragma mark  -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *GameTableIdentifier = @"GameTableIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GameTableIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:GameTableIdentifier] autorelease];
	}
	
	NSUInteger row = [indexPath row];
	NSArray *theKeys = [self allKeys];
	[[cell textLabel] setText:[theKeys objectAtIndex:row]];
	id<NamedObjectProtocol> tempObject = [theContainer objectForKey:[theKeys objectAtIndex:row]];
	[[cell imageView] setImage:[UIImage imageNamed:[tempObject image]]];
	return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return name;
}



@end

Was This Post Helpful? 0
  • +
  • -

#7 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: help needed writing my pickupItem method in my code?

Posted 26 July 2011 - 07:32 PM

Also make sure that when an object is picked up you remove it from the room.
Was This Post Helpful? 0
  • +
  • -

#8 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

Re: help needed writing my pickupItem method in my code?

Posted 26 July 2011 - 07:51 PM

How could i remove it for the room?
Was This Post Helpful? 0
  • +
  • -

#9 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: help needed writing my pickupItem method in my code?

Posted 26 July 2011 - 08:38 PM

You have a Room class. This class has a NamedObjectContainer receiver for the items in the room. It also has a selector to handle picking up items in the room. When someone picks up an item in the room, you will have to remove it from your NamedObjectContainer.
Was This Post Helpful? 0
  • +
  • -

#10 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

Re: help needed writing my pickupItem method in my code?

Posted 26 July 2011 - 09:14 PM

View PostGWatt, on 26 July 2011 - 09:38 PM, said:

You have a Room class. This class has a NamedObjectContainer receiver for the items in the room. It also has a selector to handle picking up items in the room. When someone picks up an item in the room, you will have to remove it from your NamedObjectContainer.

i been trying to do what you been telling me but it not working. Can you show me an example of this.
Was This Post Helpful? 0
  • +
  • -

#11 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: help needed writing my pickupItem method in my code?

Posted 27 July 2011 - 07:22 AM

The NSMutableDictionary has access to the following selectors:
-(void) removeObjectForKey:(id)aKey;
-(void) removeObjectsForKeys:(NSArray *)keyArray;


Similarly a NSMutableArray has the following selector:
-(void) removeObject:(id)anObject;



You will need to change your code to use a NSMutableDictionary to use the remove selector. Assuming you do that you can make your pickupItem look something like this:
-(Item *) pickupItem:(NSString *) theItem {
    return [items removeItemByName: theItem];
}


and then add this to your NamedObjectContainer:
-(Item *) removeItemByName:(NSString *) name {
    Item *item = [theContainer objectForKey: name];
    [theContainer removeObjectforKey: name];
    [sortkeys removeObject: name];

    return item;
}


This post has been edited by GWatt: 27 July 2011 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#12 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

Re: help needed writing my pickupItem method in my code?

Posted 27 July 2011 - 09:59 AM

When i try to remove the items for the room the program will not even run.it will let me pick the items up but the item remain in the room. here is the code with the item
//Add items to the outside room
		[outside dropItem:[[[Item alloc] initWithName:@"Trees"] autorelease]];
		[outside dropItem:[[[Item alloc] initWithName:@"Bicycle"] autorelease]];
		[mbedroom dropItem:[[[Item alloc] initWithName:@"TV"] autorelease]];
		[mbedroom dropItem:[[[Item alloc] initWithName:@"shirts"] autorelease]];
		[bedroom2 dropItem:[[[Item alloc] initWithName:@"Bed"] autorelease]];
		[bedroom2 dropItem:[[[Item alloc] initWithName:@"Mirror"] autorelease]];
		[bathroom dropItem:[[[Item alloc] initWithName:@"bathtub"]autorelease]];
		[hallway1 dropItem:[[[Item alloc] initWithName:@"Picture"] autorelease]];
		[bedroom3 dropItem:[[[Item alloc] initWithName:@"trunk"] autorelease]];
		[hallway2 dropItem:[[[Item alloc] initWithName:@"Picture"] autorelease]];
		[bedroom3 dropItem:[[[Item alloc] initWithName:@"Dresser"] autorelease]];
		[kitchen dropItem:[[[Item alloc] initWithName:@"Fridge"]autorelease]];
		[livingroom dropItem:[[[Item alloc] initWithName:@"Chair"]autorelease]];
		[livingroom dropItem:[[[Item alloc] initWithName:@"TV"]autorelease]];
		[mbedroom pickupItem:@"shirts"];

Was This Post Helpful? 0
  • +
  • -

#13 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: help needed writing my pickupItem method in my code?

Posted 27 July 2011 - 10:10 AM

At this point you're going to need to use the debugger to see why objects are remaining in the room.
Was This Post Helpful? 0
  • +
  • -

#14 over5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-November 09

Re: help needed writing my pickupItem method in my code?

Posted 27 July 2011 - 10:51 AM

ok thanks for helping me get this far
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1