Cocos Creator

    // 刷新消息
    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("鼠标移进来了,位置在:"
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值