[LayaAirIDE 2.0]timer无法执行
初学菜鸟,自己写了一个刚体碰撞的demo,但是timer下的方法无法执行,求大佬解答,有关代码如下(我是直接在自动生成的GameUI类里头写的):
shootBall方法用来生成球体,直接调用的没有问题的,可以正常执行,但是
constructor() {
super();
//添加3D场景
this.onAwake();
//加载camera和light
this.onStart();
//加载地面
this.loadGround();
//加载墙面
this.loadwall();
this.loopShoot();
}
public loopShoot(): void{
Laya.timer.loop(1000, this, this.shootBall);
}
public shootBall(): void{
//生成空中降落的球
//var random: number = Math.random()*24-7;
let ball: Laya.MeshSprite3D = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createSphere(1));
this.scene1.addChild(ball);
ball.transform.position = new Laya.Vector3((Math.random() - 0.5) * 2, 10, 17);
//添加刚体
ball.addComponent(Laya.PhysicsCollider);
let ballrigid: Laya.Rigidbody3D = ball.addComponent(Laya.Rigidbody3D);
ballrigid.colliderShape = new Laya.SphereColliderShape(1);
ballrigid.friction = 1;
ballrigid.mass = Math.random() * 200 + 100;//质量随机[100,300]
//生成地面上的球
let ball1: Laya.MeshSprite3D = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createSphere(1));
this.scene1.addChild(ball1);
ball1.transform.position = new Laya.Vector3(0, 1, 16);
//加载材质
let ball1mat: Laya.BlinnPhongMaterial = new Laya.BlinnPhongMaterial();
Laya.Texture2D.load("res/layabox.png", Laya.Handler.create(null, function(layabox: Laya.Texture2D): void{
ball1mat.albedoTexture = layabox;
}));
//ball1mat.tilingOffset = new Laya.Vector4(10, 10, 0, 0);
ball1.meshRenderer.material = ball1mat;
//添加刚体
ball1.addComponent(Laya.PhysicsCollider);
let ballrigid1: Laya.Rigidbody3D = ball1.addComponent(Laya.Rigidbody3D);
ballrigid1.colliderShape = new Laya.SphereColliderShape(1);
ballrigid1.friction = 0.2;
ballrigid1.mass = 2;
}
shootBall方法用来生成球体,直接调用的没有问题的,可以正常执行,但是
constructor() {
super();
//添加3D场景
this.onAwake();
//加载camera和light
this.onStart();
//加载地面
this.loadGround();
//加载墙面
this.loadwall();
this.loopShoot();
}
public loopShoot(): void{
Laya.timer.loop(1000, this, this.shootBall);
}
public shootBall(): void{
//生成空中降落的球
//var random: number = Math.random()*24-7;
let ball: Laya.MeshSprite3D = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createSphere(1));
this.scene1.addChild(ball);
ball.transform.position = new Laya.Vector3((Math.random() - 0.5) * 2, 10, 17);
//添加刚体
ball.addComponent(Laya.PhysicsCollider);
let ballrigid: Laya.Rigidbody3D = ball.addComponent(Laya.Rigidbody3D);
ballrigid.colliderShape = new Laya.SphereColliderShape(1);
ballrigid.friction = 1;
ballrigid.mass = Math.random() * 200 + 100;//质量随机[100,300]
//生成地面上的球
let ball1: Laya.MeshSprite3D = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createSphere(1));
this.scene1.addChild(ball1);
ball1.transform.position = new Laya.Vector3(0, 1, 16);
//加载材质
let ball1mat: Laya.BlinnPhongMaterial = new Laya.BlinnPhongMaterial();
Laya.Texture2D.load("res/layabox.png", Laya.Handler.create(null, function(layabox: Laya.Texture2D): void{
ball1mat.albedoTexture = layabox;
}));
//ball1mat.tilingOffset = new Laya.Vector4(10, 10, 0, 0);
ball1.meshRenderer.material = ball1mat;
//添加刚体
ball1.addComponent(Laya.PhysicsCollider);
let ballrigid1: Laya.Rigidbody3D = ball1.addComponent(Laya.Rigidbody3D);
ballrigid1.colliderShape = new Laya.SphereColliderShape(1);
ballrigid1.friction = 0.2;
ballrigid1.mass = 2;
}
没有找到相关结果
已邀请:
要回复问题请先登录
1 个回复
177*****542
赞同来自: