我已经使用spriteWithFile方法定义了一个sprite,提供了一个120px x 30px的.png
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乘以30.000000吗?
任何帮助都将不胜感激。
致以敬意,
富足
发布于 2009-07-15 17:28:32
这些线路是跳床班的一部分吗?
Sprite *trampoline = [Sprite spriteWithFile:@"trampoline.png"];
[self addChild:trampoline];从我有限的cocos2d经验来看,雪碧图的contentSize似乎只适用于雪碧图的内容,而不是雪碧图的所有子元素。因此,在上面的示例中,请求日志语句中的contentSize将不起作用,因为没有任何内容添加到Trampoline节点。但是,如果您要重写Trampoline类中的contentSize方法以返回实际加载图形的Sprite的contentSize,则应该可以工作。
这是我目前正在开发的一款游戏中使用的雪碧的一个片段,它说明了我正在谈论的内容:
- (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的内容大小,它就像我期望的那样工作。
发布于 2009-07-10 18:03:24
我假设Trampoline继承自Sprite,而Sprite则继承自Node。您正在使用创建节点的trampoline节点覆盖TRAMPPOLIN...但是,Trampoline实现是否覆盖了node方法,从而将sprite文件初始化为Trampoline节点?
我认为您只是从行中得到了一个空的Node类:
trampoline = [Trampoline node];https://stackoverflow.com/questions/1110991
复制相似问题