Method in Objective-C that points to an object


Method in Objective-C that points to an object



I have created a few sprites using a spriteclass and I have loaded them into an array. In my app, I loop over the array checking for particular conditions (position, etc.). I want to create an explosion method that I can pass one of these objects to and then using the pointer pull the position of the object on the screen and show an explosion there. I don't know how to pass the pointer/object that is in my array to the method to be used.

Here is essentially what I had in mind:

for (int i=1; i<4; i++) {      EnemySprite *currentenemy = [enemies objectAtIndex:i-1];      //Blow this guy up     [self explosion:currentenemy] }   -(void)explosion someobject {      explosion.position = someobject.position      someobject.setHidden=YES;  } 



How does a Protocol on an instance variable in Objective-C work?

1:



NSMutableArray as @property with readonly
You would write it like this for one param.
Is Objective-C on Linux garbage collected?
// definition -(void) explosion:(EnemySprite*) someObject {  }  // usage [self explosion: object]; 
For two params things are a bit more involved.


Weird leak when using [NSStringObject UTF8String]
Consider;.
Does [NSMutableDictionary setValue: value forKey: key] retain NSString key?
// definition -(void) explosion:(EnemySprite*) someObject radius:(float)explosionRadius {   ... 


What does the 'k' prefix indicate in Apple's APIs? [duplicate]
if (pos < explosionRadius) ...


Object retain behavior of Objective-C class methods
} // usage [self explosion: object radius:10.0f];
Everything before the : is used for the external name, everything after is the name internal to the function..
What causes fluctuating execution times when presenting the Renderbuffer? (OpenGL)
This is why you will often see Objective-C functions written with function names that end with the name of the first type:.
-(void) explodeSprite:(EnemySprite*) sprite radius:(float)radius; 
Both the sprite and radius params then appear to be "named" when the function is written;.
[self explodeSprite:sprite radius:10.0f]; 


2:


Why not make your object the receiver of the explosion?.
for (id currentenemy in enemies) {     [currentenemy explode]; } 



76 out of 100 based on 56 user ratings 1006 reviews