cocos 动态创建节点

动态创建节点
import zi_js from './zi.js';
cc.Class({
    extends: cc.Component,
    properties: {
        zi:{
            default: null, 
            type: cc.SpriteFrame,
        },
        bao:{
            default: null, 
            type: cc.SpriteFrame,
        }
    },
    onLoad () {
        this.node.on('touchstart', this.ontouchstart, this)
        this.node.on('touchmove', this.ontouchmove, this)
        this.node.on('touchend', this.ontouchend, this)
        this.node.on('touchcancel', this.ontouchcancel, this)
    },
    ontouchstart(e){
        //新建一个节点
        let node = new cc.Node();
        //添加一个精灵属性
        let sp=node.addComponent(cc.Sprite)
        //设置精灵的图片,图片是上方接受的
        sp.spriteFrame=this.zi
        //把新建的节点挂载到,当前节点下方
        node.parent=this.node
        //设置新建节点的属性,位置
        node.setPosition(cc.v2(0, 60))
        //添加子弹 节点的js,参数是上方引入的
        let zi_js= node.addComponent(zi_js)
        //把图片添加到 新创建的js 里面
        zi_js.bao=this.bao
    },
    ontouchmove(e){},
    ontouchend(e){},
    ontouchcancel(e){},
    start () {},
    // update (dt) {},
});
给动态创建的节点,添加,js 和图片
cc.Class({
    extends: cc.Component,
    properties: {

    },
    onLoad () {
        let y=0 
        let wo=() =>{
            if(y>300){
                //this.unschedule(wo);
                //获取节点
                let bg=this.node.getComponent(cc.Sprite)
                //切换图片
                bg.spriteFrame=this.bao
                //执行缓动效果
                this.node.scale=0.1
                //使用缓动
                cc.tween(this.node)
                .to(0.2, { scale: 0.8,opacity: 0.5 })
                .call(
                    //执行缓动回调
                    ()=>{
                        this.unschedule(wo);
                    }
                )
                .start()
                return
           }else{
            y+=10
            this.node.y=y
           }
        }
        //添加定时器,重复执行
       this.schedule(wo, 0.02);
    },
    start () {},
    update (dt) {},
});

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两个人的幸福online

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值