views:

314

answers:

2

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;

}
+3  A: 

You would write it like this for one param

// definition
-(void) explosion:(EnemySprite*) someObject
{

}

// usage
[self explosion: object];

For two params things are a bit more involved. Consider;

// definition
-(void) explosion:(EnemySprite*) someObject radius:(float)explosionRadius
{
  ...
  if (pos < explosionRadius)
  ...
}

// 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.

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];
Andrew Grant
A: 

Why not make your object the receiver of the explosion?

for (id currentenemy in enemies)
{
    [currentenemy explode];
}
mouviciel