Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Nico

In main:

populateItems: function() {
	//spawn items if they exist
	var x = 12;
	var y = 29;

	this.spawnEntity(EntityItem1, x, y);
}

Entity code:

    EntityItem1 = ig.Entity.extend({
        animSheet: new ig.AnimationSheet( 'media/inv1.png', 28, 27 ),
        size: {x: 28, y:27},
        maxVel: {x: 0, y: 0},

        checkAgainst: ig.Entity.TYPE.NONE,
        collides: ig.Entity.COLLIDES.PASSIVE,
        stats: {
            str: 2,
            dex: 0,
            inte: 0,
            vit: 5,
            spd: 0,
            luk: 0,
            armor: 4,
            resist: 2            
        },   

        init: function( x, y, settings ) {
            this.parent( x, y, settings );

            console.log(this.pos);
        	
            this.addAnim('walk', .7, [0]);
            
        },

        update: function() {

            //this.pos.x = 16;
            //this.pos.y = 90;

            if (ig.input.pressed('click') && this.inFocus()) {
                if(ig.game.gameName != 'Inventory'){
                    this.kill();
                }else{
                    ig.game.spawnEntity( EntityItem1e );
                    ig.game.allData.playerData.itemList.Item1.count = 0;
                    ig.game.allData.playerData.itemList.Item1.equipped = true;
                    for( stat in ig.game.allData.playerData.stats ){
                        ig.game.allData.playerData.stats[stat] = ig.game.allData.playerData.stats[stat] + this.stats[stat];
                    }
    
                    this.kill();
                }
            }            

            this.parent();
        },

        inFocus: function() {
            return (
               (this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
               ((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
               (this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
               ((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
            );
        }


    });

I know this code is ugly but it should work. When the positions are hard coded into the entity it spawns fine with:

this.spawnEntity(EntityItem1);

But if I try to set variables and spawn it like so:

this.spawnEntity(EntityItem1, x, y);

It will say it is spawning an entity but the entity has no position when I log it to the console and I can't see it on the screen.


Very weird, I'm stumped. Any help is much appreciated.

-Nico



EDIT: WTFFFFF?!?!?!


Moving the call to populateItems() from init to loadLevel in main fixed it. Can anyone shed light on why that would be?

1 decade ago by mLautz

Existing entities are wiped when load level is called. So, if you were calling populateItems() and then loadLevel, your entitiy would not have survivied the load.
Page 1 of 1
« first « previous next › last »