3 Replies - 1690 Views - Last Post: 02 August 2011 - 10:39 AM

#1 over5  Icon User is offline

  • New D.I.C Head

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

I am trying to get my back command to work.

Posted 01 August 2011 - 09:53 AM

I am writing this program where the user can go in different rooms, but i can't get the back command to work. it suppose to take the user back to a different room, but it just closes the program.I get this warning that says the room may not work to back.

#import "PlayerViewController.h"


@implementation PlayerViewController
@synthesize description;
@synthesize commands;
@synthesize subjects;
@synthesize availableCommands;
@synthesize player;
@synthesize currentCommand;

#pragma mark TableView Delegate Methods

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	return indexPath;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (tableView == commands) {
		NSString *commandName = [availableCommands keyForRow:[indexPath row]];
		self.currentCommand = [availableCommands objectForKey:commandName];
		
		if ([[currentCommand name] isEqualToString:@"Go"]) {
			[subjects setDataSource:[[player currentRoom] exits]];
		}
		if ([[currentCommand name] isEqualToString:@"Look"]) {
			composite = [[CompositeContainer alloc] init];
			[composite addContainer:[[player currentRoom] exits]];
			[composite addContainer:[[player currentRoom] items]];
			[subjects setDataSource:composite];
		}
		if ([[currentCommand name] isEqualToString:@"Back"]) {
			[subjects setDataSource:[[player currentRoom] back]];
		}
		if ([[currentCommand name] isEqualToString:@"Quit"]) {
			[subjects setDataSource:[[player getEntrance] exits]];
		}
		if ([[currentCommand name] isEqualToString:@"Pickup"]) {
			[subjects setDataSource:[[player currentRoom] items]];
		}
		if ([[currentCommand name] isEqualToString:@"Drop"]) {
			[subjects setDataSource:[[player currentRoom] items]];
		}
		[subjects reloadData];
	}
	if (tableView == subjects) {
		NSString *exit = [[[player currentRoom] exits] keyForRow: [indexPath row]];
		[currentCommand setSecondWord:exit];
		[currentCommand execute:player];
		[description setText:[[self.player currentRoom] description]];
		[subjects setDataSource:nil];
		[subjects reloadData];
	}
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#import "Player.h"


@implementation Player

@synthesize currentRoom;

-(id)init
{
	[self initWithRoom:[[NSNull alloc] init]];
	
	return self;
}

-(id)initWithRoom:(Room *)start
{
	[super init];
	
	self.currentRoom = start;
	
	return self;
}

-(void)walk:(NSString *)exit
{
	self.currentRoom = [currentRoom getExit:exit];
	
}

-(void)back:(NSString *)back
{
	self.currentRoom = [currentRoom getExit:exit - 1];
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: I am trying to get my back command to work.

#2 GWatt  Icon User is offline

  • member icon

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

Re: I am trying to get my back command to work.

Posted 02 August 2011 - 06:26 AM

Isn't exit a NSString? In your back function you subtract 1 from it, which doesn't make since. I think what you meant to do is something like this:
-(void)back:(NSString *)back
{
    self.currentRoom = [currentRoom getExit:exit] - 1;
     
}


Was This Post Helpful? 0
  • +
  • -

#3 over5  Icon User is offline

  • New D.I.C Head

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

Re: I am trying to get my back command to work.

Posted 02 August 2011 - 09:39 AM

I been trying different ways to write the back command, but every time i try using it it just shut the program down.the warning says if room may not respond to back. but it build .this is the part of the code that gets the warning.
([[currentCommand name] isEqualToString:@"Back"]) {
			[subjects setDataSource:[[player currentRoom] back]];
		}

Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

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

Re: I am trying to get my back command to work.

Posted 02 August 2011 - 10:39 AM

The easiest way to go back would most likely be to store a stack of rooms. You can get a NSArray to act as a stack. When the user selects 'back' you would just pop the last element off the stack and set the current room to that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1