---
theme: juejin
素材
爱给素材网
www.aigei.com/
OepnGameArt
opengameart.org/
Kenney
www.kenney.nl/
Ludum Dare**
ldjam.com/
itch.io
itch.io/
花瓣
huaban.com/
Game Icon
4131 free SVG and PNG icons for your games or apps | Game-icons.net
任天堂
ドット絵ダウンロードサイト DOTOWN|無料の素材サイト (maeda-design-room.net)
Craft Peaks
Free Sound
kenney.nl/assets
https://opengameart**.org
itch.io
www.textures.com
ambientcg.com
www.sharetextures.com
www.cgbookcase.com
tokudaya.net
kopacurve.blog.fc2.com
nanamiyuki.com
game-icons.net
图集
图集工具:texturePacker 资源目录assets/resources
节点
//获得子节点
this.node.children[0]
this.node.getChildByName('abc')
cc.find('Main/abc')
//设置父节点
this.node.getParent()
thiis.node.setParent(ddd)
//移除所有子节点
this.node.removeAllChildren()
//移除单个子节点
this.node.removeChild(aaa)
//从某个父节点移除
this.node.removeFromParent(ddd)
//访问node属性
this.node.x
this.node.y
//设置位置
this.node.setPosition(x,y)
//节点开关
this.node.active = true
复制代码
组件
//获取精灵组件
let sprite = this.node.getComponents(cc.Sprite)
//精灵组件开关
sprite.enable = false
//获取子节点的精灵组件
this.node.getComponentInChildren(cc.Sprite)
//获取UI组件属性
this.node..getComponent(cc.UITransform).height
复制代码
cc工具
//查找节点
cc.find('aaa/bbb')
//颜色工具
cc.color.RED
//创建节点
let node = new cc.Node('new')
//添加精灵组件
node.addComponent(cc.Sprite)
//队列动作
let sqe = cc.squence(action,action2)
this.node.runAction(sqe)
//重复动作
let rep = cc.repeat(sqe,3)//重复3次
let rep = cc.repeatForever(sqe)//无限重复
this.node.runAction(rep)
//并列动作
let move = cc.moveTo(1,1,1)
let color = cc.tintTo(3,100,100,20)
let spawn = cc.spawn(move,color)
this.node.runAction(spawn)
//延迟
let sqe = cc.squence(action,action2,cc.delayTime(1),callbackFunction)
复制代码
预设体
把节点拖到资源(assets)目录生成预设体
//设置脚本预设体属性
@property(cc.Prefab)
pre: cc.Prefab = null
//手动拖拽预设体与脚本属性进行关联
//实例化预设体
let node = cc.instantiate(this.pre)
//设置父节点
node.setParent(this.node)
复制代码
动态加载资源
// 加载 SpriteFrame,image 是 ImageAsset,spriteFrame 是 image/spriteFrame,texture 是 image/texture
resources.load("test_assets/image/spriteFrame", SpriteFrame, (err, spriteFrame) => {
this.node.getComponent(Sprite).spriteFrame = spriteFrame;
});
// 加载 texture
resources.load("test_assets/image/texture", Texture2D, (err: any, texture: Texture2D) => {
const spriteFrame = new SpriteFrame();
spriteFrame.texture = texture;
this.node.getComponent(Sprite).spriteFrame = spriteFrame;
});
// 加载 SpriteAtlas(图集),并且获取其中的一个 SpriteFrame
// 注意 atlas 资源文件(plist)通常会和一个同名的图片文件(png)放在一个目录下, 所以需要在第二个参数指定资源类型
resources.load("test_assets/sheep", SpriteAtlas, (err, atlas) => {
const frame = atlas.getSpriteFrame('sheep_down_0');
sprite.spriteFrame = frame;
});
//预加载
resources.preload('test_assets/image/spriteFrame', SpriteFrame);
复制代码
场景
//加载场景
cc.director.loadScene('scene2',()=>{
//加载完成
})
//预加载场景
cc.director.preloadScene('scene2',()=>{
//预加载完成
})
//永久节点,切换场景不会被销毁
cc.game.addPersistRootNode(this.node)
//永久移除节点
cc.game.removePersistRootNode(this.node)
复制代码
键鼠事件
//监听鼠标相关事件
this.node.on(cc.Node.EventType.MOUSE_DOWN, event=>{
//获取点击位置
event.getLocation()
//监听左右键
if(event.getButton() == cc.Event.EventMouse.BUTTON_RIGHT){
//...
}
})
//监听键盘事件
cc.ststemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, event =>{
if(event.keyCode === cc.marco.KEY.w){
//...
}
})
//监听触摸事件
this.node.on(cc.Node.EventType.Touch_TOUCH, event =>{
//触摸哪个手指
event.getID()
//触摸位置
event.getLocation()
event.getLocationX()
event.getLocationY()
})
//监听自定义事件
this.node.on('myevent1', event=>{
//...
})
//触发自定义事件
this.node.emit('myevent1')
//冒泡触发
this.node.dispatchEvent(new cc.Event.EventCuston('myevent1',true))
复制代码
碰撞检测
Cocos Creator 3.8 手册 - 2D 物理
对节点添加刚体组件(Physics2D - RigidBody2D) 对节点添加碰撞组件(Physics2D - BoxCollider2D)
- 目标节点也需要添加刚体组件,碰撞双方不能同时设置未Static静态刚体,否则无法触发碰撞回调.
- 需要监听碰撞事件开启Enabled Contact Listener
let collider = this.node.getComponent(cc.BoxCollider2D)
collider.on(cc.Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
//碰撞回调
onBeginContact(
self: Collider2D,
other: Collider2D,
contact: IPhysics2DContact | null
) {
//...
other.tag
}
复制代码
音频
Cocos Creator 3.8 手册 - AudioSource 组件参考
射线
Cocos Creator 3.8 手册 - 2D 物理系统