Can't Call Function

Hello , I am new to programming. When I try to call the Firegun function, I get an error saying
that this.app.Firegun is not a Function. I think this is probably a bad mistake but I don’t know how to solve it , Here’s the Code:

Bullet.prototype.initialize = function() {
    this.force = new pc.Vec3();
   var self = this.app; 
    
    var camera = this.app.root.findByName("Camera");
     Bullet.prototype.Firegun = function  (entity){
         entity = new pc.Entity();
    this.app.root.addChild(entity);
    this.app.camera.addChild(entity);
    entity.addComponent("model", {
    type: 'box',
});
    entity.setScale(0.1,0.1,0.2);
    entity.addComponent("rigidbody");
   entity.rigidbody.type = pc.BODYTYPE_DYNAMIC;
    entity.addComponent("collision");
    entity.collision.halfExtents = (0.05,0.05,0.1);
     this.force.copy(entity.forward).scale(500);
entity.rigidbody.applyForce(this.force);
        if (entity.rigidbody.linearVelocity === pc.Vec3.ZERO) {
    entity.destroy();
            console.log("Bullet has been fired");
        }
};


};

// update code called every frame
Bullet.prototype.update = function(dt) {
    if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
        
        self.Firegun();}
    
    }; 

Hi @agent178 and welcome,

So there are some issues on how you are structuring your code, here is an updated version fixing those:

Bullet.prototype.initialize = function () {
  this.force = new pc.Vec3();
};

Bullet.prototype.Firegun = function (entity) {
  entity = new pc.Entity();
  this.app.root.addChild(entity);
  this.app.camera.addChild(entity);
  entity.addComponent("model", {
    type: "box",
  });
  entity.setScale(0.1, 0.1, 0.2);
  entity.addComponent("rigidbody");
  entity.rigidbody.type = pc.BODYTYPE_DYNAMIC;
  entity.addComponent("collision");
  entity.collision.halfExtents = (0.05, 0.05, 0.1);
  this.force.copy(entity.forward).scale(500);
  entity.rigidbody.applyForce(this.force);
  if (entity.rigidbody.linearVelocity === pc.Vec3.ZERO) {
    entity.destroy();
    console.log("Bullet has been fired");
  }
};

// update code called every frame
Bullet.prototype.update = function (dt) {
  if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
    this.Firegun();
  }
};

Thank You , It works Perfectly

1 Like