• cocos 入门2


    今天学习了定时器,帧动画,以及键盘控制,缓动,还是上代码吧,方便自己后面复习

    autowalk.ts
    
    // Learn TypeScript:
    //  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
    // Learn Attribute:
    //  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
    // Learn life-cycle callbacks:
    //  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
    
    const {ccclass, property} = cc._decorator;
    
    @ccclass
    export default class NewClass extends cc.Component {
    
        @property
        speed:number=3
    
        @property
        director:cc.Vec2=null
    
        // LIFE-CYCLE CALLBACKS:
    
        onLoad () {
            cc.systemEvent.on('keydown',this.keyDownHandle,this);
        }
    
        start () {
    
        }
        keyDownHandle(e:cc.Event.EventKeyboard){
            if(e.keyCode===cc.macro.KEY.left){
                this.director=cc.v2(-1,0)
            }else if(e.keyCode===cc.macro.KEY.right){
                this.director=cc.v2(1,0)
            }else if(e.keyCode===cc.macro.KEY.up){
                this.director=cc.v2(0,1)
            }else if(e.keyCode===cc.macro.KEY.down){
                this.director=cc.v2(0,-1)
            }else if(e.keyCode===cc.macro.KEY.space){
                this.director=null
            }
        }
    
        update (dt) {
            // cc.log(dt)
            if(!this.director){
                return
            }
            let pos:cc.Vec2=this.node.getPosition();
            pos.x+=this.speed*this.director.x;
            pos.y+=this.speed*this.director.y;
            this.node.setPosition(pos)
        }
    }
    ball.ts
    缓动
    
    // Learn TypeScript:
    //  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
    // Learn Attribute:
    //  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
    // Learn life-cycle callbacks:
    //  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
    
    const {ccclass, property} = cc._decorator;
    
    @ccclass
    export default class NewClass extends cc.Component {
    
        @property(cc.Label)
        label: cc.Label = null;
    
        @property
        text: string = 'hello';
    
        // LIFE-CYCLE CALLBACKS:
    
        onLoad () {
            this.node.on('touchstart',this.touchHandle,this);
        }
    
        start () {
    
        }
        touchHandle(){
            let h:number=300;
            cc.tween(this.node)
            .by(0.5,{position:cc.v3(0,-h,0)},{easing:'quadIn'})
            .by(0.2,{position:cc.v3(0,h/6)},{easing:'quadOut'})
            .by(0.1,{position:cc.v3(0,-h/6,0)},{easing:'quadIn'})
            .start();


          
     cc.tween(node)
            .by(1.5,{position:cc.v3(300,0,0)},{easing:'quadIn'})
            .to(1,{position:cc.v3(100,0,0)},{easing:'quadOut'})
            .bezierTo(1,cc.v2(100,10),cc.v2(10,10),cc.v2(200,200))
            .start()
    
        }
    
        // update (dt) {}
    }




    帧动画
    
    // Learn TypeScript:
    //  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
    // Learn Attribute:
    //  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
    // Learn life-cycle callbacks:
    //  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
    
    const {ccclass, property} = cc._decorator;
    
    @ccclass
    export default class NewClass extends cc.Component {
    
        @property(cc.Label)
        label: cc.Label = null;
    
        @property
        text: string = 'hello';
    
        // LIFE-CYCLE CALLBACKS:
    
        onLoad () {
            //设置帧率
            cc.game.setFrameRate(60);
        }
    
        start () {
    
        }
    
        update (dt) {
            //做帧动画
            if(this.node.x>250){
                return
            }
            this.node.x+=5
        }
    }
    定时器
    // Learn TypeScript:
    //  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
    // Learn Attribute:
    //  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
    // Learn life-cycle callbacks:
    //  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
    
    const {ccclass, property} = cc._decorator;
    
    @ccclass
    export default class NewClass extends cc.Component {
    
        @property(cc.Label)
        label: cc.Label = null;
    
        @property
        text: string = '';
        index:number=0
    
        // LIFE-CYCLE CALLBACKS:
    
        onLoad () {
            //一定要注意,这里的label其实只是一个属性变量,从label获取值还得先获取到这个组件
            this.label = this.getComponent(cc.Label);
            this.text = this.label.string; // 取得完整的文本
            cc.log(this.text,'--------------------')
            cc.log(this.label)
            cc.log(this.label.string)
            this.label.string='123'
            this.label.string = '';  // 清空文本,从头显示
    
            this.schedule(this.onTimer, 0.3);
        }
    
        start () {
            
        }
        onTimer(){
            this.index++;
            let str:string=this.text.substr(0,this.index);
            this.label.string=str;
            if(this.start.length>=this.text.length){
                this.unschedule(this.onTimer)
            }
    
        }
    
        // update (dt) {}
    }
  • 相关阅读:
    让程序在后台长久运行的方法
    unrecognized selector sent to instance的定位
    CocoaLumberjack调试的简单使用
    #pragma
    __bridge __bridge_retained __bridge_transfer
    关于c#继承
    C#__ 模拟鼠标单击事件
    C# 调用win api获取chrome浏览器中地址
    认识安卓
    源码解析-Volley(转自codeKK)
  • 原文地址:https://www.cnblogs.com/ysla/p/14788073.html
Copyright © 2020-2023  润新知