I’m new to Objective-C and C in general. I’ve been looking around and I couldn’t find the solution to this issue. Any help would be appreciated.
I have the following global variables
CCSprite* BackgroundImage;
CCSprite* BackgroundGhost;
CCSprite* Globe;
CCSprite* Logo;
in my init I call a function and pass the global variables as parameters.
if(_ourDevice == iPad)
{
[self CustomCodeSetAssetForIpad:BackgroundImage ghost:BackgroundGhost TheGlobe:Globe AndTheLogo:Logo];
}
Here is the code for CustomCodeSetAssetForIpad:
-(void) CustomCodeSetAssetForIpad:(CCSprite*) _Background ghost:(CCSprite*) _BackgroundGhosts TheGlobe:(CCSprite*)_Globes AndTheLogo:(CCSprite*) _Logos
{
_Background = [CCSprite spriteWithFile:@"1028-768-sunray.png"];
_BackgroundGhosts = [CCSprite spriteWithFile:@"1028-768-sunray.png"];
_Globes = [CCSprite spriteWithFile:@"BigGlobe.png"];
_Logos = [CCSprite spriteWithFile:@"DefaultLogo.png"];
[_BackgroundGhosts setAnchorPoint:CGPointMake(0.5, 0)];
[_BackgroundGhosts setScale:2];
[_BackgroundGhosts setOpacity:120];
//[BackgroundGhost setPosition: CGPointMake(BackgroundGhost.position.x, BackgroundGhost.position.y-500)];
[_BackgroundGhosts setPosition:CGPointMake([[CCDirector sharedDirector]winSize].width/2, -100)];
[_Globes setAnchorPoint:CGPointMake(0.5, 0.5)];
[_Globes setScale:0.7];
[_Globes setPosition:CGPointMake([[CCDirector sharedDirector]winSize].width/2, -260)];
[_Logos setPosition:CGPointMake([self CenterOfTheScreen].x, [[CCDirector sharedDirector]winSize].height-[[CCDirector sharedDirector]winSize].height*0.2)];
[_Logos setScale:0.05];
}
The first few lines instantiate the global variables that were passed. However when the function is done, the reference to those objects are lost. I thought when you pass a pointer to a function, as the object is instantiated, it would retain its reference to the instantiated object. Am I missing something here?
Ah… Variables of type
classname *are effectively references to instances of that class. So in your case,_Backgroundis an instance reference passed in as an argument to your function. If you are trying to return multiple results from a function (via pointers), your arguments should really of typeclassname **, which is a pointer to a reference.So the calling code would look like this:
[ self customCodeSetAssetForIpad:&background ghosts:&ghosts globes:&globes logos:&logos ] ;And your method looks like this:
Also, I took the liberty of making your method name and variable names more objective-c like (methods and variables begin with lowercase letters)
EDIT: I’d personally structure it like this: