【Cocos TypeScript 零基础 17.1】

羊了个羊关于运动动画

矩形移动

通过缓动动画移动

import { _decorator, Component, Node, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ts_start')
export class ts_start extends Component {
    start() {

    }

    update(deltaTime: number) {
        const pos1 = this.node.getPosition()
        if (pos1.x == 300 && pos1.y == 300){
            this.node.scale = new Vec3(1 , 1)  //  翻转一下
            this.on_anim(new Vec3(300 , -300))
        }
        else if (pos1.x == 300 && pos1.y == -300){this.on_anim(new Vec3(-300 , -300))}
        else if (pos1.x == -300 && pos1.y == -300){this.on_anim(new Vec3(-300 , 300))}
        else if (pos1.x == -300 && pos1.y == 300){
            this.node.scale = new Vec3(-1 , 1)  //  翻转一下
            this.on_anim(new Vec3(300 , 300))
        }
        else {return}
    }
    on_anim(pos:Vec3){
        tween(this.node)
        .to(2 , {position:pos})
        .start()
    }
}

但是好像cocos 在进行缓动时到达pos坐标有细微差异
导致判断失效,动画无法循环播放
但理论上是可以循环播放的

圆形移动

在<见缝插针_点击跳转>小游戏里,针脚本中
理论上建立一个节点,使其旋转
羊绑定在这个节点上,羊保持它的角度

const rw = this.node.getWorldRotation()
this.node.setWorldRotation(rw)

应该就能实现圆形移动,懒得验证了
小技巧_翻转

this.node.scale = new Vec3(-1 , 1)  //  翻转一下

在这里插入图片描述
因为矩形移动有BUG,我就又改为圆形移动

    start() {
        this.on_aud_vol()
        this.node.getWorldRotation(this.yang_rot)    //  获取羊的世界坐标系下角度
        //console.log(this.yang_rot)
    }

    on_move_yuan(){     //  羊圆形移动
        this.fu_no.angle += 1
        if (this.fu_no.angle == 360){this.fu_no.angle = 0}
        this.node.setWorldRotation(this.yang_rot)   //  保持角度
        const p = this.node.getWorldPosition()  //  获取羊的世界坐标
        console.log(p.y)
        if (p.y < 645) {this.node.scale = new Vec3(-1 , 1)}
        else {this.node.scale = new Vec3(1 , 1)}
    }

    update(deltaTime: number) {
        //this.on_move_fang()
        this.on_move_yuan()
    }

多点移动

老师方法
在这里插入图片描述
不要瞎魔改,我直接写一个死循环判定,cocos直接卡死
小技巧_绑定数组
老师代码,AI回答,都是用下面代码实现组件数组绑定
但我的 cocos 无法实现,我的版本是 3.8.5

@property([Node]) no : Node[] = [];

BUG

我的 cocos 出了点问题,调试运行的时候,
按钮按不到,滑动组件也按不到
改为浏览器调试运行,按键 和 滑动组件 都无异常
但背景音乐只有在滑动组件点击后才会出来
实验了N次后
并不是点击滑动组件才有声音
是点击 触屏 内任意地方都会出声音
我用的浏览器是夸克,所以不知道是不是浏览器的问题
用模拟器也OK
暂时就这样(应该是cocos 的问题)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值