Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Verbal CA

macrumors newbie
Original poster
Hey all,

I'm pretty new to this iPhone coding gig so apologies if this is a bit of a newbie question!

So here is the problem:

I have 2 players - Player 1 and Player 2. At any one point I want one of them marked as 'Active'. Then I have two buttons - the first button 'point' gives one point to whichever player is 'active'. The other button 'endturn' swaps the active player and gives the new active player a point. To explain that better:
- Player 1 is active - he wins and so I press 'point' and Player 1 gets the point
- On the next round Player 2 wins so I press the 'endturn' button instead. That gives Player 2 a point and makes Player 2 active. Now if the 'point' button is pressed Player 2 should get the points.

So far I have defined the two buttons and two score labels, and I can get the buttons to score each player separately, but I can't seem to figure out how to declare a player as 'active' and therefore make the points button give points to them. Any thoughts? I'm guessing I need to define a variable called 'active' and use some kind of if statement to check if 'active' is set to player 1 or 2, and then get the points button to write to the appropriate score, and then define a separate function which swops the active variable to the other player - the problem is I'm not exactly sure how to do that!

Any help would be greatly appreciated!
 
Well if you are looking for instruction on how to define a variable like that:

Code:
int active=1; //Starts with player one being "active"

in your header (.h) file. Then, your class (.m) file may look like this:

Code:
-(IBAction)addpoints{
if(active==1){
//Player one is active...

}
else{
//Player 2 is active

}

}

-(IBAction)endturn{

if(active==1){
active=2; //Player 1 is active, so make player 2 active...
}
else{
active=1; //Player 2 is active, so make player 1 active...

}

Hope that helps!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.