Cocos2d-x 3.1.1 Lua示例 ActionEaseTest(动作)

Cocos2d-x Lua示例 ActionEaseTest(动作)

本篇博客介绍Cocos2d-x中的动作,Cocos2d-x为我们提供了丰富的动作接口,下面笔者就详细介绍一下:

本系列博客介绍的是lua的实现,想看C++实现请移步到Cocos2d-x为我们提供的例子:

Action类继承关系图,各位可以到http://cn.cocos2d-x.org/doc/cocos2d-x-3.0/db/d61/classcocos2d_1_1_action.html进行参考:

本篇博客的例子并没有涉及所有的动作,其他动作,以后的例子可能会涉及到,到时再进行说明。

我们先看一下效果,笔者只做了第一个动作的gif图,剩下的,读者可以自己运行lua-tests进行查看

本例子动作总结:

MoveBy/MoveTo: 创建一个移动的动作

EaseIn/EaseOut: 动作由慢变快/动作由快变慢

EaseInOut:动作由慢变快再由快变慢

EaseExponentialIn:动作由慢变极快

EaseExponentialOut:动作由极快变慢

EaseExponentialInOut:动作由慢至极快再由极快边慢

EaseSineIn:动作由快变慢

EaseSineOut:动作由慢变快

EaseSineInOut:精灵由慢至快再由快至慢

EaseElasticIn:让目标动作赋予弹性 ,且以目标动作起点位子赋予弹性

EaseElasticOut:让目标动作赋予弹性 ,且以目标动作终点位子赋予弹性

EaseElasticInOut:让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性

EaseBounceIn:让目标动作缓慢开始

EaseBounceOut:让目标动作赋予反弹力,且以目标动作结束位子开始反弹

EaseBounceInOut:让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹

EaseBackIn:让目标动作赋予回力 , 且以目标动作起点位置作为回力点

EaseBackOut:让目标动作赋予回力 , 且以目标动作终点位置作为回力点

EaseBackInOut:让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点

JumpBy/JumpTo:跳的动作

RotateBy/RotateTo:旋转的动作

Spawn:让多个动作同时执行

Speed:让目标运行速度加倍

实现代码如下:

local kTagAction1 = 1
local kTagAction2 = 2
local kTagSlider = 1
-- 获取屏幕大小
local s = cc.Director:getInstance():getWinSize()
-- 获取与Directorg关联的调度器
local scheduler = cc.Director:getInstance():getScheduler()

-- 创建简单的移动,持续3秒
local function createSimpleMoveBy()
  return cc.MoveBy:create(3, cc.p(s.width - 130, 0))
end

-- 创建简单的延迟,0.25秒
local function createSimpleDelayTime()
  return cc.DelayTime:create(0.25)
end

-- 设置两个精灵的位置
local function positionForTwo()
  grossini:setPosition(cc.p(60, s.height * 1 / 5))
  tamara:setPosition(cc.p(60, s.height * 4 / 5))
  kathia:setVisible(false)
end

-- 获取基础层
local function getBaseLayer()
  local layer = cc.Layer:create()

  -- 三个丑陋的精灵
  grossini = cc.Sprite:create(s_pPathGrossini)
  tamara = cc.Sprite:create(s_pPathSister1)
  kathia = cc.Sprite:create(s_pPathSister2)

  -- 添加三个精灵到层当中
  layer:addChild(grossini, 3)
  layer:addChild(kathia, 2)
  layer:addChild(tamara, 1)

  -- 设置三个精灵的位置
  grossini:setPosition(cc.p(60, s.height * 1 / 5))
  kathia:setPosition(cc.p(60, s.height * 2.5 / 5))
  tamara:setPosition(cc.p(60, s.height * 4 / 5))

  -- 初始化层
  Helper.initWithLayer(layer)

  return layer
end

-----------------------------------
--  SpriteEase
-----------------------------------
local SpriteEase_entry = nil

-- 测试停止动作
local function testStopAction(dt)
  -- 取消调度脚本
  scheduler:unscheduleScriptEntry(SpriteEase_entry)
  tamara:stopActionByTag(1)
  kathia:stopActionByTag(1)
  grossini:stopActionByTag(1)
end

-- 进入或退出
local function SpriteEase_onEnterOrExit(tag)
  -- 如果是进入
  if tag == "enter" then
    -- 执行脚本方法
    SpriteEase_entry = scheduler:scheduleScriptFunc(testStopAction, 6.25, false)
  elseif tag == "exit" then
    -- 注销循环
    scheduler:unscheduleScriptEntry(SpriteEase_entry)
  end
end
--
local function SpriteEase()

  local layer = getBaseLayer()

  local move = createSimpleMoveBy()
  local move_back = move:reverse() -- 原地返回

  -- 动作由慢变快
  local move_ease_in = cc.EaseIn:create(createSimpleMoveBy(), 2.5)
  -- 原路返回
  local move_ease_in_back = move_ease_in:reverse()

  -- 动作有块变慢
  local move_ease_out = cc.EaseOut:create(createSimpleMoveBy(), 2.5)
  -- 原路返回
  local move_ease_out_back = move_ease_out:reverse()

  -- 延时
  local delay = createSimpleDelayTime()
  -- 动作序列1
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
  -- 动作序列2
  local seq2 = cc.Sequence:create(move_ease_in,createSimpleDelayTime(),move_ease_in_back,createSimpleDelayTime())
  -- 动作序列3
  local seq3 = cc.Sequence:create(move_ease_out,createSimpleDelayTime(),move_ease_out_back,createSimpleDelayTime())

  -- 执行一个无线循环的动作
  local a2 = grossini:runAction(cc.RepeatForever:create(seq1))
  a2:setTag(1)--改变这个用来更容易分辨节点的标记。
  local a1 = tamara:runAction(cc.RepeatForever:create(seq2))
  a1:setTag(1)
  local a = kathia:runAction(cc.RepeatForever:create(seq3))
  a:setTag(1)

  -- 注册监听脚本
  layer:registerScriptHandler(SpriteEase_onEnterOrExit)

  Helper.titleLabel:setString("EaseIn - EaseOut - Stop")
  return layer
end

-----------------------------------
--	SpriteEaseInOut 精灵由慢至快再由快至慢
-----------------------------------
local function SpriteEaseInOut()
  -- 获取基础层
  local layer = getBaseLayer()
  -- 创建移动动作
  local move = createSimpleMoveBy()

  -- 创建由慢变快再由快变慢的动作,第一个参数表示动作,第二个参数表示频率
  local move_ease_inout1 = cc.EaseInOut:create(createSimpleMoveBy(), 0.65)
  -- 返回执行与本Action对象相反操作的新Action对象
  local move_ease_inout_back1 = move_ease_inout1:reverse()

  -- 创建频率为1.35的easeInOut动作
  local move_ease_inout2 = cc.EaseInOut:create(createSimpleMoveBy(), 1.35)
  -- 返回执行与本Action对象相反操作的新Action对象
  local move_ease_inout_back2 = move_ease_inout2:reverse()

  -- 创建频率为1.0的easeInOut动作
  local move_ease_inout3 = cc.EaseInOut:create(createSimpleMoveBy(), 1.0)
  -- 返回执行与本Action对象相反操作的新Action对象
  local move_ease_inout_back3 = move_ease_inout3:reverse()

  -- 延时动作
  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move_ease_inout1,delay,move_ease_inout_back1,createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease_inout2,createSimpleDelayTime(),move_ease_inout_back2,createSimpleDelayTime())
  local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime() )

  tamara:runAction(cc.RepeatForever:create(seq1))
  kathia:runAction(cc.RepeatForever:create(seq2))
  grossini:runAction(cc.RepeatForever:create(seq3))

  Helper.titleLabel:setString("EaseInOut and rates")
  return layer
end

-----------------------------------
--	SpriteEaseExponential 精灵由慢至极快,由极快到慢
-----------------------------------
local function SpriteEaseExponential()
  -- 得到基础层
  local layer = getBaseLayer()
  -- 创建移动动作
  local move = createSimpleMoveBy()
  -- 反操作
  local move_back = move:reverse()

  -- 创建由慢到极快的动作
  local move_ease_in = cc.EaseExponentialIn:create(createSimpleMoveBy())
  local move_ease_in_back = move_ease_in:reverse()

  -- 创建有极快到慢的动作
  local move_ease_out = cc.EaseExponentialOut:create(createSimpleMoveBy())
  local move_ease_out_back = move_ease_out:reverse()

  -- 创建延时动作
  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
  local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())

  -- 三个丑八怪执行动作
  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))
  kathia:runAction(cc.RepeatForever:create(seq3))
  -- 设置层标题
  Helper.titleLabel:setString("ExpIn - ExpOut actions")
  return layer
end

-----------------------------------
--	SpriteEaseExponentialInOut 精灵由慢至极快再由极快至慢
-----------------------------------
local function SpriteEaseExponentialInOut()
  -- 得到基础层
  local layer = getBaseLayer()
  -- 创建简单移动动作
  local move = createSimpleMoveBy()
  -- 反向操作
  local move_back = move:reverse()

  -- 创建由慢至极快再由极快至慢的动作
  local move_ease = cc.EaseExponentialInOut:create(createSimpleMoveBy())
  local move_ease_back = move_ease:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime() )

  -- 设置两个丑八怪的位置
  positionForTwo()

  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))

  -- 设置层标题
  Helper.titleLabel:setString("EaseExponentialInOut action")
  return layer
end

-----------------------------------
--	SpriteEaseSine 由快至慢、由慢至快
-----------------------------------
local function SpriteEaseSine()
  -- 得到基础层
  local layer = getBaseLayer()

  -- 创建简单移动动作
  local move = createSimpleMoveBy()
  -- 反向操作
  local move_back = move:reverse()

  -- 创建由快至慢的动作
  local move_ease_in = cc.EaseSineIn:create(createSimpleMoveBy())
  local move_ease_in_back = move_ease_in:reverse()

  -- 创建有慢至快的动作
  local move_ease_out = cc.EaseSineOut:create(createSimpleMoveBy())
  local move_ease_out_back = move_ease_out:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() )
  local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
  local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back,createSimpleDelayTime())

  -- 运动了喂
  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))
  kathia:runAction(cc.RepeatForever:create(seq3))

  -- 设置标题
  Helper.titleLabel:setString("EaseSineIn - EaseSineOut")
  return layer
end

-----------------------------------
--	SpriteEaseSineInOut 精灵由慢至快再由快至慢
-----------------------------------
local function SpriteEaseSineInOut()
  -- 得到基础层
  local layer = getBaseLayer()
  -- 创建简单移动动作
  local move = createSimpleMoveBy()
  local move_back = move:reverse()

  -- 由慢至快再由快至慢
  local move_ease = cc.EaseSineInOut:create(createSimpleMoveBy())
  local move_ease_back = move_ease:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime())

  -- 设置两个丑八怪的位置
  positionForTwo()

  -- 丑八怪运动啦
  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))

  Helper.titleLabel:setString("EaseSineInOut action")
  return layer
end

-----------------------------------
--	SpriteEaseElastic
-----------------------------------
local function SpriteEaseElastic()
  -- 得到基础层
  local layer = getBaseLayer()
  -- 创建简单移动动作
  local move = createSimpleMoveBy()
  local move_back = move:reverse()

  -- 让目标动作赋予弹性 ,且以目标动作起点位子赋予弹性
  local move_ease_in = cc.EaseElasticIn:create(createSimpleMoveBy())
  local move_ease_in_back = move_ease_in:reverse()

  -- 让目标动作赋予弹性 ,且以目标动作终点位子赋予弹性
  local move_ease_out = cc.EaseElasticOut:create(createSimpleMoveBy())
  local move_ease_out_back = move_ease_out:reverse()

  -- 延迟动作
  local delay = createSimpleDelayTime()
  -- 创建动作序列
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() )
  local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
  local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())

  -- 三个丑八怪又执行动作了
  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))
  kathia:runAction(cc.RepeatForever:create(seq3))

  -- 设置标题
  Helper.titleLabel:setString("Elastic In - Out actions")
  return layer
end

-----------------------------------
--	SpriteEaseElasticInOut 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性
-----------------------------------
local function SpriteEaseElasticInOut()
  -- 创建基础层
  local layer = getBaseLayer()
  -- 创建简单移动的动作
  local move = createSimpleMoveBy()

  -- 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性,频率0.3
  local move_ease_inout1 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.3)
  local move_ease_inout_back1 = move_ease_inout1:reverse()

  -- 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性,频率0.45
  local move_ease_inout2 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.45)
  local move_ease_inout_back2 = move_ease_inout2:reverse()

  -- 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性,频率0.6
  local move_ease_inout3 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.6)
  local move_ease_inout_back3 = move_ease_inout3:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move_ease_inout1, delay, move_ease_inout_back1, createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease_inout2, createSimpleDelayTime(), move_ease_inout_back2, createSimpleDelayTime())
  local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime())

  tamara:runAction(cc.RepeatForever:create(seq1))
  kathia:runAction(cc.RepeatForever:create(seq2))
  grossini:runAction(cc.RepeatForever:create(seq3))

  Helper.titleLabel:setString("EaseElasticInOut action")
  return layer
end

-----------------------------------
--	SpriteEaseBounce
-----------------------------------
local function SpriteEaseBounce()
  -- 得到基础层
  local layer = getBaseLayer()
  -- 创建简单移动动作
  local move = createSimpleMoveBy()
  local move_back = move:reverse()

  -- 让目标动作缓慢开始
  local move_ease_in = cc.EaseBounceIn:create(createSimpleMoveBy())
  local move_ease_in_back = move_ease_in:reverse()

  -- 让目标动作赋予反弹力,且以目标动作结束位子开始反弹
  local move_ease_out = cc.EaseBounceOut:create(createSimpleMoveBy())
  local move_ease_out_back = move_ease_out:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() )
  local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
  local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())

  -- 烦不烦?
  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))
  kathia:runAction(cc.RepeatForever:create(seq3))

  Helper.titleLabel:setString("Bounce In - Out actions")
  return layer
end

-----------------------------------
--	SpriteEaseBounceInOut 让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹
-----------------------------------
local function SpriteEaseBounceInOut()
    -- 得到基础层
  local layer = getBaseLayer()
  -- 创建简单移动动作
  local move = createSimpleMoveBy()
  local move_back = move:reverse()

  --  让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹
  local move_ease = cc.EaseBounceInOut:create(createSimpleMoveBy())
  local move_ease_back = move_ease:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime())

  positionForTwo()

  -- 为什么只用两个丑八怪?
  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))

  Helper.titleLabel:setString("EaseBounceInOut action")
  return layer
end

-----------------------------------
--	SpriteEaseBack
-----------------------------------
local function SpriteEaseBack()
  local layer = getBaseLayer()

  local move = createSimpleMoveBy()
  local move_back = move:reverse()

  -- 让目标动作赋予回力 , 且以目标动作起点位置作为回力点
  local move_ease_in = cc.EaseBackIn:create(createSimpleMoveBy())
  local move_ease_in_back = move_ease_in:reverse()

  -- 让目标动作赋予回力 , 且以目标动作终点位置作为回力点
  local move_ease_out = cc.EaseBackOut:create(createSimpleMoveBy())
  local move_ease_out_back = move_ease_out:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
  local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())

  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))
  kathia:runAction(cc.RepeatForever:create(seq3))

  Helper.titleLabel:setString("Back In - Out actions")
  return layer
end

-----------------------------------
--	SpriteEaseBackInOut 让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点
-----------------------------------
local function SpriteEaseBackInOut()
  local layer = getBaseLayer()

  local move = createSimpleMoveBy()
  local move_back = move:reverse()

  -- 让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点
  local move_ease = cc.EaseBackInOut:create(createSimpleMoveBy())
  local move_ease_back = move_ease:reverse()

  local delay = createSimpleDelayTime()
  local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
  local seq2 = cc.Sequence:create(move_ease,createSimpleDelayTime(), move_ease_back, createSimpleDelayTime())

  positionForTwo()

  grossini:runAction(cc.RepeatForever:create(seq1))
  tamara:runAction(cc.RepeatForever:create(seq2))

  Helper.titleLabel:setString("EaseBackInOut action")
  return layer
end

-----------------------------------
--	SpeedTest 速度测试
-----------------------------------
local SpeedTest_entry = nil
local SpeedTest_action1 = nil
local SpeedTest_action2 = nil
local SpeedTest_action3 = nil

local function altertime(dt)
  -- 设置速度
  SpeedTest_action1:setSpeed(math.random() * 2)
  SpeedTest_action2:setSpeed(math.random() * 2)
  SpeedTest_action3:setSpeed(math.random() * 2)
end

local function SpeedTest_onEnterOrExit(tag)
  if tag == "enter" then
    SpeedTest_entry = scheduler:scheduleScriptFunc(altertime, 1.0, false)
  elseif tag == "exit" then
    scheduler:unscheduleScriptEntry(SpeedTest_entry)
  end
end

-- 速度测试
local function SpeedTest()
  local layer = getBaseLayer()
  --参数1:跳到目标动作位子的所需时间
  --参数2:目标位置
  --参数3:跳的高度
  --参数4跳到目标位置的次数
  local jump1 = cc.JumpBy:create(4, cc.p(- s.width + 80, 0), 100, 4)
  local jump2 = jump1:reverse() -- 反向动作
  -- 旋转动作,参数1:旋转的时间  参数2:旋转的角度  0 - 360
  local rot1 = cc.RotateBy:create(4, 360 * 2)
  local rot2 = rot1:reverse()

  -- 蹦蹦跳跳的动作序列
  local seq3_1 = cc.Sequence:create(jump2, jump1)
  -- 旋转的动作序列
  local seq3_2 = cc.Sequence:create(rot1, rot2)

  -- 让多个动作同时执行
  local spawn = cc.Spawn:create(seq3_1, seq3_2)
  -- 速度动作1
  SpeedTest_action1 = cc.Speed:create(cc.RepeatForever:create(spawn), 1.0)

  -- 克隆一个spawn
  local spawn2 = spawn:clone()
  -- 速度动作2
  SpeedTest_action2 = cc.Speed:create(cc.RepeatForever:create(spawn2), 1.0)

  local spawn3 = spawn:clone()
  -- 速度动作3
  SpeedTest_action3 = cc.Speed:create(cc.RepeatForever:create(spawn3), 1.0)

  -- 最后一个了
  grossini:runAction(SpeedTest_action2)
  tamara:runAction(SpeedTest_action3)
  kathia:runAction(SpeedTest_action1)

  layer:registerScriptHandler(SpeedTest_onEnterOrExit)

  Helper.titleLabel:setString("Speed action")
  return layer
end

function EaseActionsTest()
  -- 创建场景
  local scene = cc.Scene:create()
  cclog("EaseActionsTest")

  -- 创建方法表
  Helper.createFunctionTable = {
    SpriteEase,
    SpriteEaseInOut,
    SpriteEaseExponential,
    SpriteEaseExponentialInOut,
    SpriteEaseSine,
    SpriteEaseSineInOut,
    SpriteEaseElastic,
    SpriteEaseElasticInOut,
    SpriteEaseBounce,
    SpriteEaseBounceInOut,
    SpriteEaseBack,
    SpriteEaseBackInOut,
    SpeedTest
  }
  -- 添加子节点
  scene:addChild(SpriteEase())
  -- 添加返回菜单
  scene:addChild(CreateBackMenuItem())

  return scene
end

Cocos2d-x 3.1.1 Lua示例 ActionEaseTest(动作),布布扣,bubuko.com

时间: 2024-10-25 09:28:57

Cocos2d-x 3.1.1 Lua示例 ActionEaseTest(动作)的相关文章

Cocos2d-x 3.2 Lua示例 CaptureScreen(截屏)

Cocos2d-x 3.2 Lua示例 CaptureScreen(截屏) 转载请注明:IT_xiao小巫 Cocos2d-x截屏功能是从3.2开始提供的,本篇博客就是介绍Cocos2d-x 3.2中Lua示例中的截屏功能.效果如下所示: 例子代码如下: --[[ 截屏测试 CaptureScreenTest ]]-- -- 获取屏幕大小 local winSize = cc.Director:getInstance():getWinSize() local kTagSprite = 1 loc

Cocos2d-x 3.2 Lua示例 ClickAndMoveTest(点击移动测试)

Cocos2d-x 3.2 Lua示例 ClickAndMoveTest(点击移动测试) 本篇博客介绍Cocos2d-x 3.2Lua示例中点击移动的例子,在这个例子你可以得到如何创建单点触摸的事件和注册事件监听回调方法. 示例代码: --[[ ClickAndMoveTest.lua 点击与移动 ]]-- -- 获取屏幕尺寸 local size = cc.Director:getInstance():getWinSize() local layer = nil -- 层 local kTag

lua示例 - 访问数据库

-- 加载动态库 lib = assert(package.loadlib("luasql/mysql.dll","luaopen_luasql_mysql")) lib() -- 创建环境对象 env = assert (luasql.mysql()) -- 连接数据库 con = assert (env:connect ("test", "root", "123456", "localhost

Cocos2d-x 3.2 Lua示例CocosDenshionTest(音频测试)

Cocos2d-x 3.2 Lua示例CocosDenshionTest(音频测试) 本篇博客介绍Cocos2d-x 3.2中Lua示例的音频测试,Cocos2d-x使用SimpleAudioEngine这个类来实现音频的控制,比如播放.暂停.停止等操作. Lua代码中,使用的是AudioEngine,具体实现可以参考AudioEngine.lua文件,只是把SimpleAudioEngin进行了封装. 示例代码: --[[ CocosDenshionTest.lua Cocos2d-x 音频支

Cocos2d-x 3.2 Lua示例 AssetsManagerTest(资源管理器)

Cocos2d-x 3.2 Lua示例 AssetsManagerTest(资源管理器) 本篇博客介绍Cocos2d-x 为我们提供的一个类--AssetsManager在Lua中的使用例子,效果如下图: Cocos2d-x 给出的例子是AssetsManagerTest,进入会发现三个菜单项: enter reset update enter是进入场景,reset是删除本地版本,重新设置,update就是更新资源文件. 笔者使用LDT打开lua-tests测试项目: 在src目录下找到Asse

Cocos2d-x 3.2 Lua示例 XMLHttpRequestTest(Http网络请求)

Cocos2d-x 3.2 Lua示例 XMLHttpRequestTest(Http网络请求) 本篇博客介绍Cocos2d-x 3.2Lua示例中的XMLHttpRequestTest,这里提供GET和POST两种http请求方式,返回数据类型有以下几种: cc.XMLHTTPREQUEST_RESPONSE_STRING = 0  -- 返回字符串类型cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER = 1 -- 返回字节数组类型cc.XMLHTTPREQUES

Cocos2d-x Lua中网格动作

Cocos2d-x Lua中网格动作 GridAction它有两个主要的子类Grid3DAction和TiledGrid3DAction,TiledGrid3DAction系列的子类中会有瓦片效果,如下图所示是Waves3D特效(Grid3DAction子类),如后图所示是WavesTiles3D特效(TiledGrid3DAction子类),比较这两个效果我们会看到瓦片效果的特别之处是界面被分割成多个方格. Waves3D特效 WavesTiles3D特效 网格动作都是采用3D效果给用户的体验

笔记:利用Cocos2dx 3.3 lua 做一个动作类游戏(一)

在这之前,声明一下: 做不完我是小狗. 没办法,没毅力和恒心,之前的那个Quick Cocos2dx做的横版过关游戏的demo已经转成了3.3的版本了,其实也算是个半成品,战斗,UI啥的都有了,呵呵. 本次DEMO要达成的目的如下: 1 熟悉Cocos2dx 3.3 - lua 2 使用Joystick 3 完成简单的怪物AI 4 尝试扩展现有的api(可选) 嗯,差不多就以上了. 今天第一次笔记. 当前完成的任务有如下: 1 使用新的player新建项目 2 在场景中添加Sprite以及其帧动

Cocos2d Lua 入门小例子 一个记忆力游戏

1.游戏说明 一个"记忆"类的比赛游戏.你和电脑对战,轮到谁的回合,谁翻两张牌,如果两张牌一样,就消掉这两张牌,得2分,可以继续翻牌,如果两张牌不一样,就换一个人.直到最后,看谁的得分高. 游戏设计思想可以看这篇文章<Cocos2d 游戏状态机> 2. Lua 简单面向对象 我们知道Lua是脚本语言,不是真正面向对象语言.但是面向对象好像能让代码结构更加合理,Javascript, Lua都可以模拟出面向对象.Lua面向对象还有超多种方式实现,这里当然不一一介绍.简单来看个