cocos2d-x 平时小例子(1)
2016-03-28 本文已影响396人
绿城河
1.创建lable
function MainScene:createLable()
local lableItem = cc.Label:createWithSystemFont("zhulei", "Arial", 64)
lableItem:setPosition(cc.p(display.cx,display.cy))
lableItem:addTo(self)
end
2.使用touch监听事件来检测node节点被触摸
function MainScene:createSpriteButton()
local layer1 = cc.Layer:create()
local sprite = cc.Sprite:create("siFangmao1.png")
sprite:setPosition(cc.p(display.cx, display.cy))
local x = sprite:getContentSize().width
local y = sprite:getContentSize().height
local lableItemX = cc.Label:createWithSystemFont(x, "Arial", 32)
lableItemX:setPosition(cc.p(display.cx - 50,display.cy + 100))
local lableItemY = cc.Label:createWithSystemFont(y, "Arial", 32)
lableItemY:setPosition(cc.p(display.cx + 50,display.cy + 100))
lableItemX:addTo(self)
lableItemY:addTo(self)
layer1:addChild(sprite)
layer1:addTo(self)
local function TouchBegan(touch, event)
local position = touch:getLocation()
local xx = touch:getLocation().x
local yy = touch:getLocation().y
local lableItemX = cc.Label:createWithSystemFont(xx, "Arial", 32)
lableItemX:setPosition(cc.p(display.cx, display.cy - 125))
lableItemX:addTo(self)
local lableItemY = cc.Label:createWithSystemFont(yy, "Arial", 32)
lableItemY:setPosition(cc.p(display.cx, display.cy - 150))
lableItemY:addTo(self)
--这个是书上写的但暂时不能实现,正在寻找原因,使用下面的方法来检测区域(找到原因:问题出在将世界坐标转化为本地坐标,在判断点是否在矩形框中出现问题,与其可能出现问题,不如直接全部转化为世界坐标)
--local node = event:getCurrentTarget()
-- 获取node坐标,rect
--local locationInNode= node:convertTouchToNodeSpace(touch:getLocation())
--local rect = cc.rect(0, 0, node:getContentSize().width, node:getContentSize().height)
-- 点击范围检测
local spritePosition = sprite:getPosition()
local rect = cc.rect(display.cx - x / 2, display.cy - y / 2, x, y)
if cc.rectContainsPoint(rect, position) then
local lableItemT = cc.Label:createWithSystemFont("ok", "Arial", 32)
lableItemT:setPosition(cc.p(display.cx, display.cy - 100))
lableItemT:addTo(self)
end
return true
end
-- 设置事件监听器
local listener = cc.EventListenerTouchOneByOne:create()
-- 设置吞没事件
listener:setSwallowTouches(true)
-- 注册监听事件
listener:registerScriptHandler(TouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
-- 添加监听器
local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer1)
end
3.创建调度者
function MainScene:createSchedule()
local i = 0
local function scheduleUpdate(dt)
i = i + 1
local node = cc.Node:create()
local lableItem = cc.Label:createWithSystemFont(i, "Arial", 32)
lableItem:setPosition(cc.p(display.cx - 400 + i * 20, display.cy - 100))
node:addChild(lableItem)
node:addTo(self)
end
local scheduler = cc.Director:getInstance():getScheduler()
scheduler:scheduleScriptFunc(scheduleUpdate, 1, false)
end
4.创建ui输入控件
function MainScene:createUInput()
local node = cc.Node:create()
node:setPosition(cc.p(display.cx, display.cy))
local text = ccui.Text:create()
text:setString("Hello")
text:setFontSize(30)
node:addChild(text)
node:addTo(self)
end
5.创建背景音乐
function MainScene:createBackMusic()
-- 内部封装的调背景音乐的方法/好奇葩的用法(里面的方法的都可以用,不要换格式,win平台)
--audio.playMusic("background.wav")
--cc.SimpleAudioEngine:getInstance():playBackgroundMusic("background.wav")
ccexp.AudioEngine:play2d("background.mp3", true)
--AudioEngine.playEffect("background.wav", true)
end
6.创建精灵延迟消失
function MainScene:createAction()
local ret = cc.Layer:create()
local sprite = cc.Sprite:create("tank_p_d.png")
sprite:setPosition(display.cx, display.cy)
sprite:runAction(cc.RotateBy:create(1.5, 90))
sprite:runAction(cc.Sequence:create(cc.DelayTime:create(2.4),cc.FadeOut:create(1.1)))
ret:addChild(sprite)
ret:addTo(self)
local function removeThis()
ret:getParent():removeChild(ret, true)
end
ret:runAction(cc.Sequence:create(cc.DelayTime:create(2.4),cc.CallFunc:create(removeThis)))
end
7.创建变速动作
function MainScene:createChAction()
local sprite = cc.Sprite:create("tank_p_d.png")
sprite:setPosition(cc.p(display.cx, display.cy))
sprite:addTo(self)
sprite:runAction(cc.EaseSineIn:create(cc.MoveBy:create(3, cc.p(display.cx, 0))))
end
8.创建进度条
function MainScene:createProgressBar()
local to1 = cc.ProgressTo:create(30, 100)
local bar = cc.ProgressTimer:create(cc.Sprite:create("bar2.png"))
bar:setType(cc.PROGRESS_TIMER_TYPE_BAR)
bar:setMidpoint(cc.p(0,0))
bar:setBarChangeRate(cc.p(1, 0))
bar:setPosition(cc.p(display.cx, display.cy))
bar:runAction(cc.RepeatForever:create(to1))
bar:addTo(self)
end
9.创建截图
function MainScene:createSceneCut()
local function afterCaptured(succeed, outputFile)
if succeed then
local sp = cc.Sprite:create(outputFile)
self:addChild(sp, 0, childTag)
sp:setPosition(display.cx , display.cy)
sp:setScale(0.25)
else
cclog("Capture screen failed.")
end
end
cc.utils:captureScreen(afterCaptured, "nihao.png")
end