local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")--定时器第二种方式(需要引入头文件)
local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end)
function MainScene:ctor()
local sp = display.newSprite("ui/game_scene/game_background/game_background_0.png");
sp:pos(display.cx, display.cy)-- 屏幕中心
sp:addTo(self)
--self:addBg();
--self:testAnimation()
--self:testAction();
--self:testTouch()
--self:testSchedule()
--self:testAudio()
--self:createsp();
--self:testSlider();
--self:testBtn()
--self:testUIInput();
--self:testCheckBox();
--self:testCheckBoxGroup();
--self:testScrollView();
--self:testPageview();
self:testListView();
end
function MainScene:onEnter()
end
function MainScene:onExit()
end
-----------创建精灵Sprite-------------
function MainScene:addBg()
local sp = display.newSprite("ui/game_scene/game_background/game_background_0.png");
sp:pos(display.cx, display.cy)-- 屏幕中心
sp:addTo(self)
--------- 2、cocos方法--------
local sp = cc.Sprite:create("ui/game_scene/game_background/game_background_1.png");
sp:setPosition(cc.p(display.cx,display.cy));
self:addChild(sp, 1);
end
------------动画Animation------------
function MainScene:testAnimation()
local png = "loading.png"
local plist = "loading.plist"
display.addSpriteFrames(plist, png);--添加精灵帧
self._sp = display.newSprite("#loading_0.png")--创建精灵,从大图创建加上#
self._sp:pos(display.cx, display.cy)
self:addChild(self._sp, 1);-- 添加
-- 公共的字符串家转义字符,起始位置,结束位置
local frames = display.newFrames("loading_%d.png",0,8);--存储精灵帧
local animate = display.newAnimation(frames,0.08);--动画切换间隔为0.08妙
self._sp:playAnimationForever(animate);--播放动画
end
-----------动作Action----------------
function MainScene:testAction()
local sp = cc.Sprite:create("ui/common/face/face_0.png");
sp:pos(display.cx, display.cy)
sp:addTo(self);
-- c++动作
-- local moveto = cc.MoveTo:create(2,cc.p(100,100))-- 移动到
-- sp:runAction(moveto)
-- local moveby = cc.MoveBy:create(2, cc.p(100,100))-- 移动了
-- sp:runAction(moveby)
-- 封装好的动作
--transition.moveTo(sp, {time =1,x=display.cx-200,y=display.cy})
--transition.moveTo(sp, {time=1,y=display.bottom})
-- 设置透明度(淡入淡出)
--transition.fadeTo(sp, {time=1,opacity =025}) -- 0为全透明,此后将停止所有动作
-- 旋转
--transition.rotateTo(sp, {rotate=180,time=1}) --动作执行的对象,table表包含时间与角度
-- 执行动作
--[[transition.execute(sp,
cc.MoveTo:create(2,cc.p(100,200)),
{
daley = 1,
easing = "backut",
onComplete = function()
print("动作执行结束")
end
}
);]]
-- 动作执行结束的回调函数
-- 序列动作
local seq = transition.sequence({
cc.MoveTo:create(2,cc.p(100,100)),
cc.DelayTime:create(3),
cc.RotateBy:create(2,90)
})
--sp:runAction(seq)
-- 瞬时动作
local spwn = cc.Spawn:create({
cc.MoveTo:create(2,cc.p(100,100)),
cc.RotateBy:create(2,90)
})
sp:runAction(spwn)
end
-------------触摸--------------------
function MainScene:testTouch()
local sp = cc.Sprite:create("ui/common/face/face_3.png");
sp:pos(display.cx, display.cy)
sp:addTo(self);
-- 设置可以点击
sp:setTouchEnabled(true);
-- 设置吞噬事件
sp:setTouchSwallowEnabled(false);
-- 设置监听事件
sp:addNodeEventListener(cc.NODE_TOUCH_EVENT,
function(event)-- 回调函数
if event.name == "began" then
print("touch begin")
-- 实现场景切换
local GameScene = require("app.scenes.GameScene");
local scene = GameScene:new();
-- 切换动画
local trans = display.wrapSceneWithTransition(scene,"fade",0.5);
-- 实现场景切换
display.replaceScene(trans);
return true;-- 说明触摸已经生效
elseif event.name == "moved"then
print("touch moved")
elseif event.name == "ended"then
print("touch ended")
else
print("touch moved")
end
end
)
end
-----------时间调度器-----------------
function MainScene:testSchedule()
--[[
local scheduleId ;
-- 获取定时器的管理器
shareScheduler = cc.Director:getInstance():getScheduler();
scheduleId = shareScheduler:scheduleScriptFunc(
function()
print("定时器")
end
,1,false);-- 每隔一秒调用一次,若为0则表示每帧都会调用
]]
-- 第二种定时器
local function update(dt)
print(dt)
end
local schedule = scheduler.scheduleGlobal(update,0.5);
scheduler.unscheduleGlobal(schedule)
end
----------音频处理Audio---------------
function MainScene:testAudio()
-- 预加载音乐文件
audio.preloadSound("music/bg3.mp3");
-- 播放音乐(路径,是否循环播放)
audio.playMusic("music/bg3.mp3",true);
-- 播放音效(路径,是否循环播放)
audio.playSound("music/click.mp3",false);
-- 设置音乐音量大小0~1(与音效无关)
audio.setMusicVolume(0.5);
end
----------创建精灵的3种方式------------
function MainScene:createsp()
-- 1、直接创建
local sp = display.newSprite("ui/common/face/face_5.png")
sp:pos(100, 100);
sp:addTo(self);
-- 2、从大图中读取
display.addSpriteFrames("loading.plist","loading.png")
self._sp = display.newSprite("#loading_0.png");
self._sp:pos(200, 200)
self._sp:addTo(self);
-- 3、从精灵帧读取SpriteFrame
local spriteFrame = cc.SpriteFrame:create ("loading_0.png",cc.rect(300,300,100,100));
--local spriteFrame = cc.SpriteFrameCache:getInstance():getSpriteFrame("loading_5.png");
local spf = display.newSprite(spriteFrame)
spf:pos(300, 300)
spf:addTo(self);
end
----------创建滑动条Slider-------------
MainScene.SLIDER_IMAGES = {
bar = "SliderBar.png",
button = "SliderButton.png"
}
function MainScene:testSlider()
local barHeight = 40
local barWith = 400
-- 显示方式从左到右
local slider = cc.ui.UISlider.new(display.LEFT_TO_RIGHT,
MainScene.SLIDER_IMAGES,
{scale9 = true})
--slider:setAnchorPoint(cc.p(0.5,0.5))
-- 设置位置
--slider:pos(display.cx-200, display.cy)
slider:align(display.CENTER, display.cx, display.cy)
-- 设置Slider的内容大小
slider:setSliderSize(barWith, barHeight)
-- 设置Slider的默认值
slider:setSliderValue(75)
-- 设置滑动回调事件
slider:onSliderValueChanged(function (event)
local value = event.value;
print(value);
audio.setMusicVolume(value/100);
end)
slider:addTo(self)
end
----------按钮UIPushButton------------
function MainScene:testBtn()
-- 资源路径,设置是否可以九宫格拉伸
local btn = cc.ui.UIPushButton.new("GreenButton.png",{scale9=true})
-- 设置Button大小
btn:setButtonSize(240, 60)
-- 设置button正常状态下的标题(状态,对应的label)
btn:setButtonLabel("normal",cc.ui.UILabel.new({
UILableType = 2,
text = "This is PushButton",
size = 18
}))
-- 设置button点击状态下的标题(状态,对应的label)
btn:setButtonLabel("pressed",cc.ui.UILabel.new({
UILableType = 2,
text = "Pressed",
size = 18
}))
-- 设置锚点与位置
btn:align(display.CENTER, display.cx, display.cy)
btn:addTo(self)
-- 点击回调函数
btn:onButtonClicked(function(event)
print("Click");
end)
-- 注意点击与按下的区别
btn:onButtonPressed(function(event)
print("Pressed");
end)
end
----------输入框UIInput----------------
function MainScene:testUIInput()
-- 输入框回调函数(监听器)
local function onEdit(event,editBox)
if event == "began" then
print("开始输入")
elseif event == "changed" then
print("内容发生改变")
-- 获取编辑框内容
local text = editBox:getText();
print(text);
elseif event == "ended" then
print("输入结束")
elseif event == "return" then
print("输入返回")
end
end
local input = cc.ui.UIInput.new({
image = "GreenButton.png",
listener = onEdit,-- 监听函数
x = display.cx,
y = display.cy,
size = cc.size(400, 80)
})
input:addTo(self);
-- 设置提示语
input:setPlaceHolder("请输入密码:");
-- 设置内容显示方式 0:密文格式
input:setInputFlag(0);
end
----------复选框CheckBox----------------
MainScene.CHECKBOX_BUTTON_IMAGES={
off = "CheckBoxButtonOff.png",
on = "CheckBoxButtonOn.png"
}
function MainScene:testCheckBox()
local checkbox = cc.ui.UICheckBoxButton.new(MainScene.CHECKBOX_BUTTON_IMAGES)
-- 设置标题(创建颜色是cc.c3b,位置cc.p,大小是cc.size)
checkbox:setButtonLabel(
cc.ui.UILabel.new({
text = "复选框",
size = 22,
color = cc.c3b(255, 0, 0)}));
-- 设置标题的偏移位置
checkbox:setButtonLabelOffset(0, -40);
-- 设置标题的对齐方式
checkbox:setButtonLabelAlignment(display.CENTER)
--设置锚点及位置
checkbox:align(display.LEFT_CENTER, display.cx, display.cy)
--设置回调函数
tag = true -- 标记
checkbox:onButtonStateChanged(
function (event)
if tag then
print("true")
else print("false")
end
end)
checkbox:addTo(self);
end
----------复选框组CheckBoxGroup----------------
MainScene.RADIO_BUTTON_IMAGES = {
off = "RadioButtonOff.png",
on = "RadioButtonOn.png"
}
function MainScene:testCheckBoxGroup()
-- 排列方式
local group = cc.ui.UICheckBoxButtonGroup.new(display.TOP_TO_BOTTOM)
-- 添加复选框
local checkBtn1 = cc.ui.UICheckBoxButton.new(MainScene.RADIO_BUTTON_IMAGES);
-- 设置复选框Label
checkBtn1:setButtonLabel(cc.ui.UILabel.new({
text = "option 1",
color = display.COLOR_RED
}))
-- 设置标题偏移量
checkBtn1:setButtonLabelOffset(20, 0)
checkBtn1:align(display.LEFT_CENTER)
-- 添加复选框
local checkBtn2 = cc.ui.UICheckBoxButton.new(MainScene.RADIO_BUTTON_IMAGES);
-- 设置复选框Label
checkBtn2:setButtonLabel(cc.ui.UILabel.new({
text = "option 2",
color = display.COLOR_RED
}))
-- 设置标题偏移量
checkBtn2:setButtonLabelOffset(20, 0)
checkBtn2:align(display.LEFT_CENTER)
-- 添加复选框
local checkBtn3 = cc.ui.UICheckBoxButton.new(MainScene.RADIO_BUTTON_IMAGES);
-- 设置复选框Label
checkBtn3:setButtonLabel(cc.ui.UILabel.new({
text = "option 3",
color = display.COLOR_RED
}))
-- 设置标题偏移量
checkBtn3:setButtonLabelOffset(20, 0)
checkBtn3:align(display.LEFT_CENTER)
-- 设置间距
group:setButtonsLayoutMargin(10, 10, 10, 10)
-- 设置复选框组锚点和位置
group:align(display.LEFT_CENTER, display.left+40, display.top-240)
-- 添加到复选框组中
group:addButton(checkBtn1);
group:addButton(checkBtn2);
group:addButton(checkBtn3);
-- 设置第二个按钮默认被选中
group:getButtonAtIndex(2):setButtonSelected(true);
-- 设置回调事件
group:onButtonSelectChanged(function (event)
printf("Option %d selected",event.selected)
group:removeButtonAtIndex(event.selected);--删除选中的按钮
end)
group:addTo(self);
end
----------滚动视图ScrollView----------------
function MainScene:testScrollView()
local spr = display.newScale9Sprite("sunset.png")
spr:setContentSize(300, 200)
spr:pos(display.cx, display.cy)
--节点node
local emptyNode = cc.Node:create();
emptyNode:addChild(spr);
-- 矩形框
local bound = spr:getBoundingBox()
bound.width = 1000;
bound.height = 200;
-- 创建滑动视图
local scrollView = cc.ui.UIScrollView.new({
viewRect = bound,
bgColor = cc.c4b(0, 255, 0, 255)
})
-- 增加滑动内容
scrollView:addScrollNode(emptyNode)
-- 增加滑动方向
scrollView:setDirection(cc.ui.UIScrollView.DIRECTION_HORIZONTAL)
-- 滑动回调函数
scrollView:onScroll(handler(self,self.scrollListener))
scrollView:addTo(self)
end
function MainScene:scrollListener()
print("滑动了")
end
----------翻页视图UIPageView----------------
function MainScene:testPageview()
-- pageview与cocos中的不同在于他是item自动排列生成,
-- 根据定义的行列数一页一页的排布,不够会重新开辟一页
-- cocos中则是每一页由layout组成,页码数是确定的
local page = cc.ui.UIPageView.new({
-- bgColor = cc.c4b(255,0,0,125), -- 已被弃用
-- bg = "sunset.png",
viewRect = cc.rect(80,80,780,480),
column = 2, -- 列
row =3, -- 行
padding = {left = 20,right =20,top = 20,bottom =20},
columnSpace =10,
rowSpace =10
})
for i=1,18 do
-- 创建一个条目
local item = page:newItem();
-- 创建一个layercolor
local content = cc.LayerColor:create(
cc.c4b(math.random(250),
math.random(250),
math.random(250),
251))
-- 设置内容大小
content:setContentSize(240,140)
-- 设置不可点击
content:setTouchEnabled(false)
item:addChild(content)
page:addItem(item)
end
page:reload();-- 重新刷新界面
page:addTo(self)
end
----------列表视图UIListView----------------
function MainScene:testListView()
local lv = cc.ui.UIListView.new({
bgColor = cc.c4b(0,0,125,255),
viewRect = cc.rect(40,80,120,400),
direction = cc.ui.UIScrollView.DIRECTION_VERTICAL,
scrollbarImgV = "bar.png"
})
for i=1,20 do
local item = lv:newItem();
local content
content = cc.ui.UILabel.new({
text = "Item"..i,
size = 20,
align = cc.ui.TEXT_ALIGN_CENTER,
color = display.COLOR_RED
})
content:pos(0,40*i)
item:addContent(content)
item:setItemSize(120, 40)
lv:addItem(item)
end
lv:reload()
lv:addTo(self)
end
return MainScene