奇怪的行为 - 访问外部实例方法

发布于 2024-11-02 17:49:08 字数 1492 浏览 1 评论 0原文

嘿,又来了,
基本上,我有两个课程: HudLayer 和ConstructLayer。

我想从 ConstructLayer 访问 HudLayer 内部的方法,以关闭/打开 HudLayer 内部分配的 CCSprites 的可见性属性。

HudLayer 接口和实现:

HudLayer : CCLayer
@interface{
CCSprite *leftArrow;
CCSprite *rightArrow;
}
-(void)switcher:(BOOL)isVisible;
@end

@implementation
-(id)init{
//Create the Hud Sprites and add them at an arbitrary location

     leftArrow = [[[CCSprite alloc]init]retain];
     leftArrow = [CCSprite imageWithFile:@"file.png"];
     rightArrow = [[[CCSprite alloc]init]retain];
     rightArrow = [CCSprite imageWithFile:@"file.png"];

     leftArrow.visible = NO;
     rightArrow.visible = NO;

     [self addChild: leftArrow];
     [self addChild: rightArrow];
}

-(void)switcher:(BOOL)isVisible{
 NSLog (@"Accessed the visibility switcher");
    if (isVisible == NO){
         leftArrow.visible = NO;
         rightArrow.visible = NO;
    }

    if (isVisible == YES){
         leftArrow.visible = YES;
          rightArrow.visible = YES;
    }

@end

构造层实现:

#import "HudLayer"  
@implementation ConstructLayer

-(void)someFunction{

  //Attempt to change the visibility of leftArrow and rightArrow

    HudLayer *hud = [[HudLayer alloc]init];
    [hud switcher: NO];
    [hud release];

}

这应该可行,不是吗?但事实并非如此!
我访问 [hud switcher:] 方法,但由于某种原因,它无法正确设置属性 CCSprite.visibility。
我放置了一个 NSLog 语句,该语句在我的控制台中打印,证明它正在访问它。 真的很奇怪,我不知道这是怎么回事。 我什至在这个函数中定义了变量并用 NSLog 打印它们,它起作用了......

Hey again,
Basically, I have two Classes:
HudLayer and ConstructLayer.

I want to access a method inside HudLayer from ConstructLayer, to switch off/on the visibility properties of CCSprites allocated inside the HudLayer.

HudLayer Interface and Implementation:

HudLayer : CCLayer
@interface{
CCSprite *leftArrow;
CCSprite *rightArrow;
}
-(void)switcher:(BOOL)isVisible;
@end

@implementation
-(id)init{
//Create the Hud Sprites and add them at an arbitrary location

     leftArrow = [[[CCSprite alloc]init]retain];
     leftArrow = [CCSprite imageWithFile:@"file.png"];
     rightArrow = [[[CCSprite alloc]init]retain];
     rightArrow = [CCSprite imageWithFile:@"file.png"];

     leftArrow.visible = NO;
     rightArrow.visible = NO;

     [self addChild: leftArrow];
     [self addChild: rightArrow];
}

-(void)switcher:(BOOL)isVisible{
 NSLog (@"Accessed the visibility switcher");
    if (isVisible == NO){
         leftArrow.visible = NO;
         rightArrow.visible = NO;
    }

    if (isVisible == YES){
         leftArrow.visible = YES;
          rightArrow.visible = YES;
    }

@end

Construct Layer Implementation:

#import "HudLayer"  
@implementation ConstructLayer

-(void)someFunction{

  //Attempt to change the visibility of leftArrow and rightArrow

    HudLayer *hud = [[HudLayer alloc]init];
    [hud switcher: NO];
    [hud release];

}

This should work shouldnt it? But it doesn't!
I access the [hud switcher:] method but for some reason it wont set the property CCSprite.visibility correctly.
I put an NSLog statement which prints in my console, proving that it is accessing it.
Its really weird, I dont know whats going on with it.
I even defined variables inside this function and printed them with NSLog and it worked...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2024-11-09 17:49:08

这只是一个猜测,因为我没有使用 cocos2d 的经验,但无论如何......

我认为这部分泄漏得很严重。您分配(保留计数= 1),然后保留(保留计数= 2),然后完全忽略该对象并将您的var指向另一个对象。

 leftArrow = [[[CCSprite alloc]init]retain];
 leftArrow = [CCSprite imageWithFile:@"file.png"];
 rightArrow = [[[CCSprite alloc]init]retain];
 rightArrow = [CCSprite imageWithFile:@"file.png"];

我猜你没有看到精灵消失。由于您上面所做的操作,您实际上可能正确设置了可见属性,但由于您创建了两个 leftArrow 和两个 rightArrow 对象,因此您可能只会看到另一个(已分配、初始化和分配的对象) keep'ed,它没有指向它的指针)。

另外, imageWithFile 可能会返回一个自动释放的对象,您应该研究一下。

编辑(对于下面我的第二条评论):

替换

 leftArrow = [[[CCSprite alloc]init]retain];
 leftArrow = [CCSprite imageWithFile:@"file.png"];
 rightArrow = [[[CCSprite alloc]init]retain];
 rightArrow = [CCSprite imageWithFile:@"file.png"];

 leftArrow = [[CCSprite imageWithFile:@"file.png"] retain];
 rightArrow = [[CCSprite imageWithFile:@"file.png"] retain];

This is just a guess since I have no experience with cocos2d, but anyway...

I think this part is leaking real bad. You alloc'ed (retain count = 1), then retain'ed (retain count = 2), then totally disregarded that object and pointed your var to another.

 leftArrow = [[[CCSprite alloc]init]retain];
 leftArrow = [CCSprite imageWithFile:@"file.png"];
 rightArrow = [[[CCSprite alloc]init]retain];
 rightArrow = [CCSprite imageWithFile:@"file.png"];

I guess you're not seeing the sprite disappear. Due to what you did above, you may actually be setting the visible properties correctly, but since you created two leftArrow and two rightArrow objects, you might just be seeing the other one (the one that was alloc'ed, init'ed, and retain'ed, which has no pointers to it).

Also, imageWithFile might be returning an autoreleased object, you should look into that.

Edit (for my second comment below):

Replace

 leftArrow = [[[CCSprite alloc]init]retain];
 leftArrow = [CCSprite imageWithFile:@"file.png"];
 rightArrow = [[[CCSprite alloc]init]retain];
 rightArrow = [CCSprite imageWithFile:@"file.png"];

with

 leftArrow = [[CCSprite imageWithFile:@"file.png"] retain];
 rightArrow = [[CCSprite imageWithFile:@"file.png"] retain];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文