Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

You're developing a game using Enchant.js and you just need an Enemy class which multiple enemy classes derive from and every enemy has its own sprite and stuff.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

This old Enchant.js blog entry talks about class inheritance and offers the following example

enchant();。

A = Class.create({
    initialize:function(){
        this.x=1;
    },
    method:function(){
        document.write(this.x);
    }
});

B = Class.create(A,{   //Inherits A
    initialize:function(){
    A.call(this);  // Calls up A constructor
        this.y=100;
    },
    anotherMethod:function(){
        document.write(this.x*this.y);
    }
});

var b = new B();
b.method();
b.anotherMethod();

So, using a Chess game example, in which you gotta pass parameters to the constructor in order to "build" the piece, what has worked for me is following those guidelines and taking advantage of the Sprite class like this:

var Piece = enchant.Class.create(enchant.Sprite, {
    /**
     * Class constructor
     *
     * @param   object (game object reference)
     * @param   point (JSON object)
     * @param   string
     * @param   string
     * @return  void
     */
    initialize: function(gameObj, position, color, type) {
        enchant.Sprite.call(this,g_SizeCell, g_SizeCell); // grid's cell size (global)
        this.image = gameObj.assets[g_ImgPieces]; // sprite sheet path (global)
        this.x = position.x;
        this.y = positino.y;
        // sprite sheet's frame selection
        switch (this.m_Type) {
            case "pawn":
            default:
                this.frame = 0;
                break;
            case "knight":
                this.frame = 1;
                break;
            case "bishop":
                this.frame = 2;
                break;
            case "rook":
                this.frame = 3;
                break;
            case "queen":
                this.frame = 4;
                break;
            case "king":
                this.frame = 5;
                break;
        }
        if (this.m_Color == "black") {
            this.frame += 6;
        }
    }
    // ... some other member functions
});

var Pawn = enchant.Class.create(Piece, {
    initialize: function(gameObj, position, color) {
        // this is the most important part
        // look that the current object is not in the parent's class signature
        Piece.call(this, gameObj, position, color, "pawn");
    }
    // ... some other member functions
});

// pawn creation for using in chessboard
for (i = 0; i < 8; i++) {
    point = {x:i,y:1};
    newpiece = new Pawn(gameObj, point, "black");
    scene.addChild(newpiece);
    point. y = 6;
    newpiece = new Pawn(gameObj, point, "white");
    scene.addChild(newpiece);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.