Getters and Setters?

Recommended Posts

Hero.prototype.move = function (direction) { 
  const SPEED = 200;
    this.body.velocity.x = direction * SPEED;

So I need to change SPEED to another value during gameplay - I thought I  needed to use a setter, but I'm unsure how to implement it? (i realise i need to change it to var)

Share this post

Link to post
Share on other sites

You can pass the variable speed as a parameter.

Hero.prototype.move = function (direction, SPEED) {
    this.body.velocity.x = direction * SPEED;

The setters are useful when you want to monitor the change of a variable. But depending on the case, it is not necessary.

var _speed=200;

// Setter

set SPEED(value){
   _speed = value;
   // your custom code here


get SPEED(){
   //your custom code here
   return _speed;   

Now you can use SPEED like any other variable.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.