CocosCreator2.4常用函数


1.场景加载、资源加载(assetbundle暂无)

d=cc.director

d.loadScene(String,function)

d.preloadScene(String,function)

d.pause()

d.isPaused()return Boolean

d.resume()

d.runScene(Scene)

d.getScene()return Scene

cc.game.addPersistRootNode(Node)

cc.game.removePersistRootNode(Node)

2.获取结点

ex. cc.find

this.node.

cc.find("childNode/...",this);

node=cc.instantiate(Prefab)

cc.isValid(obj)//为空或者销毁返回false

var n=this.node;

n.position,n.x,n.y

n.anchorX,n.anchorY

n.angle

n.scale,n.scaleX,n.scaleY

n.width,n.height

n.color,n.opacity

n.name,n.uuid

n.children,n.parent

n.active,n.activeInHierarchy

n._persistNode//Boolean

n.zIndex//渲染顺序,越大越在上面

n.setPosition(x,y)

var pos=n.getPosition()

n.setScale(x,y)

var sca=n.getScale()

n.addChild(childNode,zIndex,nameString)

n.getChildByUuid(String)

n.getChildByName(String)

n.removeChild(childNode,false)

n.removeAllChildren(false)

n.isChildOf(Node)

n.getComponent(String|类)

n.getComponents(String|类)

n.getComponentInChildren(String|类)

n.getComponentsInChildren(String|类)

n.addComponent(String|类)

component.destroy()

n.destroyAllChildren()

n.clear()

n.destroy()

n.walk(prefunc,postfunc)//遍历

3.Action

cc.tween(this.node)

.to(time,{prop:value,prop:value},{easing:"sineOutIn"})

.by(time,{prop:value,prop:value})

.then(cc.tween())

.bezierTo(...)

.call(function)

.repeat(number,cc.tween().by(...))

.repeatForever()

.parallel(cc.tween.to(),cc.tween.to())

.delay(duration)

.start()

cc.tween(this.node).to(...).stop()

4.计时器

this.schedule(function,interval,repeat,delay)

this.scheduleOnce(function,delay)

this.unschedule(function)

this.unscheduleAllCallbacks();

5.事件监听

node.on(eventType|string,function,this)

node.off(eventType|string,function,this)

cc.systemEvent.on(eventType|string,function,this)

cc.systemEvent.off(eventType|string,function,this)

e.getLocation()

e.getLocationX()

e.getLocationY()

6.分辨率(暂无)

cc.view

7.音频

audio=cc.audioEngine;

audioID=audio.playMusic(clip,true)

audio.stopMusic()

audio.pauseMusic()

audio.resumeMusic()

audio.getMusicVolume()

audio.setMusicVolume(0-1)

audio.isMusicPlaying()

audioID=audio.playEffect(clip,false)

audio.setEffectsVolume(0-1)

audio.getEffectsVolume()

audio.pauseEffect(audioID)

audio.pauseAllEffects()

audio.resumeAllEffects()

audio.stopEffect(audioID)

audio.stopAllEffects()

audio.isLoop(audioID)

audio.setLoop(audioID,true)

audio.getDuration(audioID)

audio.getCurrentTime(audioID)

audio.setCurrentTime(audioID,number)

audio.uncache(filepath)//卸载文件

audio.uncacheAll()

8.数学

r是弧度

Math.abs(v)

Math.sin(r)

Math.cos(r)

Math.tan(r)

r=Math.asin(v)

r=Math.acos(v)

r=Math.atan(v)

r=Math.atan2(y,x)

Math.floor(v)//取下

Math.round(v)//四舍五入

Math.random()//0-1包含0,不包含1

Math.max(a,b,c,...)

Math.min(a,b,c,...)

坐标转换

var vec=n.convertToNodeSpaceAR(Vec2)

var vec=n.convertToWorldSpaceAR(Vec2)

Vec2:

v.mag()

v.subSelf(v)

v.mulSelf(n)

v.divSelf(n)

v.scaleSelf(v)//分量相乘

v.negSelf()//取反

v.addSelf(v)

n=v.dot(v)

n=v.cross(v)

v.normalizeSelf()

v.angle(v)

v=v.lerp(vTarget,ratio)//线性插值

boolean=v.equals(v)

v=Vec2.random()//生成单位随机向量

9.动画

anim.play(string)

animClips=anim.getClips()

anim.stop(string)

anim.pause(string)

anim.setCurrentTime(number,string)

anim.addClip(clip,string)

anim.removeClip(clip,false//是否强制)

10.对象池

this.pool=new NodePool();

this.pool.put(Node);

var node=this.pool.get();

var num=this.pool.size();

this.pool.clear();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容