Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I implement a simple logic state control in C#, by using a void function delagate. I would like to do the same thing in Objective C for my iPhone project, but I am too Newb to figure out what to do. I placed my pseudo-ObjC below. Maybe using an SEL selector maybe? I need a way to dynamically change the flow at runtime.

IN C# --
C#
public delegate void State();   // create anonymous delegate "void function()"

and a set of functions like
C#
    public void SetGameState(State newState)
    {
        //   lastState = currentState;  // not used yet..
        GameState -= currentState;  // unsubscribe current function
        currentState = newState;    // assign new function to the variable

        GameState += currentState;  // subscribe the new function

        gameSubState = GSS.GSS_0;   // apply initial game sub state.
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    SetGameState(DoState1);  // assign the 1st state to DoState1()
        masterGameTimer = new GameTimer();
        masterGameTimer.UpdateInterval = TimeSpan.FromTicks(333333);
        masterGameTimer.Update += OnUpdate;
        masterGameTimer.Start();
    }

    private void OnUpdate(object sender, GameTimerEventArgs e)
    {
        GameState(); // this redirects execution to the assigned state
    }

    private void DoState1()  // 1st state
    {
    SetGameState(DoState2);
}
    private void DoState2()  // 2nd state
    {
    SetGameState(DoState1);  // and back around we go!
}

This is just a guess at the Objective C, but I hope this looks like what I am trying to ask. Thanks.
C#
-(void)SetGameState:(SEL) newState  // assign a new state
{
    gameState=newState;
    gameSubState=GSS_0;
}

-(void)viewDidLoad
{
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.033f target:self
                  selector:@selector(Update:) userInfo:nil repeats:YES];
    [SetGameState DoState1];     // set an initial gameState 
}

-(void)DoState1: (id) sender
{    
    [SetGameState DoState2];  // first state,
}

-(void)DoState2: (id) sender
{    
    [SetGameState DoState1];  // then 2nd state, and back around
}

-(void)Update: (id) sender
{
    [self.gameState]; // this won't work, but the idea would be to execute the assigned state. and programatically change it.
}


Mucho gracias.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900