0

I'm a beginner in both javascript oop and game programming too(!) . Here i've created a game player with a method. but the method is returning undefined. why is that?,

bobsGame = {};

bobsGame.player  = function(which){
  this.which = which;
  this.rollDice = function () {
    diceVal = Math.floor(Math.random() * 6 + 1);
    console.log(diceVal);
    return diceVal;
  }
}

var player1 = new bobsGame.player('player1');

and then in the markup…

$('#roll-dice-btn-1').click(function(){
  bobsGame.player1.rollDice();
});
1
  • 2
    There are too many incoherent things in here. Where is defined bobsgame.player1 for example ? Can you build a working fiddle demonstrating your problem ? Commented Jan 9, 2014 at 16:03

6 Answers 6

3

There is no bobsGame.player1 in your class, you just instanciated a new instance to the variable player1 ?

var player1 = new bobsGame.player('player1');

$('#roll-dice-btn-1').click(function(){
    player1.rollDice();
});

FIDDLE

Sign up to request clarification or add additional context in comments.

3 Comments

This. You aren't currently calling the rollDice() method on the player1 object you created.
@egl - Then what am I calling ?
Your answer is fine, was referring to the OP calling the method incorrectly.
2

Your call should be player1.rollDice() like

  $('#roll-dice-btn-1').click(function(){
    player1.rollDice();
  });

You are confused between player property of bobsGame object and the player1 object you actually created.

Comments

2

this might work a little better

bobsGame.player1 = new bobsGame.player('player1');

2 Comments

And why would you instanciate the class to a property of itself ?
Or just replace bobsGame completely
1

bobsGame.player1 is undefined because player1 was not declared within the object bobsGame.

If you want to create player1 as a key in your bobsGame object, you must use bobsGame.player1 = new bobsGame.player('player1'); instead. So your code would look like: bobsGame = {};

bobsGame.player  = function(which){
  this.which = which;
  this.rollDice = function () {
    diceVal = Math.floor(Math.random() * 6 + 1);
    console.log(diceVal);
    return diceVal;
  }
}

var player1 = new bobsGame.player('player1');

Otherwise, you can use:

$('#roll-dice-btn-1').click(function(){
  player1.rollDice();
});

Comments

1

You can play with a structure like that:

bobsGame = function(opts) {
    this.player = opts.player;
    this.diceVal = 0;
}

bobsGame.prototype.rollDice  = function(){
    this.diceVal = Math.floor(Math.random() * 6 + 1);
}

And in your main file:

var player1 = new bobsGame({player: 'player1'});
$('#roll-dice-btn-1').click(function(){
    player1.rollDice();
    console.log(player1.diceVal);
});

JsFiddle

Comments

1

slight problem with your. It cannot find bobsGame.player1 in your class. So change like this and it would work fine..

bobsGame.player  = function(which){
    this.which = which;

    this.rollDice = function(){
        diceVal = Math.floor(Math.random() * 6 + 1);
        console.log(diceVal);
        return diceVal;
    }
}


     var player1 = new bobsGame.player('player1');
    player1.rollDice();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.