collinhover / impactplusplus

Impact++ is a collection of additions to ImpactJS with full featured physics, dynamic lighting, UI, abilities, and more.
http://collinhover.github.com/impactplusplus
MIT License
276 stars 58 forks source link

Usage of the spawnEntity method and proper file include? #103

Closed Pattentrick closed 11 years ago

Pattentrick commented 11 years ago

Hi there,

i have some trouble with the spawnEntity method, i think i misunderstand the usage of it. There is an animated „cursor entity“ in my game that replaces the default browser cursor.

ig.module(
    'game.entities.cursor'
)
.requires(
    'plusplus.core.config',
    'plusplus.core.entity'
)
.defines(function () {

    var _c  = ig.CONFIG;

    ig.EntityCursor = ig.global.EntityCursor = ig.EntityExtended.extend({

        size: {
            x: 16,
            y: 16
        },

        name: "cursor",

        animSheet: new ig.AnimationSheet( _c.PATH_TO_MEDIA + 'cursor.png', 16, 16 ),

        animInit: "blink",

        animSettings: {

            blink: {
                frameTime: 0.30,
                sequence: [0,1,2]
            }

        },

        /**
         * reposition the cursor entity to the position
         * of the invisble default browser mouse cursor
         */
        repositionCursor: function(){

            this.pos.x = ig.input.mouse.x - 24;
            this.pos.y = ig.input.mouse.y - 8;

        },

        update: function(){

            this.parent();

            this.repositionCursor();

        }

    });

});

I have tried to spawn it after the level loads like this:

ig.module(
    'game.main'
)
.requires(
    // include impact++
    'plusplus.core.plusplus',
    // levels
    'game.levels.test',
    // enable debug
    'plusplus.debug.debug'
)
// define the main module
.defines(function () {

    "use strict";

    // declare config variable
    var _c = ig.CONFIG;

    var pac = ig.GameExtended.extend({

        // background color of canvas
        clearColor: "#000000",

        // override the game init function
        init: function () {

            this.parent();

            // load level
            this.loadLevel(ig.global.LevelTest);

            // init entity based mouse cursor
            this.spawnMouseCursor();

        },

        inputStart: function () {

            ig.input.bind(ig.KEY.MOUSE1, 'click');

        },

        /**
         * spawns an entity based mouse cursor
         */
        spawnMouseCursor: function(){

            this.spawnEntity(ig.EntityCursor, 0, 0);

        }

    });

    ig.main(
        '#canvas',
        pac,
        60,
        _c.GAME_WIDTH,
        _c.GAME_HEIGHT,
        _c.SCALE,
        ig.LoaderExtended
    );

});

However i just get the following error: Uncaught Error: Can't spawn entity of type: undefined. It does not exist in namespace 'ig' or 'ig.global'/'window'.

And the error message is right, there is no „EntityCursor“ in ig or ig.global untill i include the module manually like this.

.requires(
    // include impact++
    'plusplus.core.plusplus',
    // levels
    'game.levels.test',
    // enable debug
    'plusplus.debug.debug',
    // cursor entity
    'game.entities.cursor'
)

But until now i thought impact will „scan“ all the files located under game → entities and include them for me. So apparently  i am doing something wrong :-(

I am stuck on this, as allways your help is highly appreciated!

collinhover commented 11 years ago

You do need to require the cursor module, i.e. your final code example is correct. The only time Impact scans the files in game/entities is when you are using the level editor, Weltmeister, and it only does that because we tell it to in the Weltmeister config. This is strictly ImpactJS behavior and is unchanged by Impact++.

One thing I'd suggest is to override buildLevel instead of loadLevel and call your spawnMouseCursor method after calling parent. Impact++ makes level loading deferred, so loadLevel only tells the game that it should switch levels, while buildLevel actually creates the level and spawns all the entities. This is important later, because Impact++ has built in smooth fade transitions between levels (which you can disable).

Pattentrick commented 11 years ago

Many thanks for your fast anwser and your explanation about how this whole thing works. I accept your suggestion and will use buildLevel in the future :-)