2013-07-23 3 views
2

저는 현재 blackberry/android/iOS 용 게임을 만들기 위해 cocos2d-x로 시작하고 있습니다.cocos2d-x에서 여러 애니메이션을 설정하는 방법

texturepacker로 만든 캐릭터의 애니메이션에 대한 png 및 plist가 있습니다. CCSpriteBatchNodeCCSpriteFrameCache을로드 한 다음 프레임 배열에 모든 프레임을로드 한 다음 해당 함수를 사용하여 CCAnimation 개체를 만들고 CCAnimate 개체를 애니메이션 (코드가 더 명확함)으로 저장하면 해당 항목이 I입니다. 터치를 감지하고 모든 애니메이션을 순환하는 기능을 가지고 있지만 항상 충돌합니다. 여기

일부 코드 (이것은 init() 간다)이다

_batchNode = CCSpriteBatchNode::create("Character/girl.png"); 
_cache = CCSpriteFrameCache::sharedSpriteFrameCache(); 

_cache->addSpriteFramesWithFile("Personajes/girl.plist"); 

_character = CCSprite::createWithSpriteFrameName("girlneutral1.png"); 
_character->setPosition(ccp(winSize.width * 0.1, winSize.height * 0.5)); 
_batchNode->addChild(_character, 1); 
this->addChild(_batchNode); 
createAnimation(0, "girlpush", 8, 0.15f); 
createAnimation(1, "girlneutral", 4, 0.3f); 
createAnimation(2, "girlcrash", 12, 0.3f); 
createAnimation(3, "girljump", 12, 0.2f); 
createAnimation(4, "girltrick", 12, 0.3f); 

_character->runAction(CCRepeatForever::create(_charanimation[3])); 

this->setTouchEnabled(true); 

(_charanimation []를 단지 CCAnimate의 배열) 애니메이션을로드하는 기능 :

void HelloWorld::createAnimation(int a, CCString animation_name, int frames, float delay) 
{ 
    CCArray* animframes = CCArray::createWithCapacity(frames); 
    char str[100] = {0}; 
    for(int i = 1; i <= frames; i++) 
    { 
     sprintf(str, "%s%d.png", animation_name.getCString(), i); 
     CCSpriteFrame* frame = _cache->spriteFrameByName(str); 
     animframes->addObject(frame); 
    } 
    _charanimation[a] = CCAnimate::create(CCAnimation::createWithSpriteFrames(animframes, delay)); 
    //_charanimation[a]->getAnimation()->setLoops(-1); 
} 

및 I 애니메이션을 작동 시키려면 (runAction()으로 설정) 애니메이션을 변경하려고 시도합니다 (예 : 화면을 터치 할 때).

void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event) 
{ 
    action++; 
    action%=5; 
    _character->stopAllActions(); 
    _character->runAction(CCRepeatForever::create(_charanimation[action])); 
    char str[100]; 
    sprintf(str, "Animation: %d", action); 
    pLabel->setString(str); 

} 

크래시가 ... 내가 잘못하고 있는지, 누구든지 도울 수 있다면 감사 할 것입니다.

runAction()에서 애니메이션을 변경하면 애니메이션이 올바르게 표시되지만 터치로 고통을 변경할 수 없습니다. 그런데

이 내가 콘솔에서 얻을 오류입니다 :

cocos2d-x debug info Assert failed: reference count should greater than 0 
In function retain -- ..\..\cocoa\CCObject.cpp:92 m_uReference > 0 -- assertion failed 

답변

2

그 생성 한 CCAnimate 객체가 오토 릴리즈 객체이기 때문에 당신은 유지하지 대상이다. Autorelease 개체가 명시 적으로 또는 다른 개체에 의해 이 아닌 경우이 자동으로 삭제됩니다. 당신이 할 수있는 배열에 추가하는 동안

CCAnimate *animate = CCAnimate::create(CCAnimation::createWithSpriteFrames(animframes, delay)); 
animate->retain(); 
_charanimation[a] = animate; 

모든

  • _charanimation[index]->release(); 
    

    이상 참고있을 때 배열의
    모든 개체를 해제하는 것을 잊지 마세요 :

    간단한 C 또는 C++ 배열을 사용하는 대신 Coc os2d의 CCArray 어느 은 배열에 오브젝트가 일단 추가되면을 유지합니다.

    예를 들어

:

_charanimation = new CCArray(); 

//To add object to array 
_charanimation->addObject(object); //This will retain the object 

//To get an object 
_charanimation->objectAtIndex(index); 
_charanimation->lastObject(); 
_charanimation->randomObject(); 

//To remove object 
_charanimation->removeObjectAtIndex(index); //This will release object 
_charanimation->removeObject(object);  //This will release object 

//Dont forget delete array later 
delete (_charanimation); 

You can refer this link for more on CCArray

+0

완벽한, 그래서 감사합니다 (메모리 처리가 목표 - C와 유사하다), 그 일부적인 Cocos2D 객체를 몰랐다 스스로 해방. CCArray에서 작동하게 만드는 방법에 대한 예제를 게시 할 수 있습니까? CCArray에 추가 한 객체를 얻는 방법을 모르겠습니다. 나는 그것을 사용하는 방법을 여전히 배우고있다. – nosmirck

+0

나는 또 다른 질문이있다. 어떻게이 모든 것들을 하나의 클래스로 만들 수 있는가? (플레이어 클래스를 말하게한다) 그리고 그것을 init에 통합 할 수 있을까? 나는 시험해 보니 충돌이 일어난다. 나는 batchnodes와 캐쉬를 잘 관리하지 못한다고 생각한다. 아마도 분리 된 클래스의 일부가 될 수 없다. 잘 모르겠다. 예제가 필요하다. 나를 도울 수 있니? .png 및 .plist (추가 정보 필요)를 전달할 수있는 클래스가 필요하며 PlayAnimation (인덱스)과 같이 호출 할 수있는 메서드가 있어야합니다. 그래서 Player p1, p2와 같은 것을 만들 수 있습니다. 일부 코드에서는 p1.PlayAnimation (2)을 실행할 수 있습니다. p2.PlayAnimation (0); – nosmirck

+1

답변에 예제를 추가했습니다. 당신은 그곳의 링크를'CCArray'에 더 참조 할 수 있습니다. – Souandios

관련 문제