2009-07-10 107 views
3

我定义使用spriteWithFile方法的精灵,提供了一个120像素的30像素png格式的cocos2d精灵contentSize问题

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 

我缺少的东西?这不应该是由30.000000 120.000000

任何帮助将不胜感激。

问候,

丰富

回答

3

是蹦床类的这些线的一部分?

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

从我的cocos2d的经验有限,雪碧的contentSize似乎只适用于实际上属于雪碧的内容,而不是所有的雪碧的孩子。因此,在上面的示例中,在日志语句中询问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; 
} 

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

0

我以为蹦床从雪碧,然后从节点继承继承。你用[Trampoline节点]覆盖了蹦床,它创建了一个节点...但是蹦床实现覆盖了节点方法来初始化你的Sprite文件到Trampoline节点中?

我觉得你是刚开始一个空的Node类回从线路:

trampoline = [Trampoline node]; 
+0

蹦床是Sprite的一个子类。线trampoline = [Trampoline节点]正在初始化我的对象。 蹦床不是空的,因为它有它的位置设置,并显示在我的屏幕上的图像。 – Rich 2009-07-10 18:44:58