Code not working

no errors just not working

Code:

var BowlingBallVontroller = pc.createScript('bowlingBallController');

// initialize code called once per entity
BowlingBallController.prototype.initialize = function() {
var BallScript = pc.createScript('ballScript');
    BallScript.attributes.add('speed', 
                              {  
        type: 'number',default: -8 });    
    BallScript.prototype.initialize = function()
    {
        this.entity.collision.on('collisionstart', this.onCollisionStart, this);   
        this.app.keyboard.on('keydown', this.onKeyDown, this);   
        var pins = this.app.root.findByTag("Pin");     
        this.startingPositions = [];  
        this.startingRotations = [];  
        for(var i = 0; i < pins.length; i++)
        {
            this.startingPositions.push(pins[i].getLocalPosition().clone());    
            this.startingRotations.push(pins[i].getLocalRotation().clone()); 
        }           
        this.ballPosition = this.app.root.findByName("BowlingBall").getLocalPosition().clone();  
        this.ballMoving = false;  }; 
    BallScript.prototype.onCollisionStart = function (result) 
    {
        if (result.other.rigidbody) 
        {
            if(result.other.name == "BowlingPin")
                this.entity.sound.play("Hit");   
        }
    };
    BallScript.prototype.onKeyDown = function(e) {      if(e.key == pc.KEY_SPACE || e.key == pc.KEY_W)
    {          this.entity.rigidbody.applyImpulse(0,0, this.speed);          this.ballMoving = true;      } 
                                                 };
                                                  if(e.key == pc.KEY_R)
                                                      
                                                  {          var pins = this.app.root.findByTag("pin");    
                                                   for(var i = 0; i < pins.length; i++)
                                                   {       
                                                       pins[i].rigidbody.teleport(this.startingPositions[i],this.startingRotations[i]);    
                                                       pins[i].rigidbody.linearVelocity = pc.Vec3.ZERO;    
                                                       pins[i].rigidbody.angularVelocity = pc.Vec3.ZERO;     
                                                   }          // now the ball          this.entity.rigidbody.teleport(this.ballPosition);          this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;          this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;          this.ballMoving = false;      }      if(e.key == pc.KEY_B){           // Just the ball          this.entity.rigidbody.teleport(this.ballPosition);          this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;          this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;          this.ballMoving = false;      }      if(e.key == pc.KEY_LEFT || e.key == pc.KEY_A){          this.entity.rigidbody.applyImpulse(-0.2,0,0);      }      if(e.key == pc.KEY_RIGHT || e.key == pc.KEY_D){          this.entity.rigidbody.applyImpulse(0.2,0,0);      }      };    // update code called every frame  BallScript.prototype.update = function(dt) {  };  
}
};
// update code called every frame
BowlingBallController.prototype.update = function(dt) {
    
};

// swap method called for script hot-reloading
// inherit your script state here
// BowlingBallVontroller.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

If the whole code is not working then check whether you have attached the script to any entity or not.

I have done that and it is

Hello @Jace_Andruski! To really make sure that the whole script is not working I would like to suggest to add the line console.log('check'); to your initialize function. Then you can see in the console of your browser whether this line is being executed.

1 Like

I see a typo in the very first line of your script. Replace var BowlingBallVontroller to var BowlingBallController. (Possible you have to parse the script in the editor after saving this change).

1 Like