Three ways to create animation in cocos2dx
1. The most The original method is to create animation frames first, then create animation packaging (animation), and then create animation (animate)
The first step:
Create animation frame: CCSpriteFrame, relying on the original resource picture (xx.png,xx.jpg)
CCSpriteFrame *frame1=CCSpriteFrame::create(“1.png”);
CCSpriteFrame *frame2=CCSpriteFrame::create(“2.png”);
p>
CCSpriteFrame *frame3=CCSprteFrame::create(“3.png”);
.. .
Step 2: Create animation package, CCAnimation, which depends on the created animation frame, CCSpriteFrame
CCAnimation *animation=CCAnimation::create(); p>
animation->addSpriteFrame(frame1);
animation->addSpriteFrame(frame2);
p>
animation->addSpriteFrame(frame3);
…
Set the playback interval between frame animations
animation->setDelayPerUnit(0.2);
Set the number of frame animation loop playback
animation->setLoops(5);//-1 means infinite loop< /p>
Step 3: Create a real animation: animate, rely on animation packaging, CCAnimation
CCAnimate *animate=CCAnimate::create(animation);
Execute animation: spr ->runAction(animate);
//animation->addSpriteFrameWithFileName(CCString::createWithFormat(“animation/p_2_0%d.png”, i + 1) ->getCString());//Create a frame directly from the picture, which is a simplification of the above, but the frame buffer cannot be used, and the efficiency is not high
The second way to create animation:
Use frame animation cache: CCSpriteFrameCache, mainly to simplify the steps of loading from the original picture frame by frame to the memory
Step 1: Create a picture frame cache, which depends on the packaged xx.plist picture file
CCSpriteFrameCache::sharedFrameCache()->addSpriteFramesWithFile (“xx.plist”);
Step 2: Add the picture frame in the picture frame buffer to the CCArray array (container) through a loop, you need to first Create the container
CCArray *array=CCArray::create();
for( int i=0;i { CCString *str= CCString::createWithFormat(“%d.png”,i+1); CCSpriteFrame *frame=CCSpriteFrameCache::sharedFrameCache()->spriteFrameByName(str) ;//Get the picture frame from the picture frame buffer by the name of the picture frame array->addObject(str);//Add the picture frame to the array container } CCAnimation *animation=CCAnimation::createWithSpriteFrames(array);//pass Picture frame array to create animation package animat ion->setDelayUnit(0.2); animation->setLoops(-1); CCAnimate *animate=CCAnimate::create(animation); spr->runAction(animate);
The third way to create frame animation: No First load it into the container (CCArray) and save it, and then add it directly to the frame animation package. The first step: create the frame animation cache CCSpriteFrameCache CCSpriteFrameCache::sharedFrameCache()->addSpriteFramesWithFile(xx.plist); CCAnimation *animation=CCAnimation ::create(); for(int i=0;i { CCString str=CCString::createWithFormat(“%d”,i++); animation->addSpriteFrame(CCSpriteCache::sharedFrameCache()->spriteFrameByName(str->getCstring())); } animation->setDelayUnit(0.2); animation->setLoops(-1); CCAnimate *animate=CCAnimate::create(animation); spr->runAction(animate); The third method combines the advantages of the first and second methods, eliminating the need to first get the picture frames into the container, and then uniformly extract the picture frames from the CCArray To create the steps of animation packaging. lua < p style="line-height:1.5;">—frame animation local spriteTest = cc.Sprite:createWithSpriteFrameName(“txt_n_niuji0.png”) spriteTest:setAnchorPoint(0.5, 0.5) sprite(Test:setPosition(0.5, 0.5) display.cx/4, display.cy/2 ) )
local resFile2 = “res/gameres/general/weiwei/txt_n_niuji.plist”
local spriteFrame = cc.SpriteFrameCache:getInstance( )
SpriteFrame:addSpriteFrames(resFile2)
self._widgets.testPanelV:addChild( spriteTest )
local animation = cc.Animation:create()
for i=0, 9 do
– local frameName = string.format( “shuohua%02d.png”, i)
local blinkFrame = spriteFrame:getSpriteFrame( string.format( “txt_n_niuji%d.png”, i))
animation:addSpriteFrame (blinkFrame)
end
animation:setDelayPerUnit( 0.1 )–set the playback interval of each frame
animation:setRestoreOriginalFrame( true )–set whether to return to the original state after the playback is complete
–animation: setLoops(-1)
local action = cc.Animate:create(animation)
–spriteTest:runAction(action)
spriteTest:runAction( cc.RepeatForever:create( action) )
< /p>
Original: https://www.cnblogs.com/ttss/p/4093687.html
Three ways to create animation in cocos2dx
1. The most primitive method is to create animation frames first, then create animation packaging (animation), and then create animation (animate)< / p>
The first step:
Create animation frame: CCSpriteFrame, relying on the original resource picture (xx .png,xx.jpg)
CCSpriteFrame *frame1=CCSpriteFrame::create(“1.png”);
CCSpriteFrame *frame2=CCSpriteFrame::create(“2.png”);
CCSpriteFrame *frame3=CCSprteFrame::create(” 3.png”);
…
Step 2: Create animation package , CCAnimation, depends on the created animation frame, CCSpriteFrame
CCAnimation *animation=CCAnimation::create();
animation->addSpriteFrame(frame1);
animation->addSpriteFrame(frame2);
animation->addSpriteFrame(frame3);
…
Set the playback interval between frame animations
animation->setDelayPerUnit(0.2);
Set the number of times the frame animation is played in a loop
animation->setLoops(5);//-1 means infinite loop
Step 3: Create a real animation: animate, rely on animation packaging, CCAnimation
CCAnimate *animate=CCAnimate::create(animation);
Execute animation: spr->runAction(animate);
//animation-> addSpriteFrameWithFileName(CCString::createWithFormat(“animation/p_2_0%d.png”, i + 1)->getCString());//Create a frame directly from the picture, which is a simplification of the above, but it is impossible to use the frame Caching is not efficient
The second way to create animation:
Use frame animation cache: CCSpriteFrameCache, mainly to simplify the steps of loading from the original picture frame by frame to the memory
Step 1: Create a picture frame cache, which depends on the packaged xx.plist picture file
CCSpriteFrameCache::sharedFrameCache()->addSpriteFramesWithFile (“xx.plist”);
Step 2: Add the picture frame in the picture frame buffer to the CCArray array (container) through a loop, you need to first Create the container
CCArray *array=CCArray::create();
for( int i=0;i { CCString *str= CCString::createWithFormat(“%d.png”,i+1); CCSpriteFrame *frame=CCSpriteFrameCache::sharedFrameCache()->spriteFrameByName(str) ;//Get the picture frame from the picture frame buffer by the name of the picture frame array->addObject(str);//Add the picture frame to the array container } CCAnimation *animation=CCAnimation::createWithSpriteFrames(array);//pass Image frame array to create animation package animation->setDelayUnit(0.2); animation ->setLoops(-1); CCAnimate *animate=CCAnimate::create(animation); spr->runAction(animate);
The third type of creation The method of frame animation: You don’t need to load it into the container (CCArray) first and save it, just add it to the frame animation package. The first step: Create frame animation cache CCSpriteFrameCache CCSpriteFrameCache::sharedFrameCache()->addSpriteFramesWithFile(xx.plist); CCAnimation *animation=CCAnimation::create() ; for(int i=0;i {< /p> CCString str=CCString::createWithFormat(“%d”,i++); animation ->addSpriteFrame(CCSpriteCache::sharedFrameCache()->spriteFrameByName(str->getCstring())); } animation->setDelayUnit(0.2); animation->setLoops(-1); CCAnimate *animate=CCAnimate::create(animation); spr->runAction(animate); The third method combines the advantages of the first and second methods, eliminating the need to first get the picture frames into the container, and then uniformly extract the picture frames from the CCArray To create the steps of animation packaging. lua < p style="line-height:1.5;">—frame animation
local resFile2 = “res/gameres/general/weiwei/txt_n_niuji.plist”
local spriteFrame = cc.SpriteFrameCache:getInstance()
spriteFrame:addSpriteFrames(resFile2)
local spriteTest: cc.
createWithSpriteFrameName(“txt_n_niuji0.png”)
spriteTest:setAnchorPoint( 0.5, 0.5)
spriteTest:setPosition(cc.p( display.cx/4, display.cy/2))
self._widgets. testPanelV:addChild( spriteTest)
local animation = cc.Animation:create()
for i=0, 9 do
– local frameName = string.format( “shuohua%02d.png “, i)
local blinkFrame = spriteFrame:getSpriteFrame( string.format( “txt_n_niuji%d.png”, i)) animation:addSpriteFrame( blinkFrame )
DelayPerUnit( 0.1 )–Set the playback interval of each frame
Animation:setRestoreOriginalFrame( true )–Set whether to return to the original state after playback is complete
–animation:setLoops(-1)
Local action = cc.Animate: create(animation)
–spriteTest:runAction(action)< br> spriteTest:runAction( cc.RepeatForever:create( action) )
Original: https://www.cnblogs.com/ttss/p/4093687.html
< p>
1. The most primitive method is to create animation frames first, then create animation packaging (animation), and then create animation (animate)
The first step:
Create animation frame: CCSpriteFrame, relying on the original resource picture (xx.png, xx.jpg )
CCSpriteFrame *frame1=CCSpriteFrame::create(“1.png”);
CCSpriteFrame *frame2=CCSpriteFrame::create(“2.png”);
CCSpriteFrame *frame3=CCSprteFrame::create(“3.png”);
…
Step 2: Create animation package, CCAnimation, which depends on creation Good animation frame, CCSpriteFrame
CCAnimation *animation=CCAnimation::create();
animation->addSpriteFrame(frame1);
animation->addSpriteFrame(frame2);
animation->addSpriteFrame(frame3);
…
Set the playback interval between frame animations
animation->setDelayPerUnit(0.2);
Set the number of times of frame animation looping
animation->setLoops(5);//-1 means infinite loop
第Three steps: create real animation: animate, rely on animation packaging, CCAnimation
CCAnimate *animate=CCAnimate::create(animation);
< p style="line-height:1.5;">Execute animation:spr->runAction(animate);
//animation->addSpriteFrameWithFileName(CCString:: createWithFormat(“animation/p_2_0%d.png”, i + 1)->getCString());//Create a frame directly from the picture, which is a simplification of the above, but the frame buffer cannot be used, and the efficiency is not high
The second way to create animation:
Use frame animation cache: CCSpriteFrameCache, mainly to simplify the steps of loading from the original picture frame by frame to the memory
Step 1: Create a picture frame cache, which depends on the packaged xx.plist picture file
CCSpriteFrameCache::sharedFrameCache()->addSpriteFramesWithFile(“xx.plis t”);
Step 2: Add the picture frame in the picture frame buffer to the CCArray array (container) through a loop, you need to create the container first< /p>
CCArray *array=CCArray::create();
for(int i=0 ;i { CCString *str=CCString::createWithFormat (“%d.png”,i+1); CCSpriteFrame *frame=CCSpriteFrameCache::sharedFrameCache()->spriteFrameByName(str);//Pass The name of the picture frame gets the picture frame from the picture frame buffer array->addObject(str);//Add the picture frame to the array container } CCAnimation *animation=CCAnimation::createWithSpriteFrames(array);//By the image frame array Create animation package animation->setDelayUnit(0.2); animation->setLoops( -1); CCAnimate *animate=CCAnimate::create(animation); spr->runAction(animate);
The third method of creating frame animation: does not need to be loaded into the container (CCArray) to save, directly add the frame animation package Just medium. Step 1: Create frame animation cache CCSpriteFrameCache CCSpriteFrameCache:: sharedFrameCache()->addSpriteFramesWithFile(xx.plist); CCAnimation *animation=CCAnimation::create(); for(int i=0;i { CCString str=CCString::createWithFormat(“%d”,i++); animation->addSpriteFrame(CCSpriteCache::sharedFrameCache()- >spriteFrameByName(str->getCstring())); } animation-> setDelayUnit(0.2); animation->setLoops(-1); CCAnimate * animate=CCAnimate::create(animation); spr->runAction(animate); The third method combines the advantages of the first and second In the browser, the steps of extracting picture frames from CCArray to create animation packaging are unified. lua —frame animation local spriteTest = cc.Sprite:createWithSpriteFrameName(“Pointtxt_n_niuji0.png”) sprite(0.5
local resFile2 = “res/gameres/general/weiwei/txt_n_niuji.plist”
local spriteFrame = cc.SpriteFrameCache:getInstance( )
spriteFrame:addSpriteFrames(resFile2)
spriteTest:setPosition(cc.p( display.cx/4, display.cy/2))
self._widgets.testPanelV:addChild( spriteTest)
local animation = cc.Animation: create()
for i=0, 9 do
– local frameName = string.format( “shuohua%02d.png”, i) local blinkFrame = spriteFrame:getSpriteFrame( string.format( ” txt_n_niuji%d.png”, i)) animation:addSpriteFrame( blinkFrame)
end animation:setDelayPerUnit( 0.1 )–Set the playback interval of each frame
animation:setRestoreOriginal Frame( true )–Set whether to return to the original state after playback is complete
–animation:setLoops(-1)
local action = cc.Animate:create(animation)
–spriteTest:runAction(action )
spriteTest:runAction( cc.RepeatForever:create( action) )