美文网首页
常用操作

常用操作

作者: Amy_yqh | 来源:发表于2021-11-21 23:26 被阅读0次

    一、图文按钮

    在场景中创建button 节点,把label 节点删除,把图片素材拖拽到background下,替换原来的label


    image.png
    image.png

    二、点击按钮切换图片

    1、定义2个图片变量

        @property(cc.SpriteFrame)
        face1:cc.SpriteFrame = null
    
        @property(cc.SpriteFrame)
        face2:cc.SpriteFrame = null
    

    2、在cocos编辑器设置face1、face2的资源


    image.png

    3、点击按钮,切换图片路径

    changeFace(){
            // this.node.scaleX = 0-this.node.scaleX // 部分图片可以这么操作
            let sprite:cc.Sprite = this.node.getComponent(cc.Sprite)
            if(this.faceLeft){
                sprite.spriteFrame = this.face1
            }else{
                sprite.spriteFrame = this.face2
            }
        }
    

    三、坐标

            //定义坐标
            let pos1 = new cc.Vec2(100,200)
            let pos2 = cc.v2(100,200)
    
            let pos3 = new cc.Vec3(100,200,0)
            let pos4 = cc.v3(100,200,0)
            
           //获取坐标
            let pos:cc.Vec2 = this.node.getPosition()
    
            //设置坐标
            this.node.setPosition(cc.v3(-200,100,0))
            this.node.setScale(cc.v3(1,1,0))
            cc.log(pos)
    

    四、帧操作

    1、帧动画
     update (dt) {
            if(this.node.x<-200) return
            this.node.x-=5
     }
    在update钩子函数中,绘制动画效果,平均每秒60帧(fps:60)
    
    2、设置帧频率
     onLoad () {
            cc.game.setFrameRate(30)
    }
    在onLoad方法中,设置帧率,该方法的全局的方法,把该脚本挂载到最外
    层的Canvas中,节点的加载顺序是:父再到子
    

    五、键盘事件(通过方向判断移动方向)

     onLoad () {
           cc.systemEvent.on('keydown',this.keyDown,this)
        }
    keyDown(e:cc.Event.EventKeyboard){
            if(e.keyCode== cc.macro.KEY.left){
                this.direction = cc.v2(-1,0)
            }else if(e.keyCode == cc.macro.KEY.right){
                this.direction = cc.v2(1,0)
            }else if(e.keyCode == cc.macro.KEY.up){
                this.direction = cc.v2(0,1)
            }else if(e.keyCode == cc.macro.KEY.down){
                this.direction = cc.v2(0,-1)
            }else if(e.keyCode == cc.macro.KEY.space){
                this.direction = null
            }
    }
    
    update (dt) {
            if(this.direction ==null) { 
                return
            }
            let pos:cc.Vec2 = this.node.getPosition()
            pos.x += this.step * this.direction.x
            pos.y += this.step * this.direction.y
            this.node.setPosition(pos)
        }
    

    六、计时器

    计时器封装在cc.component中
    
    启动计时器
    // callback 回调函数, interval多长事件执行一次,reapeat执行多少次,
    //delay延迟多长后启动
    comp.schedule(callback,interver,repeat,delay) 
    
    删除定时器
    comp.unschedule(callback)
    
    
    例子
    export default class NewClass extends cc.Component {
        label:cc.Label = null
        text: string = '';
        index:number = 0
    
        onLoad () {
            this.label = this.getComponent(cc.Label)
            this.text = this.label.string
            this.label.string = ''
    
            this.schedule(this.onTimer,0.3)
        }
        onTimer(){
            this.index++
            let str:string = this.text.substr(0,this.index)
            this.label.string = str
            
            if(this.index>=this.text.length){
                this.unschedule(this.onTimer)
            }
        }
    }
    

    七、方位角

    1、角度表示
    let angle:number = 45
    2、规范化表示
    let direction :cc.Vec2 = pos.normalize()
    即(0.7071,0.7071),分别为cos和sin的值,
    

    八、角度的计算

    角度的计算:radian = a.signAngle(b) 
    

    相关文章

      网友评论

          本文标题:常用操作

          本文链接:https://www.haomeiwen.com/subject/lowytrtx.html