一 實際效果
二 實現原理
三 源碼下載
在egret中實現長按復制文本效果,一般用於復制優惠碼什么的。
一 實際效果
二 實現原理
在egret的游戲元素都是繪制在canvas上的,我們在canvas上覆蓋一個<p>標簽,來實現長按復制的效果。
1 首先在index.html中為egret所在div賦值id = gameDiv
2 獲取gameDiv,在gameDiv上創建一個<p>標簽
[Actionscript3]
純文本查看 復制代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
|
//在egret的div下創建<p>
var
gameDiv = document.getElementById(
"gameDiv"
);
this
.p = document.createElement(
"p"
);
gameDiv.appendChild(
this
.p);
//設置<p>屬性
this
.p.style.border =
"0px"
;
this
.p.style.backgroundColor =
"transparent"
;
this
.p.style.position =
"absolute"
;
this
.p.style.fontSize =
this
.fontSize +
"px"
;
//fontSize是將這個功能封裝后,可自定義的文本大小。具體看源碼。
this
.p.style.display =
"none"
;
this
.p.style.color =
"#000000"
;
this
.p.style.textAlign =
"center"
;
|
3 適配<p>標簽
由於egret中組件是在canvas上,而<p>標簽是在瀏覽器頁面。
所以egret中的1像素和<p>的1像素是不一樣的。
我們這里利用stage的高寬和document.body.client的高寬比例,來進行<p>標簽在canvas上的適配。(這個點比較重要)
不知道怎么回事,代碼含不良信息粘不了...刪了才能發帖成功....握草。
4 在egret使用封裝好的HtmlText類
我把這個功能封裝了下,使用方式如下:
[Actionscript3]
純文本查看 復制代碼
1
2
3
|
var
htmlText:HTMLText =
new
HTMLText();
//新建p標簽
htmlText.setValue(
"123456"
);
//設置p文本內容
htmlText.setPosition(
0
,
100
,
640
,
30
);
//設置p在canvas上位置和高寬
|
https://files-cdn.cnblogs.com/files/gamedaybyday/Egret3.2.6_HtmlTextExample.7z