I am trying to call the audiosource.play function, but instead of passing the play function a specific name string, I want to just play whatever audiosource is attached to the entity (you can assume there is just one). I thought maybe the assets array might work so I tried something like this:
pickedEntity.audiosource.play(pickedEntity.audiosource.assets[0].getName());
The code below works fine, because I just specify the name string for the audiosource directly. But I don’t want to do that. Because I want to use the same script for multiple entities. So that when I click on an entity, it plays the associated audiosource. I know you can have multiple audiosources for an entity, so I just want to be able to play the first audiosource in the list. Any help?
pc.script.create('clickme', function (app) {
// Creates a new Clickme instance
var Clickme = function (entity) {
this.entity = entity;
};
Clickme.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
},
onSelect: function (e) {
this.cam1 = app.root.findByName("Camera1");
var from = this.cam1.camera.screenToWorld(e.x, e.y, this.cam1.camera.nearClip);
var to = this.cam1.camera.screenToWorld(e.x, e.y, this.cam1.camera.farClip);
app.systems.rigidbody.raycastFirst(from, to, function (result) {
var pickedEntity = result.entity;
pickedEntity.audiosource.play("theres-somethin-strange-about-this-place");
});
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
}
};
return Clickme;
});