iphone - cocos2d Sprite contentSize问题

标签 iphone cocos2d-iphone sprite

我使用 spriteWithFile 方法定义了一个 Sprite ,提供了一个 120px x 30px .p​​ng

Sprite *trampoline = [Sprite spriteWithFile:@"trampoline.png"];     
[self addChild:trampoline];

当我将其添加到图层并放置它时,它就是我期望它在屏幕上出现的位置。

trampoline = [Trampoline node];
trampoline.position = ccp(160,15);
[self addChild:trampoline z:0 tag:1];

但是,它似乎没有 contentSize。 NSLog语句如下:

NSLog(@"Content Size x:%f, y:%f", trampoline.contentSize.width,trampoline.contentSize.height);

给出以下读数:

2009-07-10 18:24:06.385 TouchSprite[3251:20b] Content Size x:0.000000, y:0.000000

我错过了什么吗?这不应该是 120.000000 x 30.000000

任何帮助将不胜感激。

问候,

丰富

最佳答案

这些线条是 Trampoline 类的一部分吗?

Sprite *trampoline = [Sprite spriteWithFile:@"trampoline.png"];
[self addChild:trampoline];

根据我对cocos2d的有限经验,Sprite的contentSize似乎只适用于实际属于Sprite的内容,而不是该Sprite的所有子元素。因此,在上面的示例中,在日志语句中请求 contentSize 将不起作用,因为没有任何内容添加到 Trampoline 节点。但是,如果您要重写 Trampoline 类中的 contentSize 方法以返回实际加载图形的 Sprite 的 contentSize,那么应该可以工作。

这是我在当前正在开发的游戏中使用的 Sprite 片段,它说明了我正在谈论的内容:

- (id) init
{
self = [super init];

if (self != nil)
{       
    self.textLabel = [Label labelWithString:@"*TEXT*"
                                   fontName:@"Helvetica"
                                   fontSize:18];

    [textLabel setRGB:0 :0 :0];

    textLabel.transformAnchor = CGPointZero;
    textLabel.position = CGPointZero;
    self.transformAnchor = CGPointZero;

    [self addChild:textLabel];
}

return self;
}
//

- (CGSize) contentSize
{
return textLabel.contentSize;
}

它来自一个扩展 Sprite 的类。在我添加 contentSize 的覆盖之前,从另一个类请求它会给我与您看到的相同的结果。现在我告诉它返回 textLabel 的内容大小,它的工作方式就像我期望的那样。

关于iphone - cocos2d Sprite contentSize问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1110991/

相关文章:

ios - 模糊效果在模拟器中显示,但在 iPhone 上不显示

apache-flex - 在 Sprite 中获取应用于 Flash Sprite 的滤镜范围

ios - 获取音频m4a文件ios的播放时间

iphone - 如何在 ios 中将 Nsstring 值转换为十六进制

cocos2d-iphone - 如何在cocos2d中使用转换popScene?

objective-c - 无法在参数传递中将 'b2PolygonShape' 转换为 'objc_object*'

ios - 改变场景cocos2d

java - 在java中加载 Sprite 图像

swift - 在 Swift 中保存/存档 SKSpriteNode

iphone - 从 NSDictionary 填充表格 View