Hello, all.
I have two objects in my game that have virtually the same issue.
The lighter in my game, when selected for the first time, has an opening animation, but when selected a second time, doesn’t play the animation again.
Simmilarly, the gun in my game has a reloading over animation that plays if R is pressed and you are out of loaded ammo. It only works once, and I cannot reload the gun after the first time.
Here is the code I use for the lighter:
var Lighter = pc.createScript('lighter');
Lighter.attributes.add('flame',{type:'entity'})
// initialize code called once per entity
Lighter.prototype.initialize = function() {
};
// update code called every frame
Lighter.prototype.update = function(dt) {
if(this.entity.enabled === true){
this.entity.anim.setBoolean('open', true);
this.flame.enabled = true;
}else{
this.entity.anim.setBoolean('open', false);
this.flame.enabled = false;
};
Here are some pictures of the anime state graph for the lighter:
Here is the code for the gun:
(Reload is handled on lines 20-23 and 66-71)
var Gun = pc.createScript('gun');
Gun.attributes.add('bullet', { type: "entity" });
Gun.attributes.add('mag', { type: "number" });
Gun.attributes.add('cooldown', { type: "number" });
Gun.attributes.add('bulletTxt', { type: "entity" });
Gun.attributes.add('reloadAnimTime', { type: "number" });
// initialize code called once per entity
Gun.prototype.initialize = function () {
this.rounds = this.mag;
this.isShooting = false;
this.timer = 0;
this.isTimerActive = true;
this.entity.anim.on('reloadAnimDone', function () {
this.rounds = this.mag;
// alert('reload Sucessful');
}, this);
};
// update code called every frame
Gun.prototype.update = function (dt) {
this.bulletTxt.element.text = this.rounds;
// update function
if (this.bulletTxt.element.opacity > 0) {
this.bulletTxt.element.opacity -= 0.1;
this.bulletTxt.element.outlineThickness -= 0.1;
};
if (!this.isTimerActive)
return;
this.timer += dt;
if (this.app.keyboard.isPressed(pc.KEY_Q) && this.rounds != 0 && this.timer > this.cooldown) {
this.playsound();
this.isShooting = true
this.timer = 0;
this.rounds = this.rounds - 1;
this.bulletTxt.element.opacity = 1;
this.bulletTxt.element.outlineThickness = 1;
var newBullet = this.bullet.clone();
this.app.root.addChild(newBullet);
newBullet.setPosition(this.bullet.getPosition());
newBullet.enabled = true;
//alert(this.rounds);
this.timer = 0;
} else {
this.isShooting = false;
};
//if (this.isShooting === true) {
// if (!this.entity.sound.isPlaying('gunShot')) {
// this.entity.sound.play('gunShot');
// };
// }
// else {
// this.entity.sound.stop('gunShot');
//};
if (this.app.keyboard.isPressed(pc.KEY_R) && this.rounds === 0) {
this.entity.anim.setBoolean('reload', true);
} else {
this.entity.anim.setBoolean('reload', false);
};
};
Gun.prototype.playsound = function () {
if (!this.entity.sound.isPlaying('gunShot')) {
this.entity.sound.play('gunShot');
};
};
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// Gun.prototype.swap = function(old) { };
// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/
Here are some pictures of the gun’s anim state graph:
Here is the editor link:PlayCanvas | HTML5 Game Engine
To find the two entities I am referring to:
Dev Test Controller
-View
-Hand models
Under hand models there is the lighter and R1 (the gun)
I am grateful for any help. If there is anything else I can provide please let me know!