《Cocos2d-x-3.2 Lua-tests》文件詳解 之 動作Actions


動作是游戲最基本也是最主要的表現形式。

精准到位的動作能讓精靈具有更逼真的表現力,對增強游戲的可玩性起決定性的作用。

   1 --[[
   2 這里面的Action太豐富了
   3 首先介紹一下,什么是Action,Action就是精靈屬性的改變
   4 而屬性則包括:位置、尺寸、顏色、透明度、旋轉度、歪斜度等
   5 簡單的Action是單一改變某一種屬性
   6 復雜的Action,是組合地改變多種屬性
   7 
   8 當然還有多個Action的組合,那幾是Sequence和Spawn了
   9 還有視角(Camera)位置的變動,也屬於Action的一種
  10 下面慢慢介紹
  11 --]]
  12 
  13 
  14 --首先是布局,將能用到的東西都搬上來
  15 local size = cc.Director:getInstance():getWinSize()
  16 
  17 local function initWithLayer(layer)
  18     grossini = cc.Sprite:create(s_pPathGrossini)
  19     tamara   = cc.Sprite:create(s_pPathSister1)
  20     kathia   = cc.Sprite:create(s_pPathSister2)
  21 
  22     layer:addChild(grossini, 1)
  23     layer:addChild(tamara, 2)
  24     layer:addChild(kathia, 3)
  25 
  26     grossini:setPosition(cc.p(size.width / 2, size.height / 3))
  27     tamara:setPosition(cc.p(size.width / 2, 2 * size.height / 3))
  28     kathia:setPosition(cc.p(size.width / 2, size.height / 2))
  29 
  30     Helper.initWithLayer(layer)
  31 end
  32 --需要幾個精靈就顯示幾個精靈
  33 local function centerSprites(numberOfSprites)
  34     if numberOfSprites == 0 then
  35         tamara:setVisible(false)
  36         kathia:setVisible(false)
  37         grossini:setVisible(false)
  38     elseif numberOfSprites == 1 then
  39         tamara:setVisible(false)
  40         kathia:setVisible(false)
  41         grossini:setPosition(cc.p(size.width / 2, size.height / 2))
  42     elseif numberOfSprites == 2 then
  43         kathia:setPosition(cc.p(size.width / 3, size.height / 2))
  44         tamara:setPosition(cc.p(2 * size.width / 3, size.height / 2))
  45         grossini:setVisible(false)
  46     elseif numberOfSprites == 3 then
  47         grossini:setPosition(cc.p(size.width / 2, size.height / 2))
  48         tamara:setPosition(cc.p(size.width / 4, size.height / 2))
  49         kathia:setPosition(cc.p(3 * size.width / 4, size.height / 2))
  50     end
  51 end
  52 
  53 local function alignSpritesLeft(numberOfSprites)
  54     if numberOfSprites == 1 then
  55         tamara:setVisible(false)
  56         kathia:setVisible(false)
  57         grossini:setPosition(cc.p(60, size.height / 2))
  58     elseif numberOfSprites == 2 then
  59         kathia:setPosition(cc.p(60, size.height / 3))
  60         tamara:setPosition(cc.p(60, 2 * size.height / 3))
  61         grossini:setVisible(false)
  62     elseif numberOfSprites == 3 then
  63         grossini:setPosition(cc.p(60, size.height / 2))
  64         tamara:setPosition(cc.p(60, 2 * size.height / 3))
  65         kathia:setPosition(cc.p(60, size.height / 3))
  66     end
  67 end
  68 --------------------------------------
  69 -- ActionManual
  70 --------------------------------------
  71 --手動改變精靈的屬性,也是一種Action
  72 local function ActionManual()
  73     local layer = cc.Layer:create()
  74     initWithLayer(layer)
  75 
  76     tamara:setScaleX(2.5)
  77     tamara:setScaleY(-1.0)
  78     tamara:setPosition(cc.p(100, 70))
  79     tamara:setOpacity(128)
  80 
  81     grossini:setRotation(120)
  82     grossini:setPosition(cc.p(size.width / 2, size.height / 2))
  83     grossini:setColor(cc.c3b(255, 0, 0))
  84 
  85     kathia:setPosition(cc.p(size.width - 100, size.height / 2))
  86     kathia:setColor(cc.c3b(0, 0, 255))
  87 
  88     Helper.subtitleLabel:setString("Manual Transformation")
  89     return layer
  90 end
  91 --------------------------------------
  92 -- ActionMove
  93 --------------------------------------
  94 --MoveTo:使Sprite在設定時間內,移動到當前物體所在父容器的某一設定坐標上面
  95 --MoveBy:Sprite將傳進來的位置參數加到自己現在有位置上,形成新的位置,並移動到那里
  96 local function ActionMove()
  97     local layer = cc.Layer:create()
  98     initWithLayer(layer)
  99 
 100     centerSprites(3)
 101     local actionBy = cc.MoveBy:create(2, cc.p(80, 80))
 102     local actionByBack = actionBy:reverse()
 103 
 104     tamara:runAction(cc.MoveTo:create(2, cc.p(size.width - 40, size.height - 40)))
 105     grossini:runAction(cc.Sequence:create(actionBy, actionByBack))
 106 
 107     kathia:runAction(cc.MoveTo:create(1, cc.p(40, 40)))
 108 
 109     Helper.subtitleLabel:setString("MoveTo / MoveBy")
 110     return layer
 111 end
 112 --------------------------------------
 113 -- ActionScale
 114 --------------------------------------
 115 --ScaleTo接收兩個數值參數,一個是時間,一個是縮放的倍率
 116 --ScaleBy接收三個參數:時間,x軸倍率,y軸倍率。若 x倍率 = y倍率,則效果跟ScaleTo相同
 117 local function ActionScale()
 118     local layer = cc.Layer:create()
 119     initWithLayer(layer)
 120 
 121     centerSprites(3)
 122 
 123     local actionTo = cc.ScaleTo:create(2.0, 0.5)
 124     local actionBy = cc.ScaleBy:create(2.0, 1.0, 10.0)
 125     local actionBy2 = cc.ScaleBy:create(2.0, 5.0, 1.0)
 126 
 127     grossini:runAction(actionTo)
 128     tamara:runAction(cc.Sequence:create(actionBy, actionBy:reverse()))
 129     kathia:runAction(cc.Sequence:create(actionBy2, actionBy2:reverse()))
 130 
 131     Helper.subtitleLabel:setString("ScaleTo / ScaleBy")
 132     return layer
 133 end
 134 --------------------------------------
 135 -- ActionRotate
 136 --------------------------------------
 137 --RotateTo,以初始狀態為零角度,旋轉
 138 --RotateBy,以此次Action開始的狀態為零角度,旋轉
 139 local function ActionRotate()
 140     local layer = cc.Layer:create()
 141     initWithLayer(layer)
 142 
 143     centerSprites(3)
 144 
 145     local actionTo = cc.RotateTo:create( 2, 45)
 146     local actionTo2 = cc.RotateTo:create( 2, -45)
 147     local actionTo0 = cc.RotateTo:create(2 , 0)
 148     tamara:runAction(cc.Sequence:create(actionTo, actionTo0))
 149 
 150     local actionBy = cc.RotateBy:create(2 , 360)
 151     local actionByBack = actionBy:reverse()
 152     grossini:runAction(cc.Sequence:create(actionBy, actionByBack))
 153 
 154     local action0Retain = cc.RotateTo:create(2 , 0)
 155 
 156     kathia:runAction(cc.Sequence:create(actionTo2, action0Retain))
 157 
 158     Helper.subtitleLabel:setString("RotateTo / RotateBy")
 159     return layer
 160 end
 161 -------------------------------------
 162 -- ActionSkew
 163 --------------------------------------
 164 --SkewTo,參數:時間,Sprite的X軸旋轉角度,Sprite的Y軸旋轉角度 --以初始狀態為起點
 165 --SkewBy,參數:時間,Sprite的X軸旋轉角度,Sprite的Y軸旋轉角度 --以當前狀態為起點
 166 local function ActionSkew()
 167     local layer = cc.Layer:create()
 168     initWithLayer(layer)
 169 
 170     centerSprites(3)
 171 
 172     --local actionTo     = cc.SkewTo:create(20, 37.2, -37.2)
 173     local actionTo     = cc.SkewTo:create(1, 45, -45)
 174 
 175     local actionToBack = cc.SkewTo:create(2, 0, 0)
 176     local actionBy     = cc.SkewBy:create(2, 0.0, -90.0)
 177     local actionBy2    = cc.SkewBy:create(2, 45.0, 45.0)
 178     local actionByBack = actionBy:reverse()
 179 
 180     tamara:runAction(cc.Sequence:create(actionTo))
 181     --grossini:runAction(cc.SkewTo:create(1, 60, 0))
 182     grossini:runAction(cc.Sequence:create(actionBy, actionByBack))
 183     kathia:runAction(cc.Sequence:create(actionBy2, actionBy2:reverse()))
 184 
 185     Helper.subtitleLabel:setString("SkewTo / SkewBy")
 186     return layer
 187 end
 188 
 189 --ActionRotationalSkewVSStandardSkew
 190 local function ActionRotationalSkewVSStandardSkew()
 191 
 192     local layer = cc.Layer:create()
 193     initWithLayer(layer)
 194 
 195     tamara:removeFromParent(true);
 196     grossini:removeFromParent(true);
 197     kathia:removeFromParent(true);
 198 
 199     local s = cc.Director:getInstance():getWinSize();
 200     local boxSize = cc.size(100.0, 100.0);
 201     local box = cc.LayerColor:create(cc.c4b(255,255,0,255));
 202     box:setAnchorPoint(cc.p(0.5,0.5));
 203     box:setContentSize( boxSize );
 204     box:ignoreAnchorPointForPosition(false);
 205     box:setPosition(cc.p(s.width/2, s.height - 100 - box:getContentSize().height/2));
 206     layer:addChild(box);
 207 
 208     local label = cc.Label:createWithTTF("Standard cocos2d Skew", s_markerFeltFontPath, 16);
 209     label:setAnchorPoint(cc.p(0.5, 0.5))
 210     label:setPosition(cc.p(s.width/2, s.height - 100 + label:getContentSize().height));
 211     layer:addChild(label);
 212     local actionTo = cc.SkewBy:create(2, 360, 0);
 213     local actionToBack = cc.SkewBy:create(2, -360, 0);
 214     local seq = cc.Sequence:create(actionTo, actionToBack)
 215 
 216     box:runAction(seq);
 217 
 218     box = cc.LayerColor:create(cc.c4b(255,255,0,255));
 219     box:setAnchorPoint(cc.p(0.5,0.5));
 220     box:setContentSize(boxSize);
 221     box:ignoreAnchorPointForPosition(false);
 222     box:setPosition(cc.p(s.width/2, s.height - 250 - box:getContentSize().height/2));
 223     layer:addChild(box);
 224     label = cc.Label:createWithTTF("Rotational Skew", s_markerFeltFontPath, 16);
 225     label:setAnchorPoint(cc.p(0.5, 0.5))
 226     label:setPosition(cc.p(s.width/2, s.height - 250 + label:getContentSize().height/2));
 227     layer:addChild(label);
 228     local actionTo2 = cc.RotateBy:create(2, 360, 0);
 229     local actionToBack2 = cc.RotateBy:create(2, -360, 0);
 230     seq = cc.Sequence:create(actionTo2, actionToBack2)
 231     box:runAction(seq);
 232 
 233     Helper.subtitleLabel:setString("Skew Comparison")
 234     return layer;
 235 end
 236 
 237 --------------------------------------
 238 -- ActionSkewRotate
 239 --------------------------------------
 240 --
 241 --Action組合,Skew + Rotate + Scale
 242 local function ActionSkewRotate()
 243     local layer = cc.Layer:create()
 244     initWithLayer(layer)
 245 
 246     tamara:removeFromParent(true)
 247     grossini:removeFromParent(true)
 248     kathia:removeFromParent(true)
 249 
 250     local boxSize = cc.size(100.0, 100.0)
 251     --創建一個純色的Layer,尺寸為(100,100)
 252     local box = cc.LayerColor:create(cc.c4b(255, 255, 0, 255))
 253     box:setAnchorPoint(cc.p(0, 0))
 254     box:setPosition(190, 110)
 255     box:setContentSize(boxSize)
 256 
 257     --在左上角和右上角各放一個小方格(純色的Layer)
 258     local markrside = 10.0
 259     local uL = cc.LayerColor:create(cc.c4b(255, 0, 0, 255))
 260     box:addChild(uL)
 261     uL:setContentSize(cc.size(markrside, markrside))
 262     uL:setPosition(0, boxSize.height - markrside)
 263     uL:setAnchorPoint(cc.p(0, 0))
 264 
 265     local uR = cc.LayerColor:create(cc.c4b(0, 0, 255, 255))
 266     box:addChild(uR)
 267     uR:setContentSize(cc.size(markrside, markrside))
 268     uR:setPosition(boxSize.width - markrside, boxSize.height - markrside)
 269     uR:setAnchorPoint(cc.p(0, 0))
 270     layer:addChild(box)
 271 
 272     local actionTo = cc.SkewTo:create(2, 0, 2)
 273     local rotateTo = cc.RotateTo:create(2, 61.0)
 274     local actionScaleTo = cc.ScaleTo:create(2, -0.44, 0.47)
 275 
 276     local actionScaleToBack = cc.ScaleTo:create(2, 1.0, 1.0)
 277     local rotateToBack = cc.RotateTo:create(2, 0)
 278     local actionToBack = cc.SkewTo:create(2, 0, 0)
 279 
 280     box:runAction(cc.Sequence:create(actionTo, actionToBack))
 281     box:runAction(cc.Sequence:create(rotateTo, rotateToBack))
 282     box:runAction(cc.Sequence:create(actionScaleTo, actionScaleToBack))
 283 
 284     Helper.subtitleLabel:setString("Skew + Rotate + Scale")
 285     return layer
 286 end
 287 
 288 --------------------------------------
 289 -- ActionJump
 290 --------------------------------------
 291 --Jump測試
 292 --[[
 293   JumpTo,JumpBy的區別跟MoveTo與MoveBy的區別一樣,不多解釋
 294 --]]
 295 local function ActionJump()
 296     local layer = cc.Layer:create()
 297     initWithLayer(layer)
 298 
 299     centerSprites(3)
 300 
 301     local actionTo = cc.JumpTo:create(2, cc.p(300,300), 50, 4)
 302     local actionBy = cc.JumpBy:create(2, cc.p(300,0), 50, 4)
 303     local actionUp = cc.JumpBy:create(2, cc.p(0,0), 80, 4)
 304     local actionByBack = actionBy:reverse()
 305 
 306     tamara:runAction(actionTo)
 307     grossini:runAction(cc.Sequence:create(actionBy, actionByBack))
 308     kathia:runAction(cc.RepeatForever:create(actionUp))
 309 
 310     Helper.subtitleLabel:setString("JumpTo / JumpBy")
 311     return layer
 312 end
 313 
 314 --------------------------------------
 315 -- ActionCardinalSpline
 316 --------------------------------------
 317 --[[
 318         基樣線條 運動:用一組坐標定義一個線條,以次線條為基准,讓精靈(盡量)沿着線條運動
 319         參數:時間、坐標組、路線耦合度 - 暫時這么叫把,反正第三個參數為0的時候路線最符合基准線,並且光滑。越大路線越離譜。。。
 320 --]]
 321 local function ActionCardinalSpline()
 322     local layer = cc.Layer:create()
 323     initWithLayer(layer)
 324 
 325     centerSprites(2)
 326 
 327     local array = {
 328         cc.p(0, 0),
 329         cc.p(size.width / 2 - 30, 0),
 330         cc.p(size.width / 2 - 30, size.height - 80),
 331         cc.p(0, size.height - 80),
 332         cc.p(0, 0),
 333     }
 334     local action = cc.CardinalSplineBy:create(3, array, 0)
 335     local reverse = action:reverse()
 336     local seq = cc.Sequence:create(action, reverse)
 337 
 338     tamara:setPosition(cc.p(50, 50))
 339     tamara:runAction(seq)
 340 
 341     local action2 = cc.CardinalSplineBy:create(3, array, 1)
 342     local reverse2 = action2:reverse()
 343     local seq2 = cc.Sequence:create(action2, reverse2)
 344 
 345     kathia:setPosition(cc.p(size.width / 2, 50))
 346     kathia:runAction(seq2)
 347     --[[
 348     local function drawCardinalSpline()
 349     kmGLPushMatrix()
 350     kmGLTranslatef(50, 50, 0)
 351     cc.DrawPrimitives.drawCardinalSpline(array, 0, 100)
 352     kmGLPopMatrix()
 353 
 354     kmGLPushMatrix()
 355     kmGLTranslatef(size.width / 2, 50, 0)
 356     cc.DrawPrimitives.drawCardinalSpline(array, 1, 100)
 357     kmGLPopMatrix()
 358     end
 359 
 360     array:retain()
 361     local glNode  = gl.glNodeCreate()
 362     glNode:setContentSize(cc.size(size.width, size.height))
 363     glNode:setAnchorPoint(cc.p(0.5, 0.5))
 364     glNode:registerScriptDrawHandler(drawCardinalSpline)
 365     layer:addChild(glNode,-10)
 366     glNode:setPosition( size.width / 2, size.height / 2)
 367     ]]--
 368     Helper.titleLabel:setString("CardinalSplineBy / CardinalSplineAt")
 369     Helper.subtitleLabel:setString("Cardinal Spline paths.\nTesting different tensions for one array")
 370     return layer
 371 end
 372 
 373 --------------------------------------
 374 -- ActionCatmullRom
 375 --------------------------------------
 376 
 377 --To和By的區別就不一一解釋了,這個什么什么曲線,跟上面差不多,只接收兩個參數
 378 local function ActionCatmullRom()
 379     local layer = cc.Layer:create()
 380     initWithLayer(layer)
 381 
 382     centerSprites(2)
 383 
 384     tamara:setPosition(cc.p(50, 50))
 385     local array = {
 386         cc.p(0, 0),
 387         cc.p(80, 80),
 388         cc.p(size.width - 80, 80),
 389         cc.p(size.width - 80, size.height - 80),
 390         cc.p(80, size.height - 80),
 391         cc.p(80, 80),
 392         cc.p(size.width / 2, size.height / 2),
 393     }
 394 
 395     local action = cc.CatmullRomBy:create(3, array)
 396     local reverse = action:reverse()
 397     local seq = cc.Sequence:create(action, action)
 398     tamara:runAction(seq)
 399 
 400     local array2 = {
 401         cc.p(size.width / 2, 30),
 402         cc.p(size.width  -80, 30),
 403         cc.p(size.width - 80, size.height - 80),
 404         cc.p(size.width / 2, size.height - 80),
 405         cc.p(size.width / 2, 30),
 406     }
 407 
 408     local action2 = cc.CatmullRomTo:create(3, array2)
 409     local reverse2 = action2:reverse()
 410     local seq2 = cc.Sequence:create(action2, reverse2)
 411     kathia:runAction(action2)
 412 
 413     --[[
 414     local function drawCatmullRom()
 415     kmGLPushMatrix()
 416     kmGLTranslatef(50, 50, 0)
 417     cc.DrawPrimitives.drawCatmullRom(array, 50)
 418     kmGLPopMatrix()
 419 
 420     cc.DrawPrimitives.drawCatmullRom(array2,50)
 421     end
 422 
 423     array:retain()
 424     array2:retain()
 425     local glNode  = gl.glNodeCreate()
 426     glNode:setContentSize(cc.size(size.width, size.height))
 427     glNode:setAnchorPoint(cc.p(0.5, 0.5))
 428     glNode:registerScriptDrawHandler(drawCatmullRom)
 429     layer:addChild(glNode,-10)
 430     glNode:setPosition( size.width / 2, size.height / 2)
 431     ]]--
 432 
 433     Helper.titleLabel:setString("CatmullRomBy / CatmullRomTo")
 434     Helper.subtitleLabel:setString("Catmull Rom spline paths. Testing reverse too")
 435     return layer
 436 end
 437 
 438 --------------------------------------
 439 -- ActionBezier
 440 --------------------------------------
 441 --貝塞爾運動
 442 local function ActionBezier()
 443     local layer = cc.Layer:create()
 444     initWithLayer(layer)
 445 
 446     centerSprites(3)
 447 
 448     -- sprite 1
 449     --[[
 450     local bezier = ccBezierConfig()
 451     bezier.controlPoint_1 = cc.p(0, size.height / 2)
 452     bezier.controlPoint_2 = cc.p(300, - size.height / 2)
 453     bezier.endPosition = cc.p(300, 100)
 454     ]]--
 455     --首先創建貝塞爾曲線所需要的點
 456     local bezier = {
 457         cc.p(0, size.height / 2),
 458         cc.p(300, - size.height / 2),
 459         cc.p(300, 100),
 460     }
 461     --創建貝塞爾運動,接收兩個參數:時間、坐標集
 462     local bezierForward = cc.BezierBy:create(3, bezier)
 463     local bezierBack = bezierForward:reverse()
 464     local rep = cc.RepeatForever:create(cc.Sequence:create(bezierForward, bezierBack))
 465 
 466     -- sprite 2
 467     tamara:setPosition(cc.p(80,160))
 468     --[[
 469     local bezier2 = ccBezierConfig()
 470     bezier2.controlPoint_1 = cc.p(100, size.height / 2)
 471     bezier2.controlPoint_2 = cc.p(200, - size.height / 2)
 472     bezier2.endPosition = cc.p(240, 160)
 473     ]]--
 474     local bezier2 ={
 475         cc.p(100, size.height / 2),
 476         cc.p(200, - size.height / 2),
 477         cc.p(240, 160)
 478     }
 479 
 480     local bezierTo1 = cc.BezierTo:create(2, bezier2)
 481 
 482     -- sprite 3
 483     kathia:setPosition(cc.p(400,160))
 484     local bezierTo2 = cc.BezierTo:create(2, bezier2)
 485 
 486     grossini:runAction(rep)
 487     -- tamara:runAction(bezierTo1)
 488     -- kathia:runAction(bezierTo2)
 489 
 490     Helper.subtitleLabel:setString("BezierTo / BezierBy")
 491     return layer
 492 end
 493 
 494 --------------------------------------
 495 -- ActionBlink
 496 --------------------------------------
 497 --閃爍,參數:時間,次數
 498 local function ActionBlink()
 499     local layer = cc.Layer:create()
 500     initWithLayer(layer)
 501 
 502     centerSprites(2)
 503 
 504     local action1 = cc.Blink:create(2, 50)
 505     local action2 = cc.Blink:create(2, 5)
 506 
 507     tamara:runAction(action1)
 508     kathia:runAction(action2)
 509 
 510     Helper.subtitleLabel:setString("Blink")
 511 
 512     return layer
 513 end
 514 
 515 --------------------------------------
 516 -- ActionFade
 517 --------------------------------------
 518 --FadeIn:淡入 - 從無慢慢變到有 , 接收一個參數:時間
 519 -- FadeOut:淡出 - 從有慢慢變到無, 接收一個時間參數
 520 local function ActionFade()
 521     local layer = cc.Layer:create()
 522     initWithLayer(layer)
 523 
 524     centerSprites(2)
 525 
 526     tamara:setOpacity(0)
 527     local action1 = cc.FadeIn:create(5)
 528     local action1Back = action1:reverse()
 529 
 530     local action2 = cc.FadeOut:create(5)
 531     local action2Back = action2:reverse()
 532 
 533     tamara:runAction(cc.Sequence:create( action1))
 534     kathia:runAction(cc.Sequence:create(action2))
 535 
 536     Helper.subtitleLabel:setString("FadeIn / FadeOut")
 537 
 538     return layer
 539 end
 540 
 541 --------------------------------------
 542 -- ActionTint
 543 --------------------------------------
 544 --色調的改變,by和to的不同,不多解釋
 545 local function ActionTint()
 546     local layer = cc.Layer:create()
 547     initWithLayer(layer)
 548 
 549     centerSprites(2)
 550 
 551     local action1 = cc.TintTo:create(2, 255, 0, 255)
 552     local action2 = cc.TintBy:create(2, -127, -255, -127)
 553     local action2Back = action2:reverse()
 554 
 555     tamara:runAction(action1)
 556     kathia:runAction(cc.Sequence:create(action2, action2Back))
 557 
 558     Helper.subtitleLabel:setString("TintTo / TintBy")
 559 
 560     return layer
 561 end
 562 
 563 --------------------------------------
 564 -- ActionAnimate
 565 --------------------------------------
 566 --逐幀動畫,動作的一種,可以跟精靈綁定,當播放動畫的時候,會覆蓋精靈
 567 local function ActionAnimate()
 568     local layer = cc.Layer:create()
 569     initWithLayer(layer)
 570 
 571     centerSprites(3)
 572 
 573     local animation = cc.Animation:create()
 574     local number, name
 575 
 576     --FOR循環,加載規律命名的每一幀
 577     for i = 1, 14 do
 578         if i < 10 then
 579             number = "0"..i
 580         else
 581             number = i
 582         end
 583         name = "Images/grossini_dance_"..number..".png"
 584         --將每一幀放到animation中,方法:addSpriteFrameWithFile()
 585         animation:addSpriteFrameWithFile(name)
 586     end
 587     -- should last 2.8 seconds. And there are 14 frames.
 588 
 589     --setDelayPerUnit()設置每一幀的時間
 590     animation:setDelayPerUnit(2.8 / 14.0)
 591     --是否保存初始幀,即動畫播放完之后,是顯示動畫的最后一幀,還是顯示動畫之前的Sprite
 592     animation:setRestoreOriginalFrame(true)
 593 
 594 
 595     --將Animation封裝到Aniamte中,形成幀動作,可以與Sprite綁定
 596     local action = cc.Animate:create(animation)
 597     --grossini:runAction(cc.Sequence:create(action, action:reverse()))
 598 
 599     --cc.AnimationCache:getInstance():addAnimations("animations/animations-2.plist")
 600     --將動畫文件加載到緩存中
 601     local cache = cc.AnimationCache:getInstance()
 602     cache:addAnimations("animations/animations-2.plist")
 603     --local animation2 = cc.AnimationCache:getInstance:getAnimation("dance_1") 挑出名為“dance_1”的動畫
 604     local animation2 = cache:getAnimation("dance_1")
 605 
 606     --將Animation2封裝到Animate中,成為幀動作,可與Sprite綁定
 607     local action2 = cc.Animate:create(animation2)
 608     --tamara:runAction(cc.Sequence:create(action2, action2:reverse()))
 609 
 610     --clone(),賦值action2
 611     local animation3 = animation2:clone()
 612     --循環播放四次
 613     animation3:setLoops(1)
 614     --這里設置為false的話,將顯示動畫的最后一幀,不顯示原始的kathia了
 615     animation3:setRestoreOriginalFrame(false)
 616 
 617     local action3 = cc.Animate:create(animation3)
 618     kathia:runAction(action3)
 619 
 620     Helper.titleLabel:setString("Animation")
 621     Helper.subtitleLabel:setString("Center: Manual animation. Border: using file format animation")
 622 
 623     return layer
 624 
 625         --[[
 626         現在回憶一下幀動作:
 627         1、創建逐幀動畫Animation,用addSpriteFrameWithFile(name)給動畫添加每一幀。
 628         2、創建Animate,將Animation封裝成動作,能夠與Sprite綁定的動作,對Sprite調用潤Action()綁定Animate即可播放
 629 
 630         第二種創建幀動畫的方式:
 631         1、local animation = cc.AnimationCache:getInstance:getAnimation("XXX") - 加載幀動畫文件對應的plist文件
 632         2、創建Animate,將Animation封裝成動作
 633 
 634         可直接用clone()復制動畫
 635 
 636         另外:Animation的setLoops()方法設置動畫的循環次數
 637         
 638         我們可以這樣理解Animate和Animation的區別:
 639     Animation只是純粹的動畫,還不是Sprite能夠跑的Action,需要將Animation用cc.Animate:create()封裝成Animate,Animate才是真正的Action,才能供Sprite使用
 640         --]]
 641 end
 642 
 643 --------------------------------------
 644 -- ActionSequence
 645 --------------------------------------
 646 --動作序列Sequence,前面提到過,從略
 647 local function ActionSequence()
 648     local layer = cc.Layer:create()
 649     initWithLayer(layer)
 650 
 651     alignSpritesLeft(1)
 652 
 653     local action = cc.Sequence:create(
 654         cc.MoveBy:create(2, cc.p(240,0)),
 655         cc.RotateBy:create(2, 540))
 656 
 657     grossini:runAction(action)
 658 
 659     Helper.subtitleLabel:setString("Sequence: Move + Rotate")
 660 
 661     return layer
 662 end
 663 
 664 --------------------------------------
 665 -- ActionSequence2
 666 --------------------------------------
 667 
 668 --Sequence里面接回調,上面講到過,從略
 669 local actionSequenceLayer = nil
 670 
 671 local function ActionSequenceCallback1()
 672     local label = cc.Label:createWithTTF("callback 1 called", s_markerFeltFontPath, 16)
 673     label:setAnchorPoint(cc.p(0.5, 0.5))
 674     label:setPosition(size.width / 4, size.height / 2)
 675 
 676     actionSequenceLayer:addChild(label)
 677 end
 678 
 679 local function ActionSequenceCallback2(sender)
 680     local label = cc.Label:createWithTTF("callback 2 called", s_markerFeltFontPath, 16)
 681     label:setAnchorPoint(cc.p(0.5, 0.5))
 682     label:setPosition(cc.p(size.width / 4 * 2, size.height / 2))
 683 
 684     actionSequenceLayer:addChild(label)
 685 end
 686 
 687 local function ActionSequenceCallback3(sender)
 688     local label = cc.Label:createWithTTF("callback 3 called", s_markerFeltFontPath, 16)
 689     label:setAnchorPoint(cc.p(0.5, 0.5))
 690     label:setPosition(cc.p(size.width / 4 * 3, size.height / 2))
 691 
 692     actionSequenceLayer:addChild(label)
 693 end
 694 
 695 local function ActionSequence2()
 696     actionSequenceLayer = cc.Layer:create()
 697     initWithLayer(actionSequenceLayer)
 698 
 699     alignSpritesLeft(1)
 700 
 701     grossini:setVisible(false)
 702     local action = cc.Sequence:create(cc.Place:create(cc.p(200,200)),cc.Show:create(),cc.MoveBy:create(1, cc.p(100,0)), cc.CallFunc:create(ActionSequenceCallback1),cc.CallFunc:create(ActionSequenceCallback2),cc.CallFunc:create(ActionSequenceCallback3))
 703 
 704     grossini:runAction(action)
 705 
 706     Helper.subtitleLabel:setString("Sequence of InstantActions")
 707     return actionSequenceLayer
 708 end
 709 
 710 --------------------------------------
 711 -- ActionSpawn
 712 --------------------------------------
 713 --兩種動作同時進行
 714 local function ActionSpawn()
 715     local layer = cc.Layer:create()
 716     initWithLayer(layer)
 717 
 718     alignSpritesLeft(1)
 719 
 720     local action = cc.Spawn:create(
 721         cc.JumpBy:create(2, cc.p(300,0), 50, 4),
 722         cc.RotateBy:create( 2,  720))
 723 
 724     grossini:runAction(action)
 725 
 726     Helper.subtitleLabel:setString("Spawn: Jump + Rotate")
 727 
 728     return layer
 729 end
 730 
 731 --------------------------------------
 732 -- ActionReverse
 733 --------------------------------------
 734 --逆動作Action:reverse()
 735 local function ActionReverse()
 736     local layer = cc.Layer:create()
 737     initWithLayer(layer)
 738 
 739     alignSpritesLeft(1)
 740 
 741     local jump = cc.JumpBy:create(2, cc.p(300,0), 50, 4)
 742     local action = cc.Sequence:create(jump, jump:reverse())
 743 
 744     grossini:runAction(action)
 745 
 746     Helper.subtitleLabel:setString("Reverse an action")
 747 
 748     return layer
 749 end
 750 
 751 --------------------------------------
 752 -- ActionDelaytime
 753 --------------------------------------
 754 --延時,cc.DelayTime:create(X),可放入Sequence中
 755 local function ActionDelaytime()
 756     local layer = cc.Layer:create()
 757     initWithLayer(layer)
 758 
 759     alignSpritesLeft(1)
 760 
 761     local move = cc.MoveBy:create(1, cc.p(150,0))
 762     local action = cc.Sequence:create(move, cc.DelayTime:create(2), move)
 763 
 764     grossini:runAction(action)
 765 
 766     Helper.subtitleLabel:setString("DelayTime: m + delay + m")
 767     return layer
 768 end
 769 
 770 --------------------------------------
 771 -- ActionRepeat
 772 --------------------------------------
 773 local function ActionRepeat()
 774     local layer = cc.Layer:create()
 775     initWithLayer(layer)
 776 
 777     alignSpritesLeft(2)
 778 
 779     local a1 = cc.MoveBy:create(1, cc.p(150,0))
 780     --cc.Repeat:create(action,X) - 執行action , X次
 781     --cc.Place:create(cc.p(x,y),action) 用來將action放到某一個位置 - 位置的改變和設置也是動作的一種!!
 782     local action1 = cc.Repeat:create(cc.Sequence:create(cc.Place:create(cc.p(160,160)), a1), 3)
 783 
 784     local a2 = cc.MoveBy:create(1, cc.p(150,0))
 785     local action2 = cc.RepeatForever:create(cc.Sequence:create(a2, a1:reverse()))
 786 
 787     kathia:runAction(action1)
 788     tamara:runAction(action2)
 789 
 790     Helper.subtitleLabel:setString("Repeat / RepeatForever actions")
 791     return layer
 792 end
 793 
 794 --------------------------------------
 795 -- ActionRepeatForever
 796 --------------------------------------
 797 --無限循環下去,cc.RepeatForever:create(Action)
 798 local function repeatForever(sender)
 799     local repeatAction = cc.RepeatForever:create(cc.RotateBy:create(0.1, 360))
 800 
 801     --此處的sender是grossini
 802     sender:runAction(repeatAction)
 803 end
 804 
 805 local function ActionRepeatForever()
 806     local layer = cc.Layer:create()
 807     initWithLayer(layer)
 808 
 809     centerSprites(1)
 810 
 811     local action = cc.Sequence:create(
 812         cc.DelayTime:create(1),
 813         cc.CallFunc:create(repeatForever) )
 814 
 815     grossini:runAction(action)
 816 
 817     Helper.subtitleLabel:setString("CallFuncN + RepeatForever")
 818     return layer
 819 end
 820 
 821 --------------------------------------
 822 -- ActionRotateToRepeat
 823 --------------------------------------
 824 local function ActionRotateToRepeat()
 825     local layer = cc.Layer:create()
 826     initWithLayer(layer)
 827 
 828     centerSprites(2)
 829 
 830     local act1 = cc.RotateTo:create(1, 90)
 831     local act2 = cc.RotateTo:create(1, 0)
 832     local seq  = cc.Sequence:create(act1, act2)
 833     --無限循環
 834     local rep1 = cc.RepeatForever:create(seq)
 835     --循環十次
 836     local rep2 = cc.Repeat:create(seq:clone(), 10)
 837 
 838     tamara:runAction(rep1)
 839     kathia:runAction(rep2)
 840 
 841     Helper.subtitleLabel:setString("Repeat/RepeatForever + RotateTo")
 842 
 843     return layer
 844 end
 845 
 846 --------------------------------------
 847 -- ActionRotateJerk
 848 --------------------------------------
 849 local function ActionRotateJerk()
 850     local layer = cc.Layer:create()
 851     initWithLayer(layer)
 852 
 853     centerSprites(2)
 854 
 855     local seq = cc.Sequence:create(
 856         cc.RotateTo:create(0.5, -20),
 857         cc.RotateTo:create(0.5, 20))
 858 
 859     local rep1 = cc.Repeat:create(seq, 10)
 860 
 861     local seq2 = cc.Sequence:create(
 862         cc.RotateTo:create(0.5, -20),
 863         cc.RotateTo:create(0.5, 20))
 864 
 865     local rep2 = cc.RepeatForever:create(seq2)
 866 
 867     tamara:runAction(rep1)
 868     kathia:runAction(rep2)
 869 
 870     Helper.subtitleLabel:setString("RepeatForever / Repeat + Rotate")
 871     return layer
 872 end
 873 
 874 --------------------------------------
 875 -- ActionCallFunc
 876 --------------------------------------
 877 --Sequence接回調
 878 local callFuncLayer = nil
 879 
 880 local function CallFucnCallback1()
 881     local label = cc.Label:createWithTTF("callback 1 called", s_markerFeltFontPath, 16)
 882     label:setAnchorPoint(cc.p(0.5, 0.5))
 883     label:setPosition(size.width / 4, size.height / 2)
 884 
 885     callFuncLayer:addChild(label)
 886 end
 887 
 888 local function CallFucnCallback2(sender)
 889     local label = cc.Label:createWithTTF("callback 2 called", s_markerFeltFontPath, 16)
 890     label:setAnchorPoint(cc.p(0.5, 0.5))
 891     label:setPosition(size.width / 4 * 2, size.height / 2)
 892 
 893     callFuncLayer:addChild(label)
 894 end
 895 
 896 local function CallFucnCallback3(sender)
 897     local label = cc.Label:createWithTTF("callback 3 called", s_markerFeltFontPath, 16)
 898     label:setAnchorPoint(cc.p(0.5, 0.5))
 899     label:setPosition(size.width / 4 * 3, size.height / 2)
 900 
 901     callFuncLayer:addChild(label)
 902 end
 903 
 904 local function ActionCallFunc()
 905     callFuncLayer = cc.Layer:create()
 906     initWithLayer(callFuncLayer)
 907 
 908     centerSprites(3)
 909 
 910     local action = cc.Sequence:create(
 911         cc.MoveBy:create(2, cc.p(200,0)),
 912         cc.CallFunc:create(CallFucnCallback1) )
 913     local action2 = cc.Sequence:create(cc.ScaleBy:create(2, 2),cc.FadeOut:create(2),cc.CallFunc:create(CallFucnCallback2))
 914     local action3 = cc.Sequence:create(cc.RotateBy:create(3 , 360),cc.FadeOut:create(2),cc.CallFunc:create(CallFucnCallback3))
 915 
 916     grossini:runAction(action)
 917     tamara:runAction(action2)
 918     kathia:runAction(action3)
 919 
 920     Helper.subtitleLabel:setString("Callbacks: CallFunc and friends")
 921     return callFuncLayer
 922 end
 923 
 924 --------------------------------------
 925 -- ActionCallFuncND *
 926 --   problem: the current luaEngine doesn't support
 927 --   passing more than one param to lua script
 928 --------------------------------------
 929 local function ActionCallFuncND()
 930     local layer = cc.Layer:create()
 931     initWithLayer(layer)
 932 
 933     centerSprites(1)
 934 
 935     local function doRemoveFromParentAndCleanup(sender,table)
 936         --移除並清除掉
 937         grossini:removeFromParentAndCleanup(table[1])
 938     end
 939 
 940     local action = cc.Sequence:create(
 941         cc.MoveBy:create(2, cc.p(200,0)),
 942         cc.CallFunc:create(doRemoveFromParentAndCleanup,{true}))
 943 
 944     grossini:runAction(action)
 945 
 946     Helper.titleLabel:setString("CallFuncND + auto remove")
 947     Helper.subtitleLabel:setString("CallFuncND + removeFromParent. Grossini dissapears in 2s")
 948     return layer
 949 end
 950 
 951 --------------------------------------
 952 -- ActionReverseSequence
 953 --------------------------------------
 954 --將Sequence放到Sequence中
 955 local function ActionReverseSequence()
 956     local layer = cc.Layer:create()
 957     initWithLayer(layer)
 958 
 959     alignSpritesLeft(1)
 960 
 961     local move1  = cc.MoveBy:create(1, cc.p(250,0))
 962     local move2  = cc.MoveBy:create(1, cc.p(0,50))
 963     local seq    = cc.Sequence:create(move1, move2, move1:reverse())
 964     local action = cc.Sequence:create(seq, seq:reverse())
 965 
 966     grossini:runAction(action)
 967 
 968     Helper.subtitleLabel:setString("Reverse a sequence")
 969     return layer
 970 end
 971 
 972 --------------------------------------
 973 -- ActionReverseSequence2
 974 --------------------------------------
 975 local function ActionReverseSequence2()
 976     local layer = cc.Layer:create()
 977     initWithLayer(layer)
 978 
 979     alignSpritesLeft(2)
 980 
 981     -- Test:
 982     -- Sequence should work both with IntervalAction and InstantActions
 983     local move1  = cc.MoveBy:create(1, cc.p(250,0))
 984     local move2  = cc.MoveBy:create(1, cc.p(0,50))
 985     --可視性切換,調用的時候,與上一個動作的可見性相反
 986     local tog1 = cc.ToggleVisibility:create()
 987     local tog2 = cc.ToggleVisibility:create()
 988     --local seq = cc.Sequence:create(move1, tog1, move2, tog2, move1:reverse())
 989     local seq = cc.Sequence:create(tog1,move1, tog2)
 990 
 991     local action = cc.Repeat:create(cc.Sequence:create(seq, seq:reverse()), 3)
 992 
 993     -- Test:
 994     -- Also test that the reverse of Hide is Show, and vice-versa
 995     kathia:runAction(action)
 996 
 997     local move_tamara = cc.MoveBy:create(1, cc.p(100,0))
 998     local move_tamara2 = cc.MoveBy:create(1, cc.p(50,0))
 999     local hide = cc.Hide:create()
1000 
1001     local seq_tamara = cc.Sequence:create(move_tamara, hide, move_tamara2)
1002     local seq_back = seq_tamara:reverse()
1003     -- tamara:runAction(cc.Sequence:create(seq_tamara, seq_back))
1004 
1005     Helper.subtitleLabel:setString("Reverse a sequence2")
1006     return layer
1007 end
1008 
1009 --------------------------------------
1010 -- ActionOrbit
1011 --------------------------------------
1012 --視角動作 - 簡單的說就是將Sprite與相機綁定,然后相機圍繞着Sprite轉
1013 local function ActionOrbit()
1014     local layer = cc.Layer:create()
1015     initWithLayer(layer)
1016 
1017     centerSprites(3)
1018     --[[
1019     cc.OrbitCamera:create(),參數詳解:
1020     1、時間       :與其他Action一樣,第一個參數都是時間
1021     2、初始半徑:OrbitCamera設定相機在以綁定的Sprite為球心的求面上運動的,所以有個初始半徑
1022     3、半徑差    :半徑差大於0的話,相機會跨越不同的球面,這樣看起來,Sprite就會變大或變小
1023     4、起始  z角:Sprite處於三維坐標系的原點,相機位置與原點的連線與yz面的夾角稱為z角,
1024     5、z角差     :z角改變180度,相當於從Sprite的正前方,繞到它的正后方
1025     6、起始  x角:Sprite處於三維坐標系的原點,相機位置與原點的連線與xz面的夾角成為x角,z角表示你俯視Sprite的角度
1026     7、x角差     :起始x角為0度的時候,x角差表示你的視角與水平線的夾角
1027     
1028        弄清楚了參數代表的意義,就可以隨意定義相機的視角了
1029       比如(5,10,0,90,180,0,0)表示,用5秒鍾,在距離Sprite為10的球面上,從Sprite底部,沿着yx面與球面的交線,經過正前面繞到頂部。
1030       看不懂的自己畫個圖比划一下
1031     --]]
1032     local orbit1 = cc.OrbitCamera:create(2,1, 0, 0, 180, 0, 45)
1033 
1034     local action1 = cc.Sequence:create(orbit1, orbit1:reverse())
1035 
1036     local orbit2 = cc.OrbitCamera:create(2,1, 0, 0, 180, -45, 0)
1037     local action2 = cc.Sequence:create(orbit2, orbit2:reverse())
1038 
1039     local orbit3 = cc.OrbitCamera:create(2,1, 0, 0, 180, 90, 0)
1040     local action3 = cc.Sequence:create(orbit3, orbit3:reverse())
1041 
1042     kathia:runAction(cc.RepeatForever:create(action1))
1043     --kathia:runAction(cc.Repeat:create(orbit1,2))
1044 
1045     tamara:runAction(cc.RepeatForever:create(action2))
1046     grossini:runAction(cc.RepeatForever:create(action3))
1047 
1048     local move = cc.MoveBy:create(3, cc.p(100,-100))
1049     local move_back = move:reverse()
1050     local seq = cc.Sequence:create(move, move_back)
1051     local rfe = cc.RepeatForever:create(seq)
1052     kathia:runAction(rfe)
1053     tamara:runAction(rfe:clone())
1054     grossini:runAction(rfe:clone())
1055 
1056 
1057     Helper.subtitleLabel:setString("OrbitCamera action")
1058     return layer
1059 end
1060 
1061 --------------------------------------
1062 -- ActionFollow
1063 --------------------------------------
1064 --[[
1065     上面講到的是軌跡相機,也就是視角相機,每一個精靈都有一個軌跡相機跟隨,軌跡相機從精靈上取景,各個軌跡相機需要將各自取的景呈現在主相機上,才能被渲染被看到。
1066     
1067     跟隨相機指的就是主相機,主相機的視角決定了我們能從窗口看到的東西和范圍。主相機可以是自由視角、也可以用cc.Follow:create(Sprite,cc,rect(x,x,x,x))跟隨一個運動的Sprite
1068 --]]
1069 local function ActionFollow()
1070     local layer = cc.Layer:create()
1071     initWithLayer(layer)
1072 
1073     centerSprites(1)
1074     --跟隨相機
1075     --首先,你需要一個Sprite
1076     grossini:setPosition(cc.p(-200, size.height / 2))
1077     local move = cc.MoveBy:create(2, cc.p(size.width * 3, 0))
1078     local move_back = move:reverse()
1079     local seq = cc.Sequence:create(move, move_back)
1080     local rep = cc.RepeatForever:create(seq)
1081     --Sprite需要動起來
1082     grossini:runAction(rep)
1083     --cc.Follow:create(Sprite,cc.rect(x,x,x,x)),需要設置目標,和跟隨的范圍,也就是超出這個范圍,相機就不會跟過去了。。
1084     --相機跟隨是整個層上的事情,所以要對Layer調用runAction()
1085     layer:runAction(cc.Follow:create(grossini, cc.rect(0, 0, size.width * 2 - 100, size.height)))
1086     
1087     --下面是調用繪圖API將范圍畫出來,繪圖API在后面有專門講解
1088     local function draw()
1089         local winSize = cc.Director:getInstance():getWinSize()
1090         local x = winSize.width * 2 - 100
1091         local y = winSize.height
1092         local vertices = { cc.p(5, 5), cc.p(x - 5, 5), cc.p(x - 5,y - 5), cc.p(5,y - 5) }
1093         cc.DrawPrimitives.drawPoly(vertices, 4, true)
1094     end
1095 
1096     local glNode  = gl.glNodeCreate()
1097     glNode:setContentSize(cc.size(size.width, size.height))
1098     glNode:setAnchorPoint(cc.p(0.5, 0.5))
1099     glNode:registerScriptDrawHandler(draw)
1100     layer:addChild(glNode,-10)
1101     glNode:setPosition( size.width / 2, size.height / 2)
1102 
1103     Helper.subtitleLabel:setString("Follow action")
1104     return layer
1105     
1106     --[[
1107         總結一下,跟隨相機比軌跡相機簡單多了:
1108         1、跟隨相機攝取的內容就是我們在窗口上看到的內容,僅有一個~!!
1109         2、跟隨相機需要用Layer的runAction()方法調用,而不是用Sprite的
1110         3、跟隨動作cc.Follow:create()需要一個運動的Sprite,和一個cc.rect:create()定義的范圍做參數。
1111     --]]
1112 end
1113 
1114 --------------------------------------
1115 -- ActionTargeted
1116 --------------------------------------
1117 --cc.TargetedAction:create(Sprite,Action),繼承自Action,用於將Sprite與Action綁定並實施的。
1118 --這樣做很靈活,比如游戲中,當一個BOSS揮一揮手發號施令的時候,小怪怪會做出相應的響應,
1119 --這時候直接對BOSS runAction()一個帶TargetedAction的Sequence,此TargetedAction會給小怪綁定一個動作,小怪就會“自動響應”。
1120 --為碼農節省了很多if-else
1121 local function ActionTargeted()
1122     local layer = cc.Layer:create()
1123     initWithLayer(layer)
1124 
1125     centerSprites(2)
1126 
1127     local jump1 = cc.JumpBy:create(2, cc.p(0, 0), 100, 3)
1128     local jump2 = cc.JumpBy:create(2, cc.p(0, 0), 100, 3)
1129     local rot1  = cc.RotateBy:create(1, 360)
1130     local rot2  = cc.RotateBy:create(1, 360)
1131     
1132     --Action → cc.TargetedAction:create(Sprite,Action)好好感受一下
1133     local t1 = cc.TargetedAction:create(kathia, jump2)
1134     local t2 = cc.TargetedAction:create(kathia, rot2)
1135     --local seq = cc.Sequence:create(jump1, t1, rot1, t2)
1136     
1137     --想想,如果要兩個精靈同時執行,怎么辦? 猜對了,用Spawn
1138     --當tamara運行到t1的時候,意思就是,我該去告訴kathia,讓他按照jump2動起來,並且tamara要監視kathia保質保量完成動作。
1139     local always = cc.RepeatForever:create(cc.Sequence:create(jump1, t1, rot1, t2))
1140 
1141     tamara:runAction(always)
1142 
1143     Helper.titleLabel:setString("ActionTargeted")
1144     Helper.subtitleLabel:setString("Action that runs on another target. Useful for sequences")
1145     return layer
1146 end
1147 
1148 --------------------------------------
1149 -- PauseResumeActions *
1150 --   problem: schedule feature is constructing
1151 --------------------------------------
1152 --[[
1153     Action的暫停和恢復
1154     前面提到過,所有的動作的管理,都由Director下屬的各種Manager管理
1155     這里test里面用到了腳本調度器,整體路線是:先讓Sprite執行Action,每過3秒鍾,用ActionManager()的PauseResumeActions()暫停一下所有正在進行的Action
1156     第五秒鍾的時候,又調用resumeTargets(),恢復當前Director所管理的所有Action
1157     
1158     這里提一下resumeTargets(),它接收的參數是單例Director,只恢復這個Director之前pause的Action
1159 --]]
1160 local pausedTargets = nil
1161 local PauseResumeActions_pauseEntry = nil
1162 local PauseResumeActions_resumeEntry = nil
1163 
1164 --暫停游戲
1165 local function ActionPause(dt)
1166     cclog("Pausing")
1167 
1168     local scheduler = cc.Director:getInstance():getScheduler()
1169     scheduler:unscheduleScriptEntry(PauseResumeActions_pauseEntry)
1170 
1171     local director = cc.Director:getInstance()
1172     pausedTargets = director:getActionManager():pauseAllRunningActions()
1173 end
1174 
1175 --恢復游戲
1176 local function ActionResume(dt)
1177     cclog("Resuming")
1178 
1179     local scheduler = cc.Director:getInstance():getScheduler()
1180     scheduler:unscheduleScriptEntry(PauseResumeActions_resumeEntry)
1181 
1182     local director = cc.Director:getInstance()
1183     if pausedTargets ~= nil then
1184         -- problem: will crash here. Try fixing me!
1185         director:getActionManager():resumeTargets(pausedTargets)
1186     end
1187 end
1188 
1189 local function PauseResumeActions_onEnterOrExit(tag)
1190     local scheduler = cc.Director:getInstance():getScheduler()
1191     if tag == "enter" then
1192         PauseResumeActions_pauseEntry = scheduler:scheduleScriptFunc(ActionPause, 3, false)
1193         PauseResumeActions_resumeEntry = scheduler:scheduleScriptFunc(ActionResume, 5, false)
1194     elseif tag == "exit" then
1195         scheduler:unscheduleScriptEntry(PauseResumeActions_pauseEntry)
1196         scheduler:unscheduleScriptEntry(PauseResumeActions_resumeEntry)
1197     end
1198 end
1199 
1200 local function PauseResumeActions()
1201     local layer = cc.Layer:create()
1202     initWithLayer(layer)
1203 
1204     centerSprites(2)
1205 
1206     tamara:runAction(cc.RepeatForever:create(cc.RotateBy:create(3, 360)))
1207     kathia:runAction(cc.RepeatForever:create(cc.RotateBy:create(3, 360)))
1208 
1209     --看代碼的入口:注冊Layer的加載事件
1210     --layer:registerScriptHandler(PauseResumeActions_onEnterOrExit)
1211 
1212     Helper.titleLabel:setString("PauseResumeActions")
1213     Helper.subtitleLabel:setString("All actions pause at 3s and resume at 5s")
1214     return layer
1215 end
1216 
1217 --------------------------------------
1218 -- ActionIssue1305
1219 --------------------------------------
1220 local spriteTmp = nil
1221 local Issue1305_entry = nil
1222 local Issue1305_layer = nil
1223 
1224 local function Issue1305_log(sender)
1225     cclog("This message SHALL ONLY appear when the sprite is added to the scene, NOT BEFORE")
1226 end
1227 
1228 local function addSprite(dt)
1229     local scheduler = cc.Director:getInstance():getScheduler()
1230     --首先取消腳本回調,放置重復將Sprite add到Layer上
1231     scheduler:unscheduleScriptEntry(Issue1305_entry)
1232     
1233     --將Sprite放在舞台上
1234     spriteTmp:setPosition(cc.p(250, 250))
1235     Issue1305_layer:addChild(spriteTmp)
1236 end
1237 
1238 local function Issue1305_onEnterOrExit(tag)
1239     local scheduler = cc.Director:getInstance():getScheduler()
1240     if tag == "enter" then
1241         --Layer載入之后2秒,調用addSprite,將Sprite放到Layer上
1242         Issue1305_entry = scheduler:scheduleScriptFunc(addSprite, 2, false)
1243     elseif tag == "exit" then
1244         scheduler:unscheduleScriptEntry(Issue1305_entry)
1245     end
1246 end
1247 
1248 local function ActionIssue1305()
1249     Issue1305_layer = cc.Layer:create()
1250     initWithLayer(Issue1305_layer)
1251 
1252     centerSprites(0)
1253 
1254     --創建Sprite,綁定callfunc Action,CCLog一些信息
1255     spriteTmp = cc.Sprite:create("Images/grossini.png")
1256     spriteTmp:runAction(cc.CallFunc:create(Issue1305_log))
1257 
1258     Issue1305_layer:registerScriptHandler(Issue1305_onEnterOrExit)
1259 
1260     Helper.titleLabel:setString("Issue 1305")
1261     Helper.subtitleLabel:setString("In two seconds you should see a message on the console. NOT BEFORE.")
1262     return Issue1305_layer
1263 end
1264 
1265 --------------------------------------
1266 -- ActionIssue1305_2
1267 --------------------------------------
1268 --老套路,將cc.CallFunc:create() 放到Sequence中
1269 local function Issue1305_2_log1()
1270     cclog("1st block")
1271 end
1272 
1273 local function Issue1305_2_log2()
1274     cclog("2nd block")
1275 end
1276 
1277 local function Issue1305_2_log3()
1278     cclog("3rd block")
1279 end
1280 
1281 local function Issue1305_2_log4()
1282     cclog("4th block")
1283 end
1284 
1285 local function ActionIssue1305_2()
1286     local layer = cc.Layer:create()
1287     initWithLayer(layer)
1288 
1289     centerSprites(0)
1290 
1291     local spr = cc.Sprite:create("Images/grossini.png")
1292     spr:setPosition(cc.p(200,200))
1293     layer:addChild(spr)
1294 
1295     local act1 = cc.MoveBy:create(2 ,cc.p(0, 100))
1296     local act2 = cc.CallFunc:create(Issue1305_2_log1)
1297     local act3 = cc.MoveBy:create(2, cc.p(0, -100))
1298     local act4 = cc.CallFunc:create(Issue1305_2_log2)
1299     local act5 = cc.MoveBy:create(2, cc.p(100, -100))
1300     local act6 = cc.CallFunc:create(Issue1305_2_log3)
1301     local act7 = cc.MoveBy:create(2, cc.p(-100, 0))
1302     local act8 = cc.CallFunc:create(Issue1305_2_log4)
1303     
1304     local actF = cc.Sequence:create(act1, act2, act3, act4, act5, act6, act7, act8)
1305 
1306     cc.Director:getInstance():getActionManager():addAction(actF ,spr, false)
1307 
1308     Helper.titleLabel:setString("Issue 1305 #2")
1309     Helper.subtitleLabel:setString("See console. You should only see one message for each block")
1310     return layer
1311 end
1312 
1313 --------------------------------------
1314 -- ActionIssue1288
1315 --------------------------------------
1316 --關鍵字:MoveBy,reverse,Sequence,Repeat
1317 local function ActionIssue1288()
1318     local layer = cc.Layer:create()
1319     initWithLayer(layer)
1320 
1321     centerSprites(0)
1322     
1323     --Sprite
1324     local spr = cc.Sprite:create("Images/grossini.png")
1325     spr:setPosition(cc.p(100, 100))
1326     layer:addChild(spr)
1327 
1328     local act1 = cc.MoveBy:create(0.5, cc.p(100, 0))
1329     local act2 = act1:reverse()
1330     local act3 = cc.Sequence:create(act1, act2)
1331     
1332     local act4 = cc.Repeat:create(act3, 2)
1333 
1334     spr:runAction(act4)
1335 
1336     Helper.titleLabel:setString("Issue 1288")
1337     Helper.subtitleLabel:setString("Sprite should end at the position where it started.")
1338     return layer
1339 end
1340 
1341 --------------------------------------
1342 -- ActionIssue1288_2
1343 --------------------------------------
1344 local function ActionIssue1288_2()
1345     local layer = cc.Layer:create()
1346     initWithLayer(layer)
1347 
1348     centerSprites(0)
1349 
1350     local spr = cc.Sprite:create("Images/grossini.png")
1351     spr:setPosition(cc.p(100, 100))
1352     layer:addChild(spr)
1353 
1354     local act1 = cc.MoveBy:create(0.5, cc.p(100, 0))
1355     spr:runAction(cc.Repeat:create(act1, 1))
1356 
1357     Helper.titleLabel:setString("Issue 1288 #2")
1358     Helper.subtitleLabel:setString("Sprite should move 100 pixels, and stay there")
1359     return layer
1360 end
1361 
1362 --------------------------------------
1363 -- ActionIssue1327
1364 --------------------------------------
1365 local function logSprRotation(sender)
1366     --獲得時間發送者的旋轉角度,這個角度是相對於初始狀態而言的。
1367     cclog(""..sender:getRotation())
1368 end
1369 
1370 local function ActionIssue1327()
1371     local layer = cc.Layer:create()
1372     initWithLayer(layer)
1373 
1374     centerSprites(0)
1375 
1376     local spr = cc.Sprite:create("Images/grossini.png")
1377     spr:setPosition(cc.p(100, 100))
1378     layer:addChild(spr)
1379 
1380     local act1 = cc.CallFunc:create(logSprRotation)
1381     local act2 = cc.RotateBy:create(0.25, 45)
1382     local act3 = cc.CallFunc:create(logSprRotation)
1383     local act4 = cc.RotateBy:create(0.25, 45)
1384     local act5 = cc.CallFunc:create(logSprRotation)
1385     local act6 = cc.RotateBy:create(0.25, 45)
1386     local act7 = cc.CallFunc:create(logSprRotation)
1387     local act8 = cc.RotateBy:create(0.25, 45)
1388     local act9 = cc.CallFunc:create(logSprRotation)
1389     spr:runAction(cc.Sequence:create(act1, act2, act3, act4, act5, act6, act7,act8, act9))
1390 
1391     Helper.titleLabel:setString("Issue 1327")
1392     Helper.subtitleLabel:setString("See console: You should see: 0, 45, 90, 135, 180")
1393     return layer
1394 end
1395 
1396 function ActionsTest()
1397     cclog("ActionsTest")
1398     local scene = cc.Scene:create()
1399 
1400     Helper.createFunctionTable = {
1401         ActionManual, --手動設置Action
1402         ActionMove,
1403         ActionScale,
1404         ActionRotate,
1405         ActionSkew,
1406         ActionRotationalSkewVSStandardSkew,
1407         ActionSkewRotate,
1408         ActionJump,
1409         ActionCardinalSpline,
1410         ActionCatmullRom,
1411         ActionBezier,
1412         ActionBlink,
1413         ActionFade,
1414         ActionTint,
1415         ActionAnimate,
1416         ActionSequence,
1417         ActionSequence2,
1418         ActionSpawn,
1419         ActionReverse,
1420         ActionDelaytime,
1421         ActionRepeat,
1422         ActionRepeatForever,
1423         ActionRotateToRepeat,
1424         ActionRotateJerk,
1425         ActionCallFunc,
1426         ActionCallFuncND,
1427         ActionReverseSequence,
1428         ActionReverseSequence2,
1429         ActionOrbit,
1430         ActionFollow,
1431         ActionTargeted,
1432         PauseResumeActions,
1433         ActionIssue1305,
1434         ActionIssue1305_2,
1435         ActionIssue1288,
1436         ActionIssue1288_2,
1437         ActionIssue1327
1438     }
1439     scene:addChild(ActionManual())
1440     scene:addChild(CreateBackMenuItem())
1441 
1442     return scene
1443 end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM