一般来说,我是 Objective-C 和 C 的新手。我一直在环顾四周,找不到解决此问题的方法。任何帮助,将不胜感激。
我有以下全局变量
CCSprite* BackgroundImage;
CCSprite* BackgroundGhost;
CCSprite* Globe;
CCSprite* Logo;
在我的初始化中,我调用一个函数并将全局变量作为参数传递。
if(_ourDevice == iPad)
{
[self CustomCodeSetAssetForIpad:BackgroundImage ghost:BackgroundGhost TheGlobe:Globe AndTheLogo:Logo];
}
这是 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];
}
前几行实例化了传递的全局变量。但是,当函数完成后,对这些对象的引用就会丢失。我认为当您将指针传递给函数时,当对象被实例化时,它将保留其对实例化对象的引用。我在这里错过了什么吗?