如何将 CCSprite 分层到 CCSprite 之上?
在我的游戏中,我有一系列的头像,目前每个头像都有一个小的矩形容器来表示该图像是一个按钮并且可以单击。
但是,我意识到我需要更改容器的颜色来表示视觉效果(例如单击按钮)。
例如:
关闭状态 = 灰色容器。开启状态 = 红色容器
我不想在关闭/开启状态下重复每个头像,而是想将容器和头像分开,有点像这个粗略的绘图;
+------+ +------+
| | | | O
| OFF | | ON | / \
| | | | ---
+------+ +------+
因此,我们将关闭容器、开启容器和化身分开。
然后,在我的游戏中,我希望能够使用我想要使用的图层重新构造精灵,此外,我希望新构造的精灵能够像单个 CCSprite 一样工作,这样我就可以正确定位它,或者做其他事情与精灵。
有点像从相互叠加的各种 CCSprite 创建 CCSprite。
因此,如何将多个精灵叠加在一起,然后使用分层的 CCSprite 创建 CCSprite?
谢谢。
/编辑:添加了一些代码。
我使用 CCLayer 创建了一些代码,然后将精灵添加为图层的一部分。
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
@"avatars.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:@"avatars.png"];
[self addChild:spriteSheet];
CCSprite *empty = [CCSprite spriteWithSpriteFrameName:@"hold_empty.png"];
CCSprite *boss1 = [CCSprite spriteWithSpriteFrameName:@"hold_boss_1.png"];
CCLayer *holderExample = [[CCLayer alloc] init];
[holderExample setContentSize:CGSizeMake(70, 72)];
[holderExample setPosition:CGPointMake(100, 100)];
[holderExample addChild:empty];
[holderExample addChild:boss1];
[self addChild:holderExample];
这似乎创建了一个里面有两个精灵的图层。这似乎是正确的,但我不确定这是否正确,因为我不确定是否可以使其可点击,或处理其他事件。
另外,我想添加一个颜色层。
按 Z 顺序:
1) -2 empty.png
2) -1 颜色层
3) 0 头像
In my game, I have a series of avatars, currently each avatar has a small rectangular container to denote that this image is a button and can be clicked.
However, I've realised that I need to change the color of the container to denote a visual effect (such as the button click).
For example:
Off state = Grey container. On state = Red container
Rather than repeating each avatar with the off/on state on, I'd like to seperate the container and the avatars, a bit like this crude drawing;
+------+ +------+
| | | | O
| OFF | | ON | / \
| | | | ---
+------+ +------+
Thus, we seperate the off container, the on container and the avatars.
Then, in my game I want to be able to re-construct the sprite with the layers I want to use, in addition I want the newly constructed sprite to act like a singular CCSprite, so I can position it properly, or do other things with the sprite.
Sorta like creating a CCSprite from various CCSprites layered on top of each other.
Thus, how can I layer multiple sprites on top of each other, and then create a CCSprite using layered CCSprites?
Thanks.
/Edit: Added some code.
I have created some code using CCLayer and then added my sprites as part of a layer.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
@"avatars.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:@"avatars.png"];
[self addChild:spriteSheet];
CCSprite *empty = [CCSprite spriteWithSpriteFrameName:@"hold_empty.png"];
CCSprite *boss1 = [CCSprite spriteWithSpriteFrameName:@"hold_boss_1.png"];
CCLayer *holderExample = [[CCLayer alloc] init];
[holderExample setContentSize:CGSizeMake(70, 72)];
[holderExample setPosition:CGPointMake(100, 100)];
[holderExample addChild:empty];
[holderExample addChild:boss1];
[self addChild:holderExample];
This seems to make a layer with two sprites inside it. This seems right, but I'm not sure if this is correct because I'm not sure if I can make it clickable, or handle other events.
In addition, I'm wanting to add a layer for the color.
In Z-Order:
1) -2 empty.png
2) -1 color layer
3) 0 avatar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为开/关状态制作图像,并将其添加为具有开、关和头像图像的自定义 CCSprite 的子项。此外,您还可以为头像应用颜色以产生选择效果。不知道这是否正是您所需要的。
//编辑1
检查此来源并让我知道这是否是您所需要的。我所做的非常简单,只是为了向您展示我对颜色的含义。希望这有帮助 https://rapidshare.com/files/3668681798/TestAvatar.zip
You can make images for the on/off states and add as child of a custom CCSprite that has the on, off and avatar images. Also, you can apply a color to the avatar to make the selection effect. Don't know if this is exactly what you need.
//Edit1
Check this sources and let me know if this is what you need. Is pretty simple what i did just to show you what i mean about the color. Hope this help https://rapidshare.com/files/3668681798/TestAvatar.zip