«

cocos2dx-3.2+lua 常用代码

时间:2024-3-2 17:28     作者:韩俊     分类: Android


--图片

local image = ccui.ImageView:create()

image:loadTexture("ui/bg43.png") --直接获取图片

--image:loadTexture("bg54.png",ccui.TextureResType.plistType) --从plist中获取





--按钮

local button = ccui.Button:create()

button:loadTextures("bg12.png","bg12d.png","",ccui.TextureResType.plistType)





--文本

local text = ccui.Text:create()

text:setString("hello")

text:setFontSize(50)





--输入框

local input = ccui.TextField:create()

input:setText("name")

input:setPlaceHolder("请输入") --只要输入,就会替换当前文字

input:setMaxLengthEnabled(true)

input:setMaxLength(22)

input:setTouchEnabled(true)

local value = input:getStringValue()





--标签文本

local text = ccui.TextAtlas:create()

text:setProperty("5","UI/number.png",17,22,"0")





--动画

local frame1 = cc.SpriteFrameCache:getInstance():getSpriteFrame("01.png") --默认从缓存里读取

local frame2 = cc.SpriteFrameCache:getInstance():getSpriteFrame("02.png")

local frame3 = cc.SpriteFrameCache:getInstance():getSpriteFrame("03.png")

local frames = {frame1,frame2,frame3}

local animation = cc.Animation:createWithSpriteFrames(frames,0.1)--每帧间隔0.1秒

local animate = cc.Animate:create(animation)

local sprite = cc.Sprite:createWithSpriteFrameName("01.png")

sprite:runAction(animate)





--移动

local move = cc.MoveTo:create(0.2,cc.p(50,50))

local move = cc.MoveBy:create(0.2,cc.p(10,10))

dog:runAction(move)





--缩放

local scale = cc.ScaleTo:create(0.2,0.9)

local scale = cc.ScaleBy:create(0.2,1.5)

bird:runAction(scale)





--旋转

local rotate = cc.RotateTo:create(0.2,180)

local rotate = cc.RotateBy:create(0.2,-360)

bird:runAction(rotate)





--跳跃

local jump = cc.JumpTo:create(2,ccp(500,400),50,4) --时间,目标位置,跳跃高度,跳跃次数

local jump = cc.JumpBy:create(2,ccp(500,400),50,4)

sprite:runAction(jump)





--贝塞尔曲线

local data = {

cc.p(100,100),--起点

cc.p(200,300),--控制点

cc.p(500,100)--终点

}

local bezier = cc.BezierTo:create(3, data)

local bezier = cc.BezierBy:create(3, data)

sprite:runAction(bezier)





--顺序

self:runAction(cc.Sequence:create(cc.DelayTime:create(0.3), cc.CallFunc:create(function( )

printInfo("")

end)))

image:runAction(cc.Sequence:create(move, cc.CallFunc:create(function( )

printInfo("")

end)))





--重复

image:runAction(cc.RepeatForever:create(sequence))





--重复动作

MoveBy有reverse()方法

MoveTo没有

sowrd2:runAction(cc.Repeat:create(cc.Sequence:create(rotate2,rotate2:reverse()), 1))





--按钮监听

local onClick = function ( sender, eventType )

if eventType == ccui.TouchEventType.ended then

if sender == self.m_btn then

printInfo("")

end

end

self.m_btn:addTouchEventListener(onClick)

end





--每隔时间执行一次

local function listet( ... )

if self.m_schedulerHander then--停止

scheduler.unscheduleGlobal(self.m_schedulerHander)

self.m_schedulerHander = nil

end

end

self.m_schedulerHander = scheduler.scheduleGlobal(listet,0.2)

标签: android

热门推荐