import { _decorator, Component, instantiate, Node, Prefab, resources, RigidBody2D } from ‘cc’;
const { ccclass, property } = _decorator;
//生成
@ccclass(‘shuiqqsc’)
export class shuiqqsc extends Component {
@property(Node)
spawnPoint: Node | null = null;
@property(Node)
fruitContainer: Node | null = null;
_currentFruit: Node | null = null;
@property(Node)
cmq:Node=null;
start() {
this.generateNewFruit();
}
generateNewFruit() {
const randomType = Math.floor(Math.random() * 3) + 1;
resources.load(`prefabs/fruit${randomType}`, (err, prefab: Prefab) => {
if (err) {
console.error("失败", err); return;
}
const newFruit = instantiate(prefab);
newFruit.setPosition(this.spawnPoint!.position);
this.fruitContainer.addChild(newFruit);
const rigidBody = newFruit.getComponent(RigidBody2D);
if (rigidBody) rigidBody.enabled = false;
this._currentFruit = newFruit;
this.setupTouchControl(newFruit,this.cmq);
});
}
setupTouchControl(fruitNode: Node,cmq:Node) {
//fruitNode
fruitNode.on(Node.EventType.TOUCH_START, () => {
console.log("开始拖动");
});
fruitNode.on(Node.EventType.TOUCH_END, () => {
const rigidBody =fruitNode.getComponent(RigidBody2D);
if (rigidBody) rigidBody.enabled = true;
this._currentFruit = null;
this.scheduleOnce(() => this.generateNewFruit(), 1.0);
})
}
}就是 setupTouchControl这里如果是fruitNode.on(Node.EventType.TOUCH_END, () => 也就是fruitNode在前面那么代码可以运行,但是如果是我创建的cmq,cmq.on(Node.EventType.TOUCH_END, () => 就不行,我只改了这里,cmq是空节点,添加了boxcollider2D组件,问了ai也找不到原因,为什么不行?版本3.8.7
