CCSprite 实现书本翻页效果(quick cocos 2dx)

网上一查,都是用场景切换,我擦。。这么不优雅,不节约内存的方式。

只是去小卖铺买点酱油,结果动用了飞机去买。。。

代码实现了,主要是精灵的一个动作:

  1 local MainScene = class("MainScene", function()
  2     return display.newScene("MainScene")
  3 end)
  4 
  5 local firstPage
  6 local secondPage
  7 local layerOrder = 200
  8 
  9 local sharedScheduler = CCDirector:sharedDirector():getScheduler()
 10 
 11 function MainScene:ctor()
 12     --[[
 13     --这里注销的代码是当点击屏幕时,进行的翻页。
 14     self.layer = display.newLayer()
 15     self.layer:setTouchEnabled(true)
 16     
 17     self.layer:addTouchEventListener(function(event,x,y)
 18         self.layer:setTouchEnabled(false)
 19         if CommonUtils.curPage <= #CommonUtils.pagePathTable then    
 20             if CommonUtils.curPage % 2 == 1 then
 21                 layerOrder = layerOrder - 1
 22                 secondPage = display.newSprite("res/"..CommonUtils.pagePathTable[CommonUtils.curPage], display.cx, display.cy)
 23                 self:addChild(secondPage,layerOrder)
 24 
 25                 CommonUtils.curPage = CommonUtils.curPage + 1
 26                 local seq = CCSequence:createWithTwoActions(CCPageTurn3D:create(1, CCSizeMake(15,10)),CCCallFunc:create(function()
 27                         firstPage:removeSelf(true)
 28                         self.layer:setTouchEnabled(true)
 29                     end))
 30                 firstPage:runAction(seq)
 31             else
 32                 layerOrder = layerOrder - 1
 33                 firstPage = display.newSprite("res/"..CommonUtils.pagePathTable[CommonUtils.curPage], display.cx, display.cy)
 34                 self:addChild(firstPage,layerOrder)
 35 
 36                 CommonUtils.curPage = CommonUtils.curPage + 1
 37                 local seq = CCSequence:createWithTwoActions(CCPageTurn3D:create(1, CCSizeMake(15,10)),CCCallFunc:create(function()
 38                         secondPage:removeSelf(true)
 39                         self.layer:setTouchEnabled(true)
 40                     end))
 41                 secondPage:runAction(seq)
 42             end
 43         end
 44     end)
 45 
 46     self:addChild(self.layer)
 47 ]]
 48     --将配置文件读入,并且初始化
 49     CommonUtils.readFile()
 50     firstPage = display.newSprite("res/"..CommonUtils.pagePathTable[CommonUtils.curPage], display.cx, display.cy)
 51     CommonUtils.curPage = CommonUtils.curPage + 1
 52     self:addChild(firstPage,layerOrder)
 53 
 54     
 55 end
 56 
 57 function MainScene:onEnter()
 58     if device.platform == "android" then
 59         -- avoid unmeant back
 60         self:performWithDelay(function()
 61             -- keypad layer, for android
 62             local layer = display.newLayer()
 63             layer:addKeypadEventListener(function(event)
 64                 if event == "back" then app.exit() end
 65             end)
 66             self:addChild(layer)
 67 
 68             layer:setKeypadEnabled(true)
 69         end, 0.5)
 70     end 
 71     sharedScheduler:scheduleScriptFunc(function()
 72         self:turnPage()
 73     end, 1.2, false)
 74 end
 75 
 76 function MainScene:turnPage()
 77     if CommonUtils.curPage <= #CommonUtils.pagePathTable then    
 78         if CommonUtils.curPage % 2 == 0 then
 79             layerOrder = layerOrder - 1
 80             secondPage = display.newSprite("res/"..CommonUtils.pagePathTable[CommonUtils.curPage], display.cx, display.cy)
 81             self:addChild(secondPage,layerOrder)
 82 
 83             CommonUtils.curPage = CommonUtils.curPage + 1
 84             local seq = CCSequence:createWithTwoActions(CCPageTurn3D:create(1, CCSizeMake(15,10)),CCCallFunc:create(function()
 85                     firstPage:removeSelf(true)
 86                 end))
 87             firstPage:runAction(seq)
 88         else
 89             layerOrder = layerOrder - 1
 90             firstPage = display.newSprite("res/"..CommonUtils.pagePathTable[CommonUtils.curPage], display.cx, display.cy)
 91             self:addChild(firstPage,layerOrder)
 92 
 93             CommonUtils.curPage = CommonUtils.curPage + 1
 94             local seq = CCSequence:createWithTwoActions(CCPageTurn3D:create(1, CCSizeMake(15,10)),CCCallFunc:create(function()
 95                     secondPage:removeSelf(true)
 96                 end))
 97             secondPage:runAction(seq)
 98         end
 99     end
100 end
101 
102 function MainScene:onExit()
103 end
104 
105 return MainScene

。over~

posted @ 2014-04-25 16:11  Mr轨迹  阅读(5633)  评论(0编辑  收藏  举报