Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – animating matches and collapses


Now for the last bit of logic: the final animations:

  1. We'll start with the easy ones:

    function GridAnimations:animateSelected (gem)
        gem:select()
        gem:stopAllActions()
        local rotate = cc.EaseBounceOut:create ( cc.RotateBy:create(0.5, 360) )
        gem:runAction(rotate)
    end

    This rotates a gem; we use this animation when a gem is first selected.

  2. Next is the swap animation:

    function GridAnimations:swapGems  (gemOrigin, gemTarget, onComplete)
        gemOrigin:deselect()
       local origin = self.gameLayer.selectedGemPosition
        local target = cc.p(gemTarget:getPositionX(),  gemTarget:getPositionY()) 
       local moveSelected =  cc.EaseBackOut:create(cc.MoveTo:create(0.8, target) )   
        local moveTarget =  cc.EaseBackOut:create(cc.MoveTo:create(0.8, origin) )
        local callback = cc.CallFunc:create(onComplete)
       gemOrigin:runAction(moveSelected)
        gemTarget:runAction (cc.Sequence:create(moveTarget, callback))
    end

    All this does is swap the places of...