为什么创建一个移动中的精灵那么难?

为什么创建一个移动中的精灵那么难?

参照论坛地球人系列,无脑码农系列,全部没有一个例子是成功的。不是缺少某个函数,就是直接内存错误。

CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile(“images.plist”, “images.png”);
// 1. Add a menu item with “X” image, which is clicked to quit the program.

CCArray *animFrames = new CCArray(4);
CCSpriteFrame *frame1 = cache->spriteFrameByName(“girl1.png”);
CCSpriteFrame *frame2 = cache->spriteFrameByName(“girl2.png”);
CCSpriteFrame *frame3 = cache->spriteFrameByName(“girl3.png”);
CCSpriteFrame *frame4 = cache->spriteFrameByName(“girl4.png”);
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);
animFrames->addObject(frame4);
CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.5f);

CCSprite* girlSprite = CCSprite::createWithSpriteFrame(frame1);
CC_BREAK_IF(!girlSprite);
girlSprite->setPosition(ccp(300,300));
CCAnimate* animate=CCAnimate::actionWithAnimation(animation);
CC_BREAK_IF(!animate);
girlSprite->runAction(CCRepeatForever::create(animate));

请高手指点指点。

总之用的 0|0 疼 :Q:Q:Q

顶ls

主要是版本问题,很多方法正在过渡中~

  • 本帖最后由 夺命半截砖 于 2013-2-18 12:22 编辑 *

主要是cocos2d-x的版本更替比较快
很多函数都会更改函数名或者参数表
所以旧一点点的教程可能都会出现各种各样的问题
我现在自己在学也觉得很恶心。。
至于你的问题
现在帧动画Animation里面有成员是CCMutableArray存放帧的
所以不用CCArray创建,改成这样
CCAnimation *animation = CCAnimation::create();
animation->addFrame(frame0);
animation->addFrame(frame1);
animation->addFrame(frame2);
animation->addFrame(frame3);
animation->setDelayPerUnit(0.2f);
girlSprite->runAction(CCRepeatForever::create(CCAniamte::create(animation)));
没了VS的自动联想可能有些拼写错误。。。

我也遇到错误:

报错如下:

Microsoft Visual C++ Runtime Library

Assertion failed!

Program: …
File: d:aaa-androidcocos2d-2.1beta3-x…ccanimation.cpp
Line: 144

Expression: dynamic_cast<CCAnimationFrame*>(*arr)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)

中止(A) 重试® 忽略(I)

中断点:
CCAnimation animation = CCAnimation::create(pArray, 0.2f);
转入:
CCARRAY_VERIFY_TYPE(arrayOfAnimationFrames, CCAnimationFrame
);

版本:cocos2d-2.1beta3-x-2.1.1

代码截取如下:
//第一步 先生成一个TEXTURE
CCTexture2D texture=CCTextureCache::sharedTextureCache()->addImage(“player.png”);
//对texture进行切割并分配给 CCSpriteFrame 帧动画
CCSpriteFrame frame0=CCSpriteFrame::createWithTexture(texture,CCRectMake(320, 48
0, 32, 48));
CCSpriteFrame frame1=CCSpriteFrame::createWithTexture(texture,CCRectMake(321, 480, 32, 48));
CCSpriteFrame frame2=CCSpriteFrame::createWithTexture(texture,CCRectMake(322, 48
0, 32, 48));
CCSpriteFrame frame3=CCSpriteFrame::createWithTexture(texture,CCRectMake(323, 48*0, 32, 48));

	//定义一个CCArray 并初始化
	CCArray * pArray=CCArray::create(); //此处与实例有变动

pArray->addObject(frame0);
pArray->addObject(frame1);
pArray->addObject(frame2);
pArray->addObject(frame3);

//定义一个CCAnimation 入参ccarray 和帧动画时间
CCAnimation *animation = CCAnimation::create(pArray, 0.2f);

还请高手相助。谢谢!

自己参考cocos2d的吧
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:animation

CCArray *animFrames = new CCArray(4);
为什么要用new不用类方法create?这样还要手动delete,而且还没有init