0

一般来说,我是 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];


}

前几行实例化了传递的全局变量。但是,当函数完成后,对这些对象的引用就会丢失。我认为当您将指针传递给函数时,当对象被实例化时,它将保留其对实例化对象的引用。我在这里错过了什么吗?

4

1 回答 1

1

啊...类型变量classname *实际上是对该类实例的引用。因此,在您的情况下,是作为函数参数_Background传入的实例引用如果你试图从一个函数返回多个结果(通过指针),你的参数应该真的是 type classname **,它是一个指向引用的指针。

所以调用代码看起来像这样:

CCSprite * background = nil ;
CCSprite * ghosts = nil ;
CCSprite * globes = nil ;
CCSprite * logos = nil ;

[ self customCodeSetAssetForIpad:&background ghosts:&ghosts globes:&globes logos:&logos ] ;

您的方法如下所示:

-(void)customCodeSetAssetForIPad:(CCSprite**)background ghosts:(CCSprite**)backgroundhosts globe:globes logos:(CCSprite**)logos
{
    *background = [CCSprite spriteWithFile:@"1028-768-sunray.png"];
    // ... the rest of your code ...
}

另外,我冒昧地使您的方法名称和变量名称更像objective-c(方法和变量以小写字母开头)

编辑:我个人会这样构造它:

//
// World... global things go in here
//

@interface World

@property ( nonatomic, readonly, strong ) CCSprite * background ;

+(id)theWorld // accessor to get the global world object

@end

@implementation World
@synthesize CCSprite * background = _background ;

static World * __theWorld = nil ; // global variable to hold our shared global World instance

+(void)load
{
    // when this class is loaded, create our global world object
    __theWorld = [ [ [ self class ] alloc ] init ] ;
}

+(id)theWorld
{
    return __theWorld ;
}

// return the background sprite, creating it if it hasn't be created yet
-(CCSprite*)background
{
    if ( !_background) { _background = [ CCSprite spriteWithFile:[CCSprite spriteWithFile:@"1028-768-sunray.png"] ; }
    return _background ;
}

@end
于 2012-08-23T06:50:23.370 回答