Jump to content

override


valentinaCom
 Share

Recommended Posts

hi :)

 

I have a problem with adding buttons to game. 

There is a class "button" : 

 

define([], function() {
 
myButton = function () {
 
    };
 
myButton.prototype = Object.create(Phaser.Button.prototype);
myButton.prototype.constructor = myButton;
 
    /** ----- initialize ------
     *  Function: create the button item according to parameters in config.ls
     *  Parameters: params - object with all item data
     -------------------------*/
myButton.prototype.initialize = function(params) {
if(!params.x) params.x = 0;
if(!params.y) params.y = 0;
   //  We call the Phaser.Button passing in the game reference. We're giving it params
   Phaser.Button.call(this, params.game, params.x, params.y,  params.key , params.press ,params.self);
if(params.anchor) {
  this.anchor.setTo(params.anchor.x, params.anchor.y);
}
  //TODO: ADD scale, rotation, width, height ...
 
     params.game.add.existing(this);
};
 
    /** ------- update -------
     *  Function:
     *  Parameters: -
     -------------------------*/
myButton.prototype.update = function() {
 
};
return myButton;
});
 
Every new btn that I create, I'm creating a new class that inherit from the button class. 
 
example:
define(["game/objects/button"], function(mybutton) {
 
    help_btn = function () {
        mybutton.call();
    };
 
    help_btn.prototype = Object.create(mybutton.prototype);
    help_btn.prototype.constructor = help_btn;
 
    // Inheritence Sprite
    for (var i in mybutton.prototype) {
        help_btn.prototype = mybutton.prototype;
    }
 
    /** ----- initialize ------
     *  Function: send to initialize function in sprite.js
     *  Parameters: params - object with all item data
     -------------------------*/
    help_btn.prototype.initialize = function(params) {
        mybutton.prototype.initialize(params);
    };
 
    /** ------- update -------
     *  Function:
     *  Parameters: -
     -------------------------*/
    help_btn.prototype.update = function() {
 
    };
    return help_btn;
});
 
The problem is that when I create several btns, it adds to the screen only the last one that I added. 
maybe there is a problem with the add.exisiting function that override each time the previous added btn? 
 
thnx
 

 

Link to comment
Share on other sites

Urg, that's quite a mess there.

First off: You are calling "mybutton.prototype.initialize(params);" which is the function of the prototype, not of a button instance.
Then you are doing something pretty unnecessary. You are copying the prototype " help_btn.prototype = Object.create(mybutton.prototype);" and after that you loop through the prototype again. That's redundant.

I'm not sure what the purpose of your help button is.

Also, instead of the initialize method, you could use the constructor instead. The constructor is meant for that.
 
 

define([], function() {  var myButton = function ( params ) {    if(!params.x) params.x = 0;    if(!params.y) params.y = 0;    // We call the Phaser.Button passing in the game reference. We're giving it params    Phaser.Button.call(this, params.game, params.x, params.y, params.key , params.press ,params.self);    if(params.anchor) {      this.anchor.setTo(params.anchor.x, params.anchor.y)    }    //TODO: ADD scale, rotation, width, height ...     params.game.add.existing(this);  };  myButton.prototype = Object.create(Phaser.Button.prototype);  myButton.prototype.constructor = myButton;  /** ------- update -------  * Function:  * Parameters: -  -------------------------*/  myButton.prototype.update = function() {  };  return myButton;});

and
 

define(["game/objects/button"], function(mybutton) {  var help_btn = function ( params ) {    mybutton.call( this, params );  };  help_btn.prototype = Object.create(mybutton.prototype);  help_btn.prototype.constructor = help_btn;  /** ------- update -------  * Function:  * Parameters: -  -------------------------*/  help_btn.prototype.update = function() {  };  return help_btn;});

I honestly have no idea what that help btn is for, however to create an actual sprite using mybutton or help_btn you would need to do this:

 

var button = new mybutton( params );

var button2 = new help_btn( params );

 

The prototype is pretty much just the skeleton of your object. Only the keyword "new" will give life to it.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...