rt
加载完prefab之后,会显示默认的sprite。在加载完最新的sprite之前,会有短暂的间隔。导致的结果就是会闪一下。怎么处理这种情况?
默认的图片用一张透明的图
加载sprite也是动态加载的么?如果sprite也是的话,那么问题是你这个sprite加载的时间太慢,解决办法就是提前加载你需要的图片资源,在onload里替换图片资源大概率是不会闪的
将 sprite先隐藏,我是直接一开始设置 spriteFrame = null 。或者确保新的 spriteFrame 加载后,再addChild整个 prefab,不过一般没必要这样做。
中间还是会显示一下空的
都是动态加载的,并且还是两个文件里面,提前加载是个办法,就是逻辑上就会很麻烦
中间嵌套了好多层函数。并且prefab也需要先addchild,不然也是显示空的。很多层资源嵌套。
图片多么?不多的话 直接 拉进预制体里就行了,十几张也都拉进去的话也还能接受,这样的话就不用提前加载了
太多了,没办法拉预制体里面
那就用到的时候出个loading界面,把图片资源加载完 再进入这个预制体,或者就是在loading界面加载一部分,看取舍吧,loading加载太多的话,首屏时长也会过长
不是整个界面,只是一个节点上的内容。没办法用loading界面
解决这种问题就是预加载资源。再次一点先清掉默认资源,然后直接通过代码设置默认资源。
不然你就先加载图片,加载完了在加载预制体
预加载不太好处理,很多地方用,所以想问问有什么简单的办法 
看来只能这样,麻烦点修改逻辑了
跟你想一个办法 你加载完预制正常add 然后设置在屏幕外的位置,等里面的图片啊 什么的初始化完成后再设置回去
新手借楼求助!3.8.5版本,官方案例快速上手3d游戏案例,body子节点动画无法应用到主节点player上
playerController源码:
import { _decorator, Component, input, Input, EventMouse, Vec3, Animation, Node } from ‘cc’;
const { ccclass, property } = _decorator;
@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 _deltaPos: Vec3 = new Vec3(0, 0, 0);
// 当前角色位置
private _curPos: Vec3 = new Vec3();
// 角色目标位置
private _targetPos: Vec3 = new Vec3();
start() {
input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
// // 监听 Body 节点的位置变化
// if (this.BodyAnim && this.BodyAnim.node) {
// this.BodyAnim.node.on(Node.EventType.TRANSFORM_CHANGED, this.onBodyTransformChanged, this);
// }
}
onMouseUp(event: EventMouse) {
if (event.getButton() === 0) {
this.jumpByStep(-2);
} else if (event.getButton() === 2) {
this.jumpByStep(1);
}
}
update(deltaTime: number) {
if (this._startJump) {
this._curJumpTime += deltaTime;
if (this._curJumpTime > this._jumpTime) { // 跳跃结束
// 强制位移到目标位置(水平移动)
this.node.setPosition(this._targetPos);
this._startJump = false; // 标记跳跃结束
} else { // 跳跃中
// 获取 Body 节点的位置
//const bodyPos = this.BodyAnim.node.position;
// this._deltaPos.y = bodyPos.y; // 设置垂直位置为 Body 的垂直位置
// 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); // 设置位移后的位置
}
}
}
jumpByStep(step: number) {
if (this._startJump) {
return;
}
if (this.BodyAnim) {
if (step === 1) {
this.BodyAnim.play('oneStep');
} else if (step === -2) {
this.BodyAnim.play('twoStep');
}
}
this._startJump = true;
this._jumpStep = step;
this._curJumpTime = 0;
this._curJumpSpeed = this._jumpStep / this._jumpTime;
this.node.getPosition(this._curPos);
Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep, 0, 0));
}
// onBodyTransformChanged() {
// if (this.BodyAnim && this.BodyAnim.node) {
// // 获取 Body 节点的位置
// const bodyPos = this.BodyAnim.node.position;
// // 设置 Player 节点的垂直位置与 Body 节点相同
// this.node.setPosition(new Vec3(this.node.position.x, bodyPos.y, this.node.position.z));
// }
// }
// onDestroy() {
// // 移除事件监听
// if (this.BodyAnim && this.BodyAnim.node) {
// this.BodyAnim.node.off(Node.EventType.TRANSFORM_CHANGED, this.onBodyTransformChanged, this);
// }
// }
}
想了很多办法比如上述注释的 this.node.setPosition(new Vec3(this.node.position.x, bodyPos.y, this.node.position.z));,这样确实player跳了,但是body跳动会叠加本来垂直移动1个单位,body会移动2个单位搞了一天了,要裂开了
问题解决了,player应该是空节点,我创建成capsule了

