Cocos2d-x-lua学习点滴
Lua下的方法。自己项目经验,个人见解,不能确保正确。
Sprite:
local Light = CCSprite:create("light.png")
Light:setPosition(ccp(300,300))
Light:setScale(1)
Light:setVisible(true)
self.secondLevelMenuLayer:addChild(Light)
Button:
searchButton = Button:create()
searchButton:setScale(1.2)
searchButton:setTouchEnabled(true)
searchButton:loadTextures("button-search1.png","button-search2.png","button-search3.png") -- 3个""不可或缺。可nil。
searchButton:setPosition(CCPoint(280,400))
self.superView.secondLevelMenuLayer:addWidget(searchButton)
searchButton:setEnabled(false)
Button的禁用状态。
在button不可触摸的时候设置图片灰色。
Button:setTouchEnable(false)
同一时候设置Button:setBright(false)
Fade效果
FadeIn->是从暗逐渐变亮
FadeOut->从亮逐渐变消失。
FadeTo->从亮逐渐变暗
local actionTime = 0.2local array = CCArray:create()
array:addObject(CCFadeTo:create(0.2, 100))
local action = CCSequence:create(array)
self.LayerColor:runAction(action)
当前引擎UI界面不可实现此效果。
Sprite的旋转
local Light = CCSprite:create("light.png")
Light:setPosition(ccp(480,510))
Light:setScale(0.8)
Light:setVisible(true)
L = CCRotateBy:create(5,360) --RotateTo角度360就没效果。有网帖
Light:runAction(CCRepeatForever:create(L))
缩放效果
local ImageView_victory = self.secondLevelMenuLayer:getWidgetByName("ImageView_victory")
local array = CCArray:create()
array:addObject(CCScaleBy:create(0.5,1.5))--參数为time。scale
array:addObject(CCScaleTo:create(0.5,1))
local action1 = CCSequence:create(array)
ImageView_victory:runAction(action1)
UI界面分步载入的实现
local imgs = {} --创建数组
imgs[1] = ImageView_victory
local imgs0 = {}
imgs0[1] = level
imgs0[2] = shengwang
imgs0[3] = gold
local imgs1 = {}
imgs1[1] = hero0
imgs1[2] = hero1
imgs1[3] = hero2
imgs1[4] = hero3
imgs1[5] = hero4
local imgs2 = {}
imgs2[1] = jingyantiao0
imgs2[2] = jingyan0
imgs2[3] = jingyantiao1
imgs2[4] = jingyan1
imgs2[5] = jingyantiao2
imgs2[6] = jingyan2
imgs2[7] = jingyantiao3
imgs2[8] = jingyan3
imgs2[9] = jingyantiao4
imgs2[10] = jingyan4
local imgs3 = {}
imgs3[1] = exp0
imgs3[2] = exp1
imgs3[3] = exp2
imgs3[4] = exp3
imgs3[5] = exp4
local imgs4 = {}
imgs4[1] = ImageView_sun_0
imgs4[2] = ImageView_sun_1
imgs4[3] = ImageView_sun_2
imgs4[4] = ImageView_sun_3
imgs4[5] = ImageView_sun_4
local imgs5 = {}
imgs5[1] = Button_again
imgs5[2] = Button_share
imgs5[3] = Button_sure
local all_imgs = {}
all_imgs[1] = imgs
all_imgs[2] = imgs0
all_imgs[3] = imgs1
all_imgs[4] = imgs2
all_imgs[5] = imgs3
all_imgs[6] = imgs4
all_imgs[7] = imgs5
local index = 1
function callback() --for循环遍历数组
if all_imgs[index] then
for k, v in ipairs(all_imgs[index]) do
if v then
v:setEnabled(true)
end
end
end
index = index +1
if index >7 then
self.secondLevelMenuLayer:stopAllActions()
end
end
local array = CCArray:create()
local callfunc = CCCallFunc:create(callback)
array:addObject(callfunc)
local sequence = CCSequence:create(array)
local forever = CCRepeatForever:create(sequence)
self.secondLevelMenuLayer:runAction(forever)
UI上的控件获取须要强转
--战斗结束界面Button实现
local function back()
CCDirector:sharedDirector():popScene()
end
local Button_close = tolua.cast(self.secondLevelMenuLayer:getWidgetByName("Button_close"),"Button")--lua的强转
Button_close:setScale(1.3)
Button_close:addTouchEventListener(back)
local Button_sure = self.secondLevelMenuLayer:getWidgetByName("Button_sure")
Button_sure:addTouchEventListener(back)
local Button_again = self.secondLevelMenuLayer:getWidgetByName("Button_again")
local function enterBattle(sender, eventType)
local pvpScene = PVPScene:new()
pvpScene:init()
CCDirector:sharedDirector():pushScene(pvpScene.scene)
end
Button_again:addTouchEventListener(enterBattle)
Button直接获取UI
创建UI
FormationUI = class ()
local ccs = require ("ccs")
function FormationUI:ctor()
self.node = GUIReader:shareReader():widgetFromJsonFile("fight-object_1.ExportJson")--当初做的时候json文件出错当时一直崩溃
end
function FormationUI:init()
end
Button获取UI(不通过scene)
--formationButton
local function enterFormation(sender,eventType)
if eventType == ccs.TouchEventType.ended then
local formationUI = FormationUI.new()
self.secondLevelMenuLayer:addWidget(formationUI.node)
formationUI:init()
end
end
local formationButton = self.menuLayer:getWidgetByName("formationButton")
formationButton:addTouchEventListener(enterFormation)
获取屏幕尺寸
local visibleSize = CCDirector:sharedDirector():getVisibleSize()
获取json文件
self.node = GUIReader:shareReader():widgetFromJsonFile("battleLose.ExportJson")
加入当前副本指示箭头(跳动效果)
jiantou = CCSprite:create("image_jiantou.png")
jiantou:setPosition(ccp(170,460))
jiantou:setZOrder(1)
self.superView.secondLevelMenuLayer:addChild(jiantou)
local actionUp = CCJumpBy:create(20,ccp(0,0),40,20)
local actionByBack = actionUp:reverse()
local array = CCArray:create()
array:addObject(actionUp)
array:addObject(actionByBack)
local action = CCSequence:create(array)
jiantou:runAction(CCRepeatForever:create(action))
显示一精灵的方法。
没有就创建,有就隐藏。sender,eventType调用函数。參数为xy坐标
--显示箭头
function RegionMapUI:showArrow(x, y)
--假设没有箭头就新建
if not self.arrow then
self.arrow = CCSprite:create("image_jiantou.png")
self.arrow:setZOrder(1000)
self.arrow:setAnchorPoint(CCPoint(0.52,0.17))
self.node:addNode(self.arrow)
end
--推断是否在运行动画
if self.arrow:numberOfRunningActions() then
self.arrow:stopAllActions()
end
self.arrow:setVisible(true)
self.arrow:setPosition(CCPoint(x,y))
self.arrow:runAction(CCRepeatForever:create(CCJumpBy:create(0.8,ccp(0,0),15,1)))
end
--隐藏箭头
function RegionMapUI:hideArrow()
--假设有箭头就隐藏
if self.arrow then
self.arrow:stopAllActions()
self.arrow:setVisible(false)
end
end
self:showArrow(sender:getPositionX(), sender:getPositionY())
self.winSize = cc.Director:getInstance():getWinSize()
setPosition(self.winSize.width/2,self.winSize.height/3)