1、获取URL中的请求参数的值----此方法接收参数名
1
2
3
4
5
6
|
function
getQueryString(name) {
var
reg =
new
RegExp(
"(^|&)"
+ name +
"=([^&]*)(&|$)"
,
"i"
);
var
r = window.location.search.substr(1).match(reg);
if
(r !=
null
)
return
decodeURIComponent(r[2]);
return
null
;
};
|
2、底图上添加文字---适用于按钮Sprite
1
2
3
4
5
6
7
8
9
10
|
var
MyButtonSprite = cc.Sprite.extend({
ctor:
function
(fileName, title, fontName, fontSize) {
this
._super(fileName);
var
titleLabel =
new
cc.LabelTTF(title, fontName, fontSize);
this
.addChild(titleLabel);
titleLabel.x =
this
.getContentSize().width / 2;
titleLabel.y =
this
.getContentSize().height / 2;
}
});
|
3、远程图片加载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
loadImgFromUrl:
function
(target, imgUrl, p, tag) {
if
(!imgUrl)
return
;
var
self = target;
var
loadCb =
function
(err, img){
cc.textureCache.addImage(imgUrl);
var
texture2d =
new
cc.Texture2D();
texture2d.initWithElement(img);
texture2d.handleLoadedTexture();
var
sp =
new
cc.Sprite();
sp.initWithTexture(texture2d);
self.addChild(sp);
sp.x = p.x;
sp.y = p.y;
sp.tag = tag;
};
cc.loader.loadImg(imgUrl, {isCrossOrigin :
false
}, loadCb);
}
|
4、XMLHttpRequest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
var
sendRequest =
function
(url, params, isPost, callback, errorcallback){
if
(url ==
null
|| url ==
''
)
return
;
var
xhr = cc.loader.getXMLHttpRequest();
if
(isPost){
xhr.open(
"POST"
,url);
}
else
{
xhr.open(
"GET"
,url);
}
xhr.setRequestHeader(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
xhr.onreadystatechange =
function
() {
if
(xhr.readyState == 4 && xhr.status == 200){
var
response = xhr.responseText;
if
(callback)
callback(response);
}
else
if
(xhr.readyState == 4 && xhr.status != 200){
var
response = xhr.responseText;
if
(errorcallback)
errorcallback(response);
}
};
if
(params ==
null
|| params ==
""
){
xhr.send();
}
else
{
xhr.send(params);
}
};
|
5、JSON解析以及上述第4条的回调方法
1
2
3
4
5
6
7
8
|
var
callback =
function
(response) {
var
jsonData = JSON.parse(response);
var
data = jsonData[
"users"
];
if
(data){
alert(data[
"name"
]);
// todo something
}
};
|
6、创建切换按钮(开关);
1
2
3
4
5
6
7
8
9
10
|
this
.menuItemToggle =
new
cc.MenuItemToggle(
new
cc.MenuItemImage(res.al_musicOn),
new
cc.MenuItemImage(res.al_musicOff),
this
.toggleMusicCallback,
this
);
this
.menuItemToggle.setPosition(cc.pAdd(cc.visibleRect.topRight, cc.p(-
this
.menuItemToggle.width / 2 - 45, -140)));
this
.menuItemToggle.scaleX = sx*p;
this
.menuItemToggle.scaleY = sy*p;
var
togglemenu =
new
cc.Menu(
this
.menuItemToggle);
togglemenu.anchorX = 0;
togglemenu.anchorY = 0;
togglemenu.x = 0;
togglemenu.y = 0;
this
.addChild(togglemenu, 10);
|
源引:http://www.ipastimes.com/post/9.html