1.單點觸控
--測試**,新增label
local txt_label = cc.labelttf:create("測試觸控","courier",50);
txt_label:addto(self)
txt_label:setposition(yl.width/2,yl.height/2 - 100);
self.txt_label = txt_label
--單點觸控監聽
local txtlisteneronebyone = cc.eventlistenertouchonebyone:create();
--單點觸控處理函式
local function ontouchbegan( touch , event)
local pos = touch:getlocation()
pos = self.txt_label:converttonodespace(pos)
local rec = cc.rect(0, 0, self.txt_label:getcontentsize().width, self.txt_label:getcontentsize().height)
if true == cc.rectcontainspoint(rec, pos) then
--print("**********=單點觸控開始**********===")
endreturn true --返回true,代表事件繼續傳遞,後續可執行move,ended等事件. 返回false則終止事件傳遞
endlocal function ontouchmove( touch , event)
local pos = touch:getlocation()
pos = self.txt_label:converttonodespace(pos)
local rec = cc.rect(0, 0, self.txt_label:getcontentsize().width, self.txt_label:getcontentsize().height)
if true == cc.rectcontainspoint(rec, pos) then
--print("**********=單點移動開始**********===")
self.txt_label:setposition(touch:getlocation())
endreturn true
endlocal function ontouchended( touch , event)
local pos = touch:getlocation()
pos = self.txt_label:converttonodespace(pos)
local rec = cc.rect(0, 0, self.txt_label:getcontentsize().width, self.txt_label:getcontentsize().height)
if true == cc.rectcontainspoint(rec, pos) then
print("**********=單點觸控結束**********===")
self.txt_label:setposition(yl.width/2,yl.height/2 - 100)
endreturn true
end--註冊單點觸控
txtlisteneronebyone:registerscripthandler(ontouchbegan,cc.handler.event_touch_began )
txtlisteneronebyone:registerscripthandler(ontouchmove,cc.handler.event_touch_moved )
txtlisteneronebyone:registerscripthandler(ontouchended,cc.handler.event_touch_ended )
--把事件監聽新增進事件管理器
local eventdispatcher = cc.director:getinstance():geteventdispatcher()
eventdispatcher:addeventlistenerwithscenegraphpriority(txtlisteneronebyone, self.txt_label)
2.多點觸控
--測試**,新增兩個label
local txt_label = cc.labelttf:create("測試觸控","courier",50);
txt_label:addto(self)
txt_label:setposition(yl.width/2,yl.height/2 - 100);
self.txt_label = txt_label
local txt_label2 = cc.labelttf:create("測試觸控","courier",50);
txt_label2:addto(self)
txt_label2:setposition(yl.width/2,yl.height/2 + 300);
self.txt_label2 = txt_label2
--建立多點觸控監聽
local txtlistenerallatonce = cc.eventlistenertouchallatonce:create();
--多點觸控處理函式
local function ontouchesbegan( touches , event)
for k,v in ipairs(touches) do --多點觸控傳遞進來是乙個容器,用遍歷來讀取元素
local pos = self:converttonodespace(v:getlocation())
local box1 = self.txt_label:getboundingbox()
local box2 = self.txt_label2:getboundingbox()
if true == cc.rectcontainspoint(box1, pos) or true == cc.rectcontainspoint(box2, pos) then
print("多點觸控開始")
return true --返回true,代表事件繼續傳遞,後續可執行move,ended等事件. 返回false則終止事件傳遞
endend
endlocal function ontouchesmove( touches , event)
for k,v in ipairs(touches) do
local pos = self:converttonodespace(v:getlocation())
local box1 = self.txt_label:getboundingbox()
local box2 = self.txt_label2:getboundingbox()
if true == cc.rectcontainspoint(box1, pos) or true == cc.rectcontainspoint(box2, pos) then
print("多點觸控移動")
if true == cc.rectcontainspoint(box1, pos) then
self.txt_label:setposition(v:getlocation())
elseif true == cc.rectcontainspoint(box2, pos) then
self.txt_label2:setposition(v:getlocation())
endreturn true --返回true,代表事件繼續傳遞,後續可執行move,ended等事件. 返回false則終止事件傳遞
endend
endlocal function ontouchesended( touches , event)
print("多點觸控結束")
return true
end--註冊多點觸控
txtlistenerallatonce:registerscripthandler(ontouchesbegan,cc.handler.event_touches_began)
txtlistenerallatonce:registerscripthandler(ontouchesmove,cc.handler.event_touches_moved)
txtlistenerallatonce:registerscripthandler(ontouchesended,cc.handler.event_touches_ended)
--把事件監聽新增進事件管理器
local eventdispatcher = cc.director:getinstance():geteventdispatcher()
eventdispatcher:addeventlistenerwithscenegraphpriority(txtlistenerallatonce, self)
cocos lua中富文字控制項
local rich text ccui.richtext create 建立富文字 rich text ignorecontentadaptwithsize false 是否文字自動轉行 rich text setcontentsize 500 40 設定大小 rich text setposit...
安卓態建立控制項給控制項分配ID
如果在 中動態建立控制項想給控制項分配id該怎麼分配呢?textview text new textview this text.setid r.id.tvusercode text.settext array i text.settextcolor color.black text.settext...
給動態建立的控制項指定事件
1.先宣告dialogfield變數,記住需要指定事件的控制項使用formstringcontrol 2.在dialog事件中建立相關控制項,注意dialogfield與formstringcontrol的不同之處。formstringcontrol需要指定name 3.在dialogpostrun...