3.8版本官方示例问题

  • Creator 版本:3.8.1

  • 目标平台:谷歌浏览器

  • 重现方式:官方示例

  • 重现概率:必现

按照3.8版本的《快速上手:制作第一个 2D 游戏》给方块创建了一个动画 把这个动画执行之后发现方块原先的位移没有了 就是方块现在只是原地跳跃 并不是像官方一样跳跃前进 注释掉动画代码后方块则正常前行
iShot_2024-01-12_14.18.04

import {
  _decorator,
  Component,
  Vec3,
  EventMouse,
  input,
  Input,
  Animation,
} from "cc";
const { ccclass, property } = _decorator;
export const BLOCK_SIZE = 40;

@ccclass("PlayerController")
export class PlayerController extends Component {
  @property({ type: Animation })
  public BodyAnim: Animation | null = null!;

  // 是否开始跳跃
  private _startJump: boolean = false;
  // 跳跃次数
  private _jumpStep: number = 0;
  // 当前跳跃时间
  private _curJumpTime: number = 0;
  // 跳跃时间
  private _jumpTime: number = 0.1;
  // 跳跃速度
  private _curJumpSpeed: number = 0;
  // 当前位置
  private _curPos: Vec3 = new Vec3();
  // 当前位置
  private _deltaPos: Vec3 = new Vec3(0, 0, 0);
  // 目标位置
  private _targetPos: Vec3 = new Vec3();
  start() {
    input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
  }

  jumpByStep(step: number) {
    if (this._startJump) {
      return;
    }
    this._startJump = true;
    this._jumpStep = step;
    this._curJumpTime = 0;
    this._curJumpSpeed = (this._jumpStep * BLOCK_SIZE) / this._jumpTime;
    this.node.getPosition(this._curPos);
    Vec3.add(
      this._targetPos,
      this._curPos,
      new Vec3(this._jumpStep * BLOCK_SIZE, 0, 0)
    );
    if (this.BodyAnim) {
      if (step === 1) {
        this.BodyAnim.play("stepOne");
      } else {
        this.BodyAnim.play("stepTwo");
      }
    }
  }

  onMouseUp(event: EventMouse) {
    if (event.getButton() === 0) {
      this.jumpByStep(1);
    } else if (event.getButton() === 2) {
      this.jumpByStep(2);
    }
  }

  update(deltaTime: number) {
    if (this._startJump) {
      this._curJumpTime += deltaTime;
      if (this._curJumpTime > this._jumpTime) {
        // end
        this.node.setPosition(this._targetPos);
        this._startJump = false;
      } else {
        // tween
        this.node.getPosition(this._curPos);
        this._deltaPos.x = this._curJumpSpeed * deltaTime;
        Vec3.add(this._curPos, this._curPos, this._deltaPos);
        this.node.setPosition(this._curPos);
      }
    }
  }
}

你的动画是不是有修改位置啊

您好,是的 但是我理解的动画不就是得修改位置吗
下图就是三段动画修改的position的y位置
image

image x y z 都修改了的 你update 修改x 给动画还原了

我又仔细的看了下代码 确实是因为这个 但是按照官方文档的思路就是这样写的 这是不是官方示例有问题

你好,我也遇到了同样的问题,请问这个怎么解决

问题解决了,示例中创建ainmation的oneStep时,position属性展开后,去掉x后面的选中状态

如下图:1706690181375

感谢老哥 确实解决了 :smiley:

我的这样改了不行, 方便把整个demo 项目源码给我下吗?