开发者

Is it possible to manually traverse nested if statements?

开发者 https://www.devze.com 2023-02-06 10:50 出处:网络
Binary trees are confusing me into inactivity, so I thought I\'d try a simpler (if significantly messier) approach.

Binary trees are confusing me into inactivity, so I thought I'd try a simpler (if significantly messier) approach.

For example...

if (a) {
    do something
    // wait for a button press before checking the next 'if'
    if (aa) {
        do something
    } else if (ab) {
        do something
    }
} else if (b) {
    do something else
}

Et cetera.

How do I force my app to wait for a button press before asking if 'aa' returns true? (And so on and so forth.)

Switches seem like a cleaner 开发者_StackOverflow中文版alternative, so if someone has an answer for that method instead, I'd be happy to give it a go. It's the same problem, though. I can't figure out how to progress step by step rather than all at once.


Here's a different approach, using a state or control variable for the view, to determine what it should do next given the button presses.

// pseudo-code based on your example
-(IBAction) buttonPress1
if (a) {
    do something
    self.setState = stateA;
} else if (b) {
    do something else
    self.setState = stateB;
}

-(IBAction) buttonPress2
 if (self.state == stateA) {
    if (a) {
        do something
    } else if (b) {
        do something
    }
 }

hope that helps, if not, ask away in the comments.


[EDIT]

OK, after you explained the DA/ME/ME2 reference I get what you're looking for.

What you're maybe going to want to do, is store your Dialogue in a plist, to load into a NSDictionary. (read up on NSDictionary)

Each NSDictionary should look like this:

key = value

@"prompt" = @"hi my name is bob" @"MEAN" = NSDictionary object for next convo choice for mean @"NICE" = NSDictionary object for next convo choice for nice

const BOOL NICE = YES;
const BOOL MEAN = !NICE;

NSDictionary *convo = //loaded to initial starting point from your plist file

- (IBAction) playerChoseMean:(id)sender
{
    [self sayConvo:convo withChoice:MEAN];
}

- (IBAction) playerChoseNice:(id)sender
{
    [self sayConvo:convo withChoice:NICE];
}

- (void) sayConvo:(NSDictionary)convo withChoice:(BOOL)b
{
  NSLog(@"NPC says: %@", [convo valueForKey:@"prompt"]);
  if(b) {
     convo = (NSDictionary*)[convo valueForKey:@"NICE"];
  } else {
     convo = (NSDictionary*)[convo valueForKey:@"MEAN"];
  }

  if (convo == [NSNull null] || convo == nil) then continue;
  //else continue
}

Here's an example NSDictionary graph which should get you started.

NSDictionary *intro = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nice = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nicenice = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nicegoodbye = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *mean = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *meangoodbye = [NSDictionary dictionaryWithCapacity:3];

[intro addValue:@"hi there!" forKey:prompt];
[intro addValue:nice forKey:@"NICE"];
[intro addValue:mean forKey:@"MEAN"];

[nice addValue:@"that was nice" forKey:prompt];
[nice addValue:nicenice forKey:@"NICE"];
[nice addValue:mean forKey:@"MEAN"];

[nicenice addValue:@"awww" forKey:prompt];
[nicenice addValue:nicegoodbye forKey:@"NICE"];
[nicenice addValue:mean forKey:@"MEAN"];

[nicegoodbye addValue:@"you were super nice, here's a bonus for being so nice" forKey:prompt];
[nicegoodbye addValue:[NSNull null] forKey:@"NICE"];
[nicegoodbye addValue:[NSNull null] forKey:@"MEAN"];

[mean addValue:@"that was mean" forKey:prompt];
[mean addValue:nice forKey:@"NICE"];
[mean addValue:meangoodbye forKey:@"MEAN"];

[meangoodbye addValue:@"you're a jerk!  goodbye!" forKey:prompt];
[meangoodbye addValue:[NSNull null] forKey:@"NICE"];
[meangoodbye addValue:[NSNull null] forKey:@"MEAN"];

Take a look at this excellent example over on GameDeve.SE


If the present function is in secondary thread then you can use

CFRunLoopRun() -- stop the thread then once button is pressed call CFRunLoopStop() to resume the operation.

I would prefer you to do same implementation bit differently: Brake the flow in to 2 parts

in first part ---

if (a) {
    do something

    }
} else if (b) {
    do something else
}

in second part do --- do this once button is clicked.

   if (aa) {
        do something
    } else if (ab) {
        do something
0

精彩评论

暂无评论...
验证码 换一张
取 消