3.7.2
我创建了27个prefeb实例,销毁时发生了奇怪的情况,我在预制体脚本里写了碰撞时,将预制体放入节点池,照理是哪个实例被碰撞就销毁哪个,但是奇怪的是碰撞一发生,所有27个实例都没了。
gamemanager代码:
@ccclass(‘GameManger’)
export class GameManger extends Component {
@property(Node)
public GemRoot: Node = null;
@property(Node)
public Bar: Node = null;
@property(Prefab)
public Ball: Prefab = null;
@property(Prefab)
public Gem1: Prefab = null;
@property(Prefab)
public Gem2: Prefab = null;
@property(Prefab)
public Gem3: Prefab = null;
@property(Prefab)
public Gem4: Prefab = null;
@property(Prefab)
public Gem5: Prefab = null;
@property(Prefab)
public Gem6: Prefab = null;
start() {
this._init();
}
update(deltaTime: number) {
}
CreateBall() {
const ball = PoolManager.instance().getNode(this.Ball, this.GemRoot);
//ChangeAni.setPosition(pos.x, pos.y, pos.z );
}
CreateGem(x: number, y: number) {
var gem: Prefab[] = [this.Gem1, this.Gem2, this.Gem3, this.Gem4, this.Gem5, this.Gem6];
const i = math.randomRangeInt(0, 6);
//const x=math.randomRangeInt(-300,300);
const Ball = PoolManager.instance().getNode(gem[i], this.GemRoot);
log(this.GemRoot.parent.name);
Ball.setPosition(x, y, 0);
}
_init() {
const balla = PoolManager.instance().getNode(this.Ball, this.GemRoot);
balla.setPosition(0, -500, 0);
const compo = balla.getComponent(ball);
compo.SetSpeed();
let i;
for (i = 0; i < 9; i++) {
this.CreateGem(i * 80 - 320, 520);
this.CreateGem(i * 80 - 320, 680);
this.CreateGem(i * 80 - 320, 360);
}
}
}
预制体代码:
@ccclass(‘Gem’)
export class Gem extends Component {
private rgb: RigidBody2D = null;
private speed = 1;
start() {
this.speed = 1;
this.rgb = this.node.getComponent(RigidBody2D);
let collider = this.getComponent(Collider2D);
// if (collider) {
// collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
// // collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
// // collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
// // collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
// }
if (PhysicsSystem2D.instance) {
PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
// PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
// PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
// PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
}
}
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
//log('peng!')
PoolManager.instance().putNode(this.node);
}
update(deltaTime: number) {
const pos = this.node.position;
if (pos.y < -500) {
this.node.setPosition(pos.x, 700, 0);
}
}