Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2d-JS开发中的一些小技巧大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

1、获取URL中的请求参数的值----此方法接收参数名

1
2
3
4
5
6
functiongetQueryString(Name){
varreg= new RegExp( "(^|&)" +name+ "=([^&]*)(&|$)" , "i" );
varr=window.LOCATIOn.search.substr(1).match(reg);
if (r!=null) return decodeURIComponent(r[2]);
null;
};


2、底图上添加文字---适用于按钮Sprite

6
7
8
9
10
varMyButtonSprite=cc.Sprite.extend({
ctor:function(filename,title,fontName,fontSizE){
this ._super(fileName);
vartitleLabel= cc.LabelTTF(title,fontSizE);
.addChild(titleLabel);
titleLabel.x= .getContentSize().width/2;
titleLabel.y= .getContentSize().height/2;
}
});

3、远程图片加载

10
11
12
13
14
15
16
17
loadImgFromUrl:function(target,imgUrl,p,tag){
(!imgUrl) ;
varself=target;
varloadCb=function(err,img){
cc.textureCache.addImage(imgUrl);
vartexture2d= cc.Texture2D();
texture2d.initWitHelement(img);
texture2d.handleLoadedTexture();
varsp= 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);
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,


4、XMLhttprequest

17
18
19
20
21
22
23
24
25
26
27
28
29
varsendrequest=function(url,params,isPost,callBACk,errorcallBACk){
(url==null||url== '' )
;
varxhr=cc.loader.getXMLhttprequest();
(isPost){
xhr.open( "POST" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,url);
} else {
"GET" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,url);
}
xhr.setrequestHeader( "Content-Type" "application/x-www-form-urlencoded" );
xhr.onreadystatechange=function(){
(xhr.readyState==4&&xhr.status==200){
varresponse=xhr.responseText;
(callBACk)
callBACk(responsE);
else (xhr.readyState==4&&xhr.status!=200){
varresponse=xhr.responseText;
(errorcallBACk)
errorcallBACk(responsE);
}
};
(params==null||params== "" ){
xhr.send();
{
xhr.send(params);
}
5、JSON解析以及上述第4条的回调方法

8
varcallBACk=function(responsE){
varjsonData=JSON.parse(responsE);
vardata=jsonData[ "users" ];
(data){
alert(data[ "name" ]);
//todosomething
}
6、自定义Loading界面

29
30
31
32
@H_56_489@ 33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
varMyLoaderScene=cc.Scene.extend({
_interval:null,
_length:0,
_count:0,
_label:null,
_className: "MyLoaderScene" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
init:function(){
varself= ;
//bg
varbgLayer=self._bgLayer=cc.LayerColor.create(cc.color(32,32,255));
bgLayer.setPosition(cc.visibleRect.bottomLeft);
self.addChild(bgLayer,0);
//loadingpercent
varlabel=self._label=cc.LabelTTF.create( "玩命加载中...0%" "Arial" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,24);
label.setPosition(cc.pAdd(cc.visibleRect.center,cc.p(0,0)));
label.setColor(cc.color(180,180,180));
bgLayer.addChild( ._label,10);
return true ;
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
_initStage:function(img,centerPos){
;
vartexture2d=self._texture2d= cc.Texture2D();
texture2d.initWitHelement(img);
texture2d.handleLoadedTexture();
varlogo=self._logo=cc.Sprite.create(texture2d);
logo.setScale(cc.contentScaleFactor());
logo.x=centerPos.x;
logo.y=centerPos.y;
self._bgLayer.addChild(logo,10);
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
@H_675_759@ onEnter:function(){
;
cc.Node.prototype.onEnter.call(self);
self.@R_674_10906@dule(self._startLoading,0.3);
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
onExit:function(){
cc.Node.prototype.onExit.call( );
vartmpStr= ;
._label.setString(tmpStr);
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
/**
*initwithresources
*@param{Array}resources
*@param{Function|String}cb
*/
initWithresources:function(resources,cb){
(typeofresources== "String" )resources=[resources];
.resources=resources||[];
.cb=cb;
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
_startLoading:function(){
;
self.un@R_674_10906@dule(self._startLoading);
varres=self.resources;
self._length=res.length;
self._count=0;
cc.loader.load(res,function(result,count){self._count=count;},function(){
(self.cb)
self.cb();
});
self.@R_674_10906@dule(self._updatePercent);
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
_updatePercent:function(){
;
varcount=self._count;
varlength=self._length;
varpercent=(count/length*100)|0;
percent=Math.min(percent,100);
self._label.setString( "玩命加载中..." +percent+ "%" );
(count>=length)self.un@R_674_10906@dule(self._updatePercent);
}
});
@H_469_24@myLoaderScene.preload=function(resources,cb){
var_myLoaderScene=null;
(!_myLoaderScenE){
_myLoaderScene= @H_3_118@myLoaderScene();
_myLoaderScene.init();
}
_myLoaderScene.initWithresources(resources,cb);
cc.director.runScene(_myLoaderScenE);
_myLoaderScene;
7、网页跳转

1
window.LOCATIOn.href= "http://www.baidu.com"

8、关于进入游戏时黑屏时间较长的处理方法

1)

<bodystyle="padding:0;margin:0;BACkground:#000;">

删除index.html中<body>标签的样式BACkground: #000;

2)按照自己需要添加编译模块 修改project.json如

1
"modules" :[ "core" "actions" "shape-nodes" "labels" "menus" "transitions" "physics" "chipmunk" "gui" ],serif; font-size:14px"> 3) 压缩game.min.js

4) 找到CCBoot.js中得canvasnode.style.BACkgroundColor = "black";注释掉


本文由CocoaChina会员happyfhc总结,欢迎大家学习与讨论。

来源网址:http://www.cocoachina.com/bbs/read.php?tid=226079

大佬总结

以上是大佬教程为你收集整理的Cocos2d-JS开发中的一些小技巧全部内容,希望文章能够帮你解决Cocos2d-JS开发中的一些小技巧所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。