I am trying to call a method that checks whether the player is bigger or smaller than the enemy. (this is a fish game)
In EnemyFish.m I am using this method
-(void) compareSize:(Player*)player{
if (self.fSize > player.pSize){
isBigger = true;
}
else{
isBigger = false;
}
}
Then I want to call this method during the update so I am doing this:
-(void) update {
[self compareSize];
//Check to see if bigger than player fish
if( isBigger == true){
//code for if bigger
}else{ //etc. }
I am getting an exception: sharedlibrary apply-load-rules all
not sure what the best way to set up this method would be, and the best way to call it, since [self compareSize] is definately not working.
Any help would be greatly appreciated, Thanks!
——UPDATE———-
What about if I use this
update:(Player *)player{
The problem I was running into here, is how to call this correctly, I wasn’t sure how to change this to correctly call the new update method:
[self schedule:@selector(update) interval:1.0f/60.0f];
It is unclear what you are asking, but let’s look at your code and see if it helps.
Your first method can be written more concisely as:
There is no point in using an
if/elseto assign atrue/false(orYES/NO) value.Looking at this method raises the obvious question of whether it would be better returning a value rather than assigning to an instance variable. This would look like:
and now you can use a call to
compareSizein anif.Assuming the second version of
compareSizeyour second method is:But this doesn’t work as you need an instance of
Playerto pass tocompareSize:, e.g.[self compareSize:somePlayerInstance]. So you now have to ask yourself where you expect thePlayerto be found; it could be an argument toupdate(e.g.- (void) update:(Player *)somePlayerInstance), or you might have a method to call which returns a whole collection of players and you need to test against each one, etc., etc. I can’t give an answer as I’ve no idea of your game and algorithm!Following comment
You must store a reference to your
Playerobject somewhere in your application. If there is only a single player isPlayerdesigned as a singleton with asharedInstance, or similarly named, class method that returns the single instance? If so then yourupdatewill contain:etc.
Another design pattern is to have your application delegate store the reference and to provide a method (or property) for accessing it. Following this pattern (and making up a class
MyDelegateAppand propertyplayernames) your code might look like:Yet another model is to create the single player in the application’s main XIB/NIB file – etc., etc., there are many application models!
You “simply” (its not simple of course) need to design your application model so that your single player is accessible, one way or another, where you need it…