Hi, I have this script:
var Person = pc.createScript('person');
Person.attributes.add('material_happy_0', {type: "asset", assetType: "material"});
Person.attributes.add('material_sad_0', {type: "asset", assetType: "material"});
Person.attributes.add('material_happy_1', {type: "asset", assetType: "material"});
Person.attributes.add('material_sad_1', {type: "asset", assetType: "material"});
Person.attributes.add('material_happy_2', {type: "asset", assetType: "material"});
Person.attributes.add('material_sad_2', {type: "asset", assetType: "material"});
Person.attributes.add('material_happy_3', {type: "asset", assetType: "material"});
Person.attributes.add('material_sad_3', {type: "asset", assetType: "material"});
Person.prototype.onCollision = function(result) {
var entity = result.other;
if (entity.script && entity.script.box) {
// set happy version
switch (this.mTypeSel) {
case 0:
this.entity.render.material = this.material_happy_0.resource;
break;
case 1:
this.entity.render.material = this.material_happy_1.resource;
break;
case 2:
this.entity.render.material = this.material_happy_2.resource;
break;
case 3:
this.entity.render.material = this.material_happy_3.resource;
break;
}
}
};
Person.prototype.setType = function(select) {
this.mTypeSel = select;
switch (this.mTypeSel) {
case 0:
this.entity.render.material = this.material_sad_0.resource;
break;
case 1:
this.entity.render.material = this.material_sad_1.resource;
break;
case 2:
this.entity.render.material = this.material_sad_2.resource;
break;
case 3:
this.entity.render.material = this.material_sad_3.resource;
break;
}
};
// initialize code called once per entity
Person.prototype.initialize = function() {
this.mTypeSel = 0;
this.entity.collision.on('collisionstart', this.onCollision, this);
};
After creating each entity of this type I call the setType() function to set this.mTypeSel to one of 4 values and set the material of the entity accordingly.
This all works fine.
But when the entity is hit by a box I want the material to change to a new material depending on this.mTypeSel. The collision is detected and onCollision() is called. But inside this function the value of this.mTypeSel is always 0! So the wrong material is selected.
How is that possible?
I Know setType() is called correctly, because I see the material change.