// 刷新消息
setMessage(name: string, content: string){
// 从子节点中拿到cc.Label组件
this.node.children[0].getComponent(cc.Label).string = name;
this.node.children[1].getComponent(cc.Label).string = content;
}
节点常用属性方法
const {ccclass, property} = cc._decorator;
@ccclass
export default class Text extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
// LIFE-CYCLE CALLBACKS:
// 初始化调用
start () {
// 获得子节点
// this.node.children[0];
// this.node.getChildByName("abc");
// cc.find("Canvas/Main Camera")
// 获得父节点
// this.node.getParent();
// 设置父节点
// this.node.setParent(ddd);
// 移除所有子节点
// this.node.removeAllChildren();
// 移除某个子节点
// this.node.removeChild(ddd);
// 从父节点中移除
// this.node.removeFromParent();
// 访问位置
// 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;
// 组件开关
// this.enabled = false;
// 获取组件
// let sprit = this.getComponent(cc.Sprite);
// 精灵组件开关
// sprite.enabled
// 从子物体里面获取精灵组件 如果获取失败,则返回null
this.getComponentInChildren(cc.Sprite);
}
// 每帧调用
update (dt) {
}
}
预设体的使用
const {ccclass, property} = cc._decorator;
@ccclass
export default class Text extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
// 预设体
@property(cc.Prefab)
pre: cc.Prefab = null;
// LIFE-CYCLE CALLBACKS:
// 初始化调用
start () {
// // 创建节点
// let node = new cc.Node("new");
// // 添加组件
// node.addComponent(cc.Sprite);
// 实例化预设体 最后返回是一个节点
let node = cc.instantiate(this.pre);
// 设置父节点
node.setParent(this.node);
}
// 每帧调用
update (dt) {
}
}
动态加载
const {ccclass, property} = cc._decorator;
@ccclass
export default class Text extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
// LIFE-CYCLE CALLBACKS:
// 初始化调用
start () {
let self = this;
// 第一个位置可填网址http://www.baidu.com/1.png 第二个位置可填写指定类型,此处为图片
// cc.loader.loadRes("test/白", cc.SpriteFrame, function(err, sp){
// self.getComponent(cc.Sprite).spriteFrame = sp;
// });
// 图集加载
// 这前面还可以写成 cc.resources.load
cc.loader.loadRes("test/1", cc.SpriteAtlas, function(err, atlas: cc.SpriteAtlas){
self.getComponent(cc.Sprite).spriteFrame = atlas.getSpriteFrame("bg_day");
});
}
// 每帧调用
update (dt) {
}
}
场景管理
const {ccclass, property} = cc._decorator;
@ccclass
export default class Text extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
// LIFE-CYCLE CALLBACKS:
// 初始化调用
start () {
// 加载第二个场景
// 小资源加载法
// cc.director.loadScene("game2", function(){
// // 当前已经记载到新的场景里了
// });
// 大型游戏加载
// 预加载
// cc.director.preloadScene("game2",function(){
// // 这个场景加载到内存了,但是还没有用
// cc.director.loadScene("game2");
// });
// 添加常驻节点
cc.game.addPersistRootNode(this.node);
cc.game.removePersistRootNode(this.node);
}
// 每帧调用
update (dt) {
}
}
键鼠事件
const {ccclass, property} = cc._decorator;
@ccclass
export default class Text extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
// LIFE-CYCLE CALLBACKS:
// 初始化调用
start () {
// 鼠标事件
this.node.on(cc.Node.EventType.MOUSE_DOWN,function(event){
console.debug("鼠标按下了,位置在:"+ event.getLocation());
});
this.node.on(cc.Node.EventType.MOUSE_DOWN,function(event){
if(event.getButton() == cc.Event.EventMouse.BUTTON_LEFT){
console.debug("左键");
}
if(event.getButton() == cc.Event.EventMouse.BUTTON_RIGHT){
console.debug("右键");
}
});
this.node.on(cc.Node.EventType.MOUSE_ENTER,function(event){
console.debug("鼠标移进来了,位置在:"