bool CCNewGame::init(){
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
//添加主角精灵
CCTexture2D * pTextTure = CCTextureCache::sharedTextureCache()->addImage("dragon.png");
this->m_pSpriteBatch = CCSpriteBatchNode::createWithTexture(pTextTure,10);
this->addChild(m_pSpriteBatch,1,1);
this->m_pFlyActionArray = CCArray::create();
for (int i=0;i<8;i++){
CCAnimation * pAnimation = CCAnimation::create();
CC_BREAK_IF(!pAnimation);
pAnimation->setDelayPerUnit(0.15f);
for (int j=0;j<10;j++){
CCSpriteFrame * frame = CCSpriteFrame::createWithTexture(this->m_pSpriteBatch->getTexture(),
CCRectMake(75*j,70*i,75,70));
CC_BREAK_IF(!frame);
pAnimation->addSpriteFrame(frame);
}
CCAnimate * anim = CCAnimate::create(pAnimation);
this->m_pFlyAction = CCRepeatForever::create(anim);
this->m_pFlyActionArray->addObject(m_pFlyAction);
}
CCSpriteFrame * frame1 = CCSpriteFrame::createWithTexture(this->m_pSpriteBatch->getTexture()
,CCRectMake(75*0,70*0,75,70));
CC_BREAK_IF(!frame1);
this->m_pDragon = CCSprite::createWithSpriteFrame(frame1);
this->m_pDragon->setPosition(ccp(160,240));
this->m_pFlyAction = (CCAction *)m_pFlyActionArray->objectAtIndex(0);
this->m_pDragon->runAction(m_pFlyAction);
this->addChild(m_pDragon,0,123);
this->setTouchEnabled(true);
bRet = true;
} while (0);
return bRet;
}
void CCNewGame::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
CCPoint p = pTouch->getLocation();
CCPoint nowPoint = this->getChildByTag(123)->getPosition();
CCPoint moveVector = ccpSub(p, nowPoint);
float distance = ccpLength(moveVector);
float moveAngle = ccpToAngle(moveVector);
float cocosAngle = CC_RADIANS_TO_DEGREES(-1 * moveAngle);
float moveDuration = distance/(480.0/3.0);
cocosAngle += 23;
if (cocosAngle <0){
cocosAngle += 360;
}
int animNum = (int)((cocosAngle)/45);
this->getChildByTag(123)->stopAction(this->m_pFlyAction);
this->m_pFlyAction = (CCAction *)this->m_pFlyActionArray->objectAtIndex(animNum);///报错是这一步
this->getChildByTag(123)->runAction(this->m_pFlyAction);
this->getChildByTag(123)->runAction(CCMoveTo::create(moveDuration,p));
}
------解决方案--------------------
应该是因为你在init里面创建的array没有retain, 你可以retain一下试试