概述
Cocos Creator
- 生命期回调
- 事件响应处理
- 属性的定义
- API获取节点
- API获取组件
- 访问位置
- 旋转、缩放、锚点、颜色
- 节点的开关
- 组件开关
- 创建节点
- 预设体
- 资源的动态加载
- 场景切换
- 碰撞
- 计时器
- 延时器
- 开启物理系统
生命期回调
onLoad () {} // 组件初始化时执行
satrt () {} // 节点第一次激活时触发
update () {} // 每帧调用
onEnable () {} // 组件启用时触发
onDisable () {} // 组件禁用时触发
onDestroy () {} // 组件销毁时触发
事件响应处理
点击事件 mousedown / mouseup / mousemove …
键盘事件 keyup / keydown …
触摸事件 touchstart / touchend / touchcancel …
-
鼠标
VSC可选操作(cc.Node.EventType.) / “mousedown”
this.node.on(cc.Node.EventType.MOUSE_DOWN, function(event){
cc.log(“鼠标按下了:” + event.getLocation())if(event.getButton() == cc.Event.EventMouse.BUTTON_RIGHT){
cc.log(“右键”)
}
})
一般在onDestroy(){}里停止监听
this.node.off(cc.Node.EventType.MOUSE_DOWN,“方法名字”) -
键盘
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function(event){
cc.log(event.keycode)
//
if(event.keycode == cc.macro.KEY.w){
cc.log(“按了w键”)
}
}) -
触摸事件
this.node.on(cc.Node.EventType.TOUCH_START, funciton(event){
// 判断单指还是双指
cc.log(“触摸:” + event.getID())
}) -
监听自定义事件
this.node.on(“myevent1”, function(event){
cc.log(“自定义事件”)
}){}
发送自定义事件
this.node.emit(“myevent1”)
this.node.dispatchEvent(new cc.Event.EventCustom(“myevent1”, true)) // 参数2:是否冒泡
属性的定义
1.若无@property注解,则不会出现在Cocos Creator属性面板上
2.@property应该指明类型
3.基本类型:string,number,boolean,bigint;Number类型也可以,属于引用类型
@property("string") // 基本类型string可省略写成@property
@property(cc.Node)
4.引用类型:cc.Node(节点),cc.SpriteFrame(图片帧资源),cc.AudioClip(音频资源)
API获取节点
1.当前节点 this.node : cc.Node
let node : cc.Node = this.node
2.父节点 this.node.parent
3.子节点 this.node.children : cc.Node[ ]
4.全局查找
target = cc.find("Canvas/xxx/yyy") // 严格区分大小写
5.查找子节点
target = cc.find("xxx/yyy", someNode)
API获取组件
// 获取组件
let label = node.getComponent(cc.Label)
// 获取自定义类型的组件脚本
let script = node.getComponent("YourScript")
// 获取数组
this.getComponents()
this.getComponentInChindren(cc.Sprite)
访问位置
this.node.x
this.node.y
this.node.setPosition(3,4)
this.node.setPosition(cc.v2(3,4))
旋转、缩放、锚点、颜色
this.node.rotation
this.node.scale
this.node.anchorX
this.node.color = cc.Color.RED
节点的开关
this.node.active = false/true
组件开关
this.enabled = false/true
创建节点
let node = new cc.Node("new")
添加组件:node.addComponent(cc.Sprite)
// 销毁节点
this.node.destroy()
预设体
@property(cc.Prefab)
pre: cc.Prefab = null
// 拖拽预设体到属性检查器的Pre中
// 实例化预设体:
let node = cc.instantiate(this.pre)
// 设置父节点:
node.setParent(this.node)
// 得到当前场景:
cc.director.getScene()
资源的动态加载
- 资源管理器中创建resources文件夹,把图片或者图集拖入此文件夹内
- 加载图片
参数:挂载的节点(层级关系)、指定类型(可选参数)、回调
let self = this
cc.loader.loadRes(“test/land”, cc.SpriteFrame, function(err,sp){
self.getComponent(cc.Sprite).spriteFrame = sp
}) - 加载图集
cc.loader.loadRes(“test/lands”, cc.SpriteAtlas, function(err,atlas:cc.SpriteAtlas){
self.getComponent(cc.Sprite).spriteFrame = atlas.getSpriteFrame(“name”)
})
场景切换
- 资源管理器中创建场景1(game1)、场景2(game2)
- 加载第二个场景(小资源场景):cc.director.loadScene(“game2”,function(){
// 当前已经加载到新的场景里了
}) - 加载第二个场景(大资源场景):
// 先预加载
cc.director.preloadScene(“game2”,function(){
// 这个场景加载到内存了,但是还没有用
cc.director.loadScene(“game2”)
})
-常驻节点(存在各个节点中)
添加:cc.game.addPersisRootNode(this.node)
移除:cc.game.removePersisRootNode(this.node)
碰撞
- 碰撞检测
cc.director.getCollisionManager().enabled = true - 开始碰撞,参数:other碰到的别的物体,self自己
onCollisionEnter(other,self){
cc.log(“碰撞发生” + other.tag)
} - 碰撞持续
onCollisionStay(other){
}
- 碰撞结束
onCollisionEXIT(other){
}
计时器
this.schedule(()=>{}, 时间, 重复几次)
延时器
setTimeout(()=>{
})
开启物理系统
cc.director.getPhysicsManager().enable = true
必须在onload内才能开启
持续更新~
最后
以上就是落后信封为你收集整理的Cocos Creator生命期回调事件响应处理属性的定义API获取节点API获取组件访问位置旋转、缩放、锚点、颜色节点的开关组件开关创建节点预设体资源的动态加载场景切换碰撞计时器延时器开启物理系统的全部内容,希望文章能够帮你解决Cocos Creator生命期回调事件响应处理属性的定义API获取节点API获取组件访问位置旋转、缩放、锚点、颜色节点的开关组件开关创建节点预设体资源的动态加载场景切换碰撞计时器延时器开启物理系统所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复