Quick-cocos2d-x game development [8] -- animation and action

Source: Internet
Author: User
Tags addchild

Animations and actions are encapsulated in quick, so let's take a look.

In general, we can often use the quick Encapsulation Method for Frame Animation, which is very convenient. The following code is used to intuitively feel the situation,

For example, 14 frames of images, the use of cocos2d-x Lua method to write is like this,

 local sp = display.newSprite("grossini_dance_01.png", display.cx, display.cy)    self:addChild(sp)        local animation = CCAnimation:create()    local number, name    for i = 1, 14 do        if i < 10 then            number = "0"..i        else            number = i        end        name = "grossini_dance_"..number..".png"        animation:addSpriteFrameWithFileName(name)    end    animation:setDelayPerUnit(2.8 / 14.0)    local action = CCAnimate:create(animation)    sp:runAction(action)

Each frame needs to be added to ccanimation, which is the same as C ++, but quick is used in this way,

Display. addspriteframeswithfile ("Hero. plist "," hero.png ") -- add the frame cache local sp = display. newsprite ("maid", display. CX, display. cy) Self: addchild (SP) Local frames = display. newframes ("grossini_dance_10902d.png", 1, 14) Local animation = display. newanimation (frames, 2.8/14.0) SP: playanimationonce (animation)

The meanings of parameters of display. newframes (pattern, begin, length, isreversed) are,

  • StringPatternMode string
  • IntegerBeginStart Index
  • IntegerLengthLength
  • BooleanIsreversedWhether it is a descending Index
Note that the image name in newframes must be the image name in the frame cache. In other words, we need to use the image packaging tool to process the image, if multiple images are used, they certainly cannot be used. You can think of it that we will use image packaging tools to process images later, so quick will encapsulate this method directly.

If you don't believe it, you can check the source code of this function,

function display.newFrames(pattern, begin, length, isReversed)    local frames = {}    local step = 1    local last = begin + length - 1    if isReversed then        last, begin = begin, last        step = -1    end    for index = begin, last, step do        local frameName = string.format(pattern, index)        local frame = sharedSpriteFrameCache:spriteFrameByName(frameName)        if not frame then            printError("display.newFrames() - invalid frame, name %s", tostring(frameName))            return        end        frames[#frames + 1] = frame    end    return framesend


Directly call the spriteframebyname function.


For playing an animation, quick provides two functions for the sprite genie class,

function Sprite:playAnimationOnce(animation, removeWhenFinished, onComplete, delay)    return transition.playAnimationOnce(self, animation, removeWhenFinished, onComplete, delay)endfunction Sprite:playAnimationForever(animation, delay)    return transition.playAnimationForever(self, animation, delay)end

One is to play the animation once, and the other is to play the animation permanently. Easy to use!


The above is the animation usage. Next we will look at the use of the action,

The class encapsulated by the action is transition, which provides these functions,

Transition. neweasing (action, easingname, more)
Create effects for images
Transition.exe cute (target, action, argS)
Execute an action
Transition. rotateto (target, argS)
Rotate the displayed object to the specified angle and return the ccaction object.
Transition. moveTo (target, argS)
Move the displayed object to the specified position and return the ccaction object.
Transition. fadeto (target, argS)
The transparency of the displayed object is changed to a specified value, and the ccaction action object is returned.
Transition. scaleto (target, argS)
Scales the displayed object to a specified scale and returns the ccaction object.
Transition. Sequence (Actions)
Create an action sequence object.
Transition. playanimationonce (target, animation, removewhenfinished, oncomplete, delay)
Play an animation on the displayed object and return the ccaction object.

I think the single actions like move, scale, and fade are easy to remember and use, for example, it is quite good to use ccmoveto for mobile. However, what I think is really good about quick encapsulation is,

Transition.exe cute (target, action, argS)

Transition. Sequence (Actions)

These two, why? Let's see,


Transition.exe cute () is a powerful tool that can add various additional features for a single action.

The parameter table of transition.exe cute () supports the following parameters:

  • Delay: how long will the execution start?
  • Easing: the name of the easing effect and optional additional parameters. The effect name is case insensitive.
  • Oncomplete: the function to be called after the action is executed.
  • Time: the time required to execute the action.

The following describes the easing effect supported by transition.exe cute:

  • Backin
  • Backinout
  • Backout
  • Bounce
  • Bouncein
  • Bounceinout
  • Bounceout
  • Elastic. The default value of the additional parameter is 0.3.
  • Elasticin. The default value of additional parameters is 0.3.
  • Elasticinout. The default value of additional parameters is 0.3.
  • Elasticout. The default value of the additional parameter is 0.3.
  • Exponentialin. The default value of the additional parameter is 1.0.
  • Exponentialinout. The default value of the additional parameter is 1.0.
  • Exponentialout. The default value of the additional parameter is 1.0.
  • In. The default value of the additional parameter is 1.0.
  • Inout. The default value of the additional parameter is 1.0.
  • Out. The default value of the additional parameter is 1.0.
  • Rateaction. The default value of the additional parameter is 1.0.
  • Sinein
  • Sineinout
  • Sineout
This function can complete the speed effect in motion, as well as cccallfunc, ccdelaytime and other functions. If you append them together, you don't need to write tedious function nesting and ccsequence. Liao Dazhen wrote it to my heart. Like this,

transition.execute(sprite, CCMoveTo:create(1.5, CCPoint(display.cx, display.cy)), {    delay = 1.0,    easing = "backout",    onComplete = function()        print("move completed")    end,})

Transition. sequence is also a convenient function. If multiple actions are executed in sequence before,

 local move1  = CCMoveBy:create(1, ccp(250,0))    local move2  = CCMoveBy:create(1, ccp(0,50))    local array  = CCArray:createWithCapacity(2)    array:addObject(move1)    array:addObject(move2)    local seq = CCSequence:create(array)

Each action must be packed in an array before a ccsequence can be created. But now,

local sequence = transition.sequence({        CCMoveBy:create(1, ccp(250,0)),        CCMoveBy:create(1, ccp(0,50))    })

Just like C ++, you can create and add them one by one, which is very convenient ~


The above is all content.

Quick-cocos2d-x game development [8] -- animation and action

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.