没搞懂rotation与angle,rotation既然是个四元素,可面板上的是什么Vec3的方式显示,而且发现旋转节点,哪怕单独的旋转y轴,有时候三个值也会发生变化,猜测可能引擎内部是按照一定的规则,来表示这个旋转的形式,可能按照x,y,z或者x,z,y这样,来改变每个值的不同,实现这个旋转。
console.log(this.node.children[0].position);
console.log(this.node.children[0].worldPosition);
console.log(this.node.children[0].scale);
this.node.children[0].setScale(v3(2, 2, 2));
console.log(this.node.children[0].worldScale);
console.log(this.node.children[0].angle);
console.log(this.node.children[0].rotation);
//rotation转euler
this.node.children[0].setRotationFromEuler(0, 60, 0);
this.node.children[0].setWorldRotationFromEuler(0, 60, 0);
console.log(this.node.children[0].angle);
console.log(this.node.children[0].rotation);
最新的api,好像要提出systemEvent这个全局单例了。
需要一个预制体,预制体[其他也差不多],声明或者脚本中使用的一些小小的写法,一些判断,允许空,但是需要做一些处理。
@property(Prefab)
plane: Prefab = null!;
// plane: Prefab | null = null;
// plane: Prefab = null as unknown as Prefab;
this.node?.addChild(plane);
下面是一些事件的调试,已经加载预制体的两种方式,界面挂载与动态加载
//事件监听
//系统事件 全局单例模式的 systemEvent SystemEvent
//小写的是实例,大写的是这个类 on emit off
// [Scene] 'systemEvent' is deprecated since v3.4.0, please use 'input' instead.
systemEvent.on(SystemEvent.EventType.TOUCH_START, this._onTouchStart, this);
systemEvent.on(SystemEvent.EventType.TOUCH_MOVE, this._onTouchMove, this);
// systemEvent.on(SystemEvent.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
// systemEvent.on(SystemEvent.EventType.TOUCH_END, this._onTouchEnd, this);
input.on(Input.EventType.TOUCH_START, this._onInputTouchStart, this);
input.on(Input.EventType.KEY_UP, this._onInputkeyUp, this);
//重力感应
input.on(Input.EventType.DEVICEMOTION, this._DeviceMotion, this);
const plane: Node = instantiate(this.plane);
plane.parent = this.node;
plane.setPosition(0, 2, 0);
//loadDir<T extends Asset>(dir: string, onComplete?: ((err: Error | null, data: T[]) => void) | null): void;
resources.loadDir("plane01", (err: Error | null, data: Asset[]) => {if (err) {console.log("加载资源错误");}data.forEach(element => {if (element instanceof Prefab) {const plane: Node = instantiate(element);this.node?.addChild(plane);// plane.setParent(this.node);}});
});
API中一些常用事件的罗列
// export interface _cocos_input_input__InputEventMap {
// [Input.EventType.MOUSE_DOWN]: (event: EventMouse) => void;
// [Input.EventType.MOUSE_MOVE]: (event: EventMouse) => void;
// [Input.EventType.MOUSE_UP]: (event: EventMouse) => void;
// [Input.EventType.MOUSE_WHEEL]: (event: EventMouse) => void;
// [Input.EventType.TOUCH_START]: (event: EventTouch) => void;
// [Input.EventType.TOUCH_MOVE]: (event: EventTouch) => void;
// [Input.EventType.TOUCH_END]: (event: EventTouch) => void;
// [Input.EventType.TOUCH_CANCEL]: (event: EventTouch) => void;
// [Input.EventType.KEY_DOWN]: (event: EventKeyboard) => void;
// [Input.EventType.KEY_PRESSING]: (event: EventKeyboard) => void;
// [Input.EventType.KEY_UP]: (event: EventKeyboard) => void;
// [Input.EventType.DEVICEMOTION]: (event: EventAcceleration) => void;
// [Input.EventType.GAMEPAD_CHANGE]: (event: EventGamepad) => void;
// [Input.EventType.GAMEPAD_INPUT]: (event: EventGamepad) => void;
// [Input.EventType.HANDLE_INPUT]: (event: EventHandle) => void;
// [Input.EventType.HANDLE_POSE_INPUT]: (event: EventHandle) => void;
// [Input.EventType.HMD_POSE_INPUT]: (event: EventHMD) => void;
// [Input.EventType.HANDHELD_POSE_INPUT]: (event: EventHandheld) => void;
// }
// export interface _cocos_input_system_event__SystemEventMap {
// [SystemEvent.EventType.MOUSE_DOWN]: (event: EventMouse) => void;
// [SystemEvent.EventType.MOUSE_MOVE]: (event: EventMouse) => void;
// [SystemEvent.EventType.MOUSE_UP]: (event: EventMouse) => void;
// [SystemEvent.EventType.MOUSE_WHEEL]: (event: EventMouse) => void;
// [SystemEvent.EventType.TOUCH_START]: (touch: Touch, event: EventTouch) => void;
// [SystemEvent.EventType.TOUCH_MOVE]: (touch: Touch, event: EventTouch) => void;
// [SystemEvent.EventType.TOUCH_END]: (touch: Touch, event: EventTouch) => void;
// [SystemEvent.EventType.TOUCH_CANCEL]: (touch: Touch, event: EventTouch) => void;
// [SystemEvent.EventType.KEY_DOWN]: (event: EventKeyboard) => void;
// [SystemEvent.EventType.KEY_UP]: (event: EventKeyboard) => void;
// [SystemEvent.EventType.DEVICEMOTION]: (event: EventAcceleration) => void;
// }
/**
* - cc.Node.EventType.TOUCH_START // 按下时事件
* - cc.Node.EventType.TOUCH_Move // 按住移动后事件
* - cc.Node.EventType.TOUCH_END // 按下后松开后事件
* - cc.Node.EventType.TOUCH_CANCEL // 按下取消事件
*
* 以下事件只在 PC 平台上触发:
*
* - cc.Node.EventType.MOUSE_DOWN // 鼠标按下时事件
* - cc.Node.EventType.MOUSE_MOVE // 鼠标按住移动后事件
* - cc.Node.EventType.MOUSE_ENTER // 鼠标进入目标事件
* - cc.Node.EventType.MOUSE_LEAVE // 鼠标离开目标事件
* - cc.Node.EventType.MOUSE_UP // 鼠标松开事件
*/
记住事件监听后也要在必要的时候释放
//释放事件
protected onDestroy(): void {//估计要移除systemEvent这个组件实例了// systemEvent.off(SystemEvent.EventType.TOUCH_START, this._onTouchStart, this);input.off(Input.EventType.TOUCH_START, this._onInputTouchStart, this);// input.off(Input.EventType.TOUCH_START);
}