lsiunsuex
lsiunsuex

Reputation: 480

How to pass variable to other method?

I know this is a very beginner question, but I've been struggling to figure it out.

I'm trying to build an iOS game (using cocos2d) and so I have 2 sets of files

GameScene.h and gameScene.m MainMenu1.h and MainMenu1.m

GameScene has the sharedcode I've leaned to put in.

I call my MainMenu1 - the user chooses how many players from a MenuItemwithImage and that calls ChoosePlayers

I can figure out which menu item was touched, but I need to pass the number of players back to GameScene

in GameScene I put in

-(void) setPlayers (nsinteger*) players
{
totalplayers = players;
}

so in mainmenu1 chooseplayers i did

[[GameScene SharedGameData] setPlayers : 2];

but that doesn't work.

I'm sorry, I don't have the code in front of me (not until tonight); i've been searching for hours and can't figure it out.

Upvotes: 0

Views: 97

Answers (2)

Snowman
Snowman

Reputation: 32061

Your method format is incorrect. It should be:

-(void)setPlayers:(NSInteger)players;

NSInteger is not a pointer either.

To pass multiplie values, you coud either pass in an array, or:

-(void)setPlayers:(NSInteger)firstValue withSecondValue:(NSInteger)secondValue; and when you want to call it, it would look like this:

[[GameScene SharedGameData] setPlayers:2 withSecondValue:4];

Upvotes: 3

Justin Thompson
Justin Thompson

Reputation: 1

Hi it would be good if you could post some more detail about the errors you are receiving (if any?), this would helpt to diagnose the problem but looking at you code it i think that you need to change the

-(void) setPlayers (nsinteger*) players

to -(void) setPlayers :(nsinteger*) players

hope this helps!

Upvotes: 0

Related Questions