当前位置: 首页 > news >正文

【Cocos TypeScript 零基础 15.1】

目录

  • 见缝插针
    • UI脚本
    • 针脚本
    • 球脚本
    • 心得_旋转
    • 心得_更改父节点
    • 心得_缓动动画
    • 成品展示图

见缝插针

本人只是看了老师的大纲,中途不明白不会的时候再去看的视频
所以代码可能与老师代码有出入
SIKI_学院_点击跳转

UI脚本

import { _decorator, Camera, color, Component, director, instantiate, Label, math, Node, Prefab, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ts_ui')
export class ts_ui extends Component {
    static inthis : ts_ui
    static getinthis() : ts_ui {return this.inthis}

    @property(Prefab) pin : Prefab = null
    @property(Node) cam : Node = null
    pin_num : number = 0    //  是否生成pin

    @property(Label) ui_fen : Label = null
    fen : number = 0
    @property(Camera) camera : Camera = null
    @property(Node) but : Node = null

    start() {
        ts_ui.inthis = this
        this.schedule(this.on_rate,1)
        this.on_fen(0)
    }

    update(deltaTime: number) {
        
    }
    on_rate(){
        if (this.pin_num == 1){return}  //  是否生成
        const p = instantiate(this.pin)
        this.cam.addChild(p)
        p.setPosition(0 , -640)
        this.pin_num = 1
    }
    on_fen(num : number){
        this.fen += num
        this.ui_fen.string = this.fen.toString()
    }
    on_end(){
        this.but.active = true
        this.on_anim()
        this.scheduleOnce(function(){director.pause()},1)
    }
    on_anim(){  //  结束缓动动画函数
        let new_col = new math.Color()
        new_col.r = 60
        new_col.g = 5
        new_col.b = 5
        new_col.a = 255
        tween(this.camera)
        .to(1 , {orthoHeight : 450 , clearColor : new_col})
        .start()
    }
    on_reset(){
        director.resume()
        director.loadScene(`s1`)
    }
}

针脚本

import { _decorator, Collider2D, Component, Contact2DType, Input, input, Node } from 'cc';
import { ts_circle } from './ts_circle';
import { ts_ui } from './ts_ui';
const { ccclass, property } = _decorator;

@ccclass('ts_pin_s')
export class ts_pin_s extends Component {
    move_sp : number = -2   //  -2刚生成时 -1等待发射 0发射 1完成碰撞
    start() {
        const col = this.getComponent(Collider2D)
        if (col){col.on(Contact2DType.BEGIN_CONTACT,this.on_bc,this)}   //  开启碰撞
        else {console.log(`针头 开启碰撞异常`)}
        input.on(Input.EventType.TOUCH_END , this.on_te , this)     //  开启触摸
    }
    on_bc (me : Collider2D , oth : Collider2D){
        console.log(`针头碰撞`,oth.name)
        if (oth.name == `Circle<CircleCollider2D>`){
            const pw = this.node.getWorldPosition()
            const rw = this.node.getWorldRotation()
            const cir = ts_circle.getinthis()
            this.node.setParent(cir.node)       //  更新父节点
            this.node.setWorldPosition(pw)
            this.node.setWorldRotation(rw)
            this.move_sp = 1
            const ui = ts_ui.getinthis()
            ui.pin_num = 0
            ui.on_fen(1)
        }
        if (oth.name == `Pin<BoxCollider2D>`){
            ts_ui.getinthis().on_end()
        }
    }
    on_te(){
        if (this.move_sp == -1){this.move_sp = 0}
    }
    update(deltaTime: number) {
        this.move(deltaTime)
    }
    move(deltaTime: number){
        if (this.move_sp >= 1){return}
        const pos = this.node.getPosition()
        if (this.move_sp == -2){
            if (pos.y <= -500){this.node.setPosition(pos.x , pos.y + deltaTime * 500)}      //  新生成速度
            else {this.move_sp = -1}
        }
        if (this.move_sp == -1){return}
        if (this.move_sp == 0){this.node.setPosition(pos.x , pos.y + deltaTime * 1000)}      //  发射速度
    }
}

move 函数处于性能考虑
应该在条件判断成立时 返回的,不应该多个IF轮流判定

球脚本

import { _decorator, CircleCollider2D, Collider2D, Component, Contact2DType, Input, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ts_circle')
export class ts_circle extends Component {
    static inthis : ts_circle
    static getinthis() : ts_circle {return this.inthis}

    start() {
        ts_circle.inthis = this
        const col = this.getComponent(Collider2D)
        if (col){col.on(Contact2DType.BEGIN_CONTACT,this.on_bc,this)}
        else {console.log(`球 开启碰撞异常`)}
    }
    on_bc(me : Node , oth : Node){
        console.log(`球 碰撞` , oth.name)
    }
    update(deltaTime: number) {
        this.node.angle += 2
        if (this.node.angle >= 360){this.node.angle = 0}
    }
}

心得_旋转

在这里插入图片描述
项目设置 > 功能裁剪 > 2D物理系统 > 内置2D物理系统
在不改内置的情况下

this.node.angle += 2    //  旋转角度速度

球旋转会卡住不动,取消刚体组件也可以使其正常旋转,但碰撞就会有点麻烦

心得_更改父节点

在变更父节点的时候,子节点的位置和角度会被重置
不想重置,就需要记录之前的位置和角度,更换后再设置回来

        if (oth.name == `Circle<CircleCollider2D>`){
            const pw = this.node.getWorldPosition()
            const rw = this.node.getWorldRotation()
            const cir = ts_circle.getinthis()
            this.node.setParent(cir.node)       //  更新父节点
            this.node.setWorldPosition(pw)
            this.node.setWorldRotation(rw)
            this.move_sp = 1
            const ui = ts_ui.getinthis()
            ui.pin_num = 0
            ui.on_fen(1)
        }

心得_缓动动画

还没有仔细研究,看了老师的视频,依葫芦画瓢
但看使用情况来看,以下是个人理解
tween 传入缓动组件
to 传入 1缓动执行时间 2组件需要缓动变更的属性
start 开始

    on_anim(){  //  结束缓动动画函数
        let new_col = new math.Color()
        new_col.r = 60
        new_col.g = 5
        new_col.b = 5
        new_col.a = 255
        tween(this.camera)
        .to(1 , {orthoHeight : 450 , clearColor : new_col})
        .start()
    }

成品展示图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://www.dtcms.com/a/20119.html

相关文章:

  • 如何在Spring Boot中配置分布式配置中心
  • 2025-02-13 学习记录--C/C++-PTA 7-17 爬动的蠕虫
  • c#自动更新-源码
  • WPF的Prism框架的使用
  • 算法刷题-链表系列-两两交换链表结点、删除链表的倒数第n个元素
  • C#打印设计器
  • 72.git指南(简单)
  • SpringCloud系列教程:微服务的未来 (五)枚举处理器、JSON处理器、分页插件实现
  • DeepSeek24小时写作机器人,持续创作高质量文案
  • pnpm的使用
  • Python 字典思维导图
  • 制药行业 BI 可视化数据分析方案
  • dedecms 开放重定向漏洞(附脚本)(CVE-2024-57241)
  • Docker学习
  • dma_ddr 的编写 通过mig控制ddr3
  • MySQL数据库入门到大蛇尚硅谷宋红康老师笔记 基础篇 part 13
  • 掌握SQL多表连接查询_轻松处理复杂数据关系
  • [0689].第04节:Kafka与第三方的集成 – Kafka集成SpringBoot
  • 《Spring实战》(第6版)第1章 Spring起步
  • 【数据结构】(9) 优先级队列(堆)
  • floodfill算法系列一>太平洋大西洋水流问题
  • 【第3章:卷积神经网络(CNN)——3.6 CNN的高级特性与优化策略】
  • 如何使用 DeepSeek 和 Dexscreener 构建免费的 AI 加密交易机器人?
  • EasyExcel 复杂填充
  • DeepSeek接入网络安全领域,AI高效驱动,重新定义网络防御边界!
  • UniApp 中制作一个横向滚动工具栏
  • MyBatis:动态SQL高级标签使用方法指南
  • 数据管理的四大基石:通俗解读数据中台、数据仓库、数据治理和主数据
  • 《千多桃花一世开》:南胥月为何爱暮悬铃
  • 输电杆塔沉降智能监测系统:如何用数据守护电网安全