请教一个关于CCTexture2D的使用box2d方法

使用CCAnimate、CCAnimation、CCTextureCache、CCTexture2D来实现动画效果-中国学网-中国IT综合门户网站-提供健康,养生,留学,移民,创业,汽车等信息
使用CCAnimate、CCAnimation、CCTextureCache、CCTexture2D来实现动画效果
来源:互联网 更新时间: 5:33:14 责任编辑:李志喜字体:
使用CCTexture2D来创建动画效果,前提资源是有一张合成的大图
下面看具体的做法:
CCSprite* heroSprite = CCSprite::create();
heroSprite -& setAnchorPoint(ccp(0.35,0.3));
heroSprite -& setPosition(ccp(heroSprite -& getContentSize().width/2, m_winSize.height/2));
this -& addCHild(heroSprite,1,0);
CCAnimation* animation = CCAnimation::create();
animation -& setDelayPerUnit(0.3f);//设定时间间隔
animation -& setLoops(-1);//(-1)表示一直执行
CCTextureCache* cache = CCTextrueCache::sharedTextureCache();
CCTexture2D* texture = cache -& addImage(&background.png&);//这张图片上有要实现运动的各个图片
for(int i = 0; i & 5; i++)
& CCRect rect = CCRectMake(85*i,0,85,121);
& animation -& addSpriteFrameWithTexture(texture,rect);
CCAnimate* animate = CCAnmiate::create(animation);
heroSprite -& runAction(animate);
当然,如果你的图片是自己合成的,就应该有plist文件,这时候,就不用使用CCTexture2D来实现动画效果了,还有一个更简单的方法
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache -& addSpriteFrameWithFile(&background.plist&);
char temp[20];
CCArray* plistArray = CCArray::createWithCapacity(10);
for(int i = 0; i & 10; i++)
& sprintf(temp,&pook%d&,i);
& CCSpriteFrame* frame = cache -& spriteFrameByName(temp);
& plistArray -& addObject(frame);
CCAnimation* animation = CCAnimation::createWithSpriteFrames(plistArray,0.1);
CCAnimate* animate = CCAnimate::create(animation);
//然后让执行此动画的精灵开始执行animate即可
//heroSprite -& runAction(animte);
相关文章:
上一篇文章:下一篇文章:
最新添加资讯
24小时热门资讯
Copyright © 2004- All Rights Reserved. 中国学网 版权所有
京ICP备号-1 京公网安备02号13:20:45 UTC
i want to know is there a way to getPixel/setPixel from CCTexture2Di look into a class CCMutableTexture2D which can do thatand try to convert it to cocos2dx.but there is a lot of class cocos2dx doesnt support.like CGContextRef,CGColorSpaceRef …
any other way to do this?
18:37:52 UTC
In cocos2d-iphone, CCTexture2D have no interfaces of getPixel & setPixel, because glReadPixels & glWritePixels have very low performance.But you can add glReadPixels & glWritePixels into CCTexture2D as you wish, and export interface for your usage.
You talked about CGContextRef,CGColorSpaceRef, they work with CGImage/UIImage, not CCTexture2D. So another approach is to modify CCImage in cocos2d-x, it’ll be more close to your original code. But you had to implement the new interfaces in all your target platforms.
19:14:27 UTC
In my knowledge, i don’t know there is any way to read or write pixels directly from a texture.
if you do want to do that, ccrendertexture can realize the idea, but glreadpixels is rather slow, a texture of
would cost 0.2-0.3s.
10:54:01 UTC
I’ve just ported CCTexture2DMutable to cocos2d-x and it seems working. A very quick test:
CCSize texSize = CCSizeMake(128, 128);
int bytes = texSize.width * texSize.height * 4;
void* textureData = malloc(bytes);
memset(textureData, INT32_MAX, bytes);
CCTexture2DMutable* texture = new CCTexture2DMutable();
if (!texture-&initWithData(textureData, kCCTexture2DPixelFormat_RGBA8888, texSize.width, texSize.height, texSize)) {
CCLOG("Could not create texture");
texture = NULL;
texture-&setAliasTexParameters();
for (int x = 0; x & 128; x++) {
for (int y = 0; y & 128; y++) {
ccColor4B color = ccc4(CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255,
CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255);
texture-&setPixelAt(CCPointMake(x, y), color);
texture-&apply();
CCSprite* sprite = CCSprite::spriteWithTexture(texture);
sprite-&setScale(3.0);
this-&addChild(sprite);
texture-&autorelease();
10:55:40 UTC
However, I don’t know why, but another simpler test doesn’t work. It should draw a single diagonal line with random pixels.
for (int x = 0; x & 128; x++) {
ccColor4B color = ccc4(CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255,
CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255);
texture-&setPixelAt(CCPointMake(x, x), color);
Anyway, I think, it would be good to get CCRenderTexture and CCTexture2DMutable married together
20:06:19 UTC
this actually does work, i think the issue is when you apply the texture to the sprite, it anchors the center of the texture to the origin of the sprite. if you translate the sprite to the upper right, you will see the result.
Dmitry Matyukhin wrote:
However, I don’t know why, but another simpler test doesn’t work. It should draw a single diagonal line with random pixels.&[…]&Anyway, I think, it would be good to get CCRenderTexture and CCTexture2DMutable married together
15:32:51 UTC
I just tested it with cocos2d-1.0.1-x-0.13.0-beta. In Android if you change the texSize to NPOT (Not Power Of Two), it just draws white. On iOS it works fine.Any clue why it does this?

我要回帖

更多关于 cctexturecache 的文章

 

随机推荐