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 gort

I am wondering, how would you save a player's place, like, which level he/she was on? I already treid. hers my code:

//main.js
var GameInfo = function () {
    this.points = 0;
    this.level = "Level1"
}
MyGame = ig.Box2DGame.extend({
	
	gravity: 100, // All entities are affected by this
	GameInfo: new GameInfo(),
	// Load a font
	font: new ig.Font( 'media/04b03.font.png' ),
	
	init: function() {
		// Bind keys
		ig.input.bind( ig.KEY.LEFT_ARROW, 'left' );
		ig.input.bind( ig.KEY.RIGHT_ARROW, 'right' );
		ig.input.bind( ig.KEY.UP_ARROW, 'jump' );
        ig.input.bind( ig.KEY.DOWN_ARROW, 'down' );
		ig.input.bind( ig.KEY.SPACE, 'shoot' );
		this.loadLevel( 'Level' + this.GameInfo.level );
	},
	
	loadLevel: function( data ) {
		this.parent( data );
		for( var i = 0; i < this.backgroundMaps.length; i++ ) {
			this.backgroundMaps[i].preRender = true;
		}
	},

//levelexit.js
ig.module(
    'game.entities.levelexit'
)
.requires(
    'impact.entity'
)
.defines(function () {
    EntityLevelexit = ig.Entity.extend({
        _wmScalable : true,
        gameOverTest: false,
        _wmDrawBox: true,
        _wmBoxColor: 'rgba( 0, 0, 255, 0.7 )',
        size: { x: 8, y: 8 },
        level: null,
        checkAgainst: ig.Entity.TYPE.A,
        update: function () {},
        
        check: function ( other ) {
            if ( other instanceof EntityPlayer && ig.game.getEntitiesByType(EntityCrate).length <= 0 && ig.game.getEntitiesByType(EntityMonster).length <= 0) {
                    this.nextLevel()
            }
        },
        nextLevel: function () {
            if ( this.level ){
                var levelName = this.level.replace(/^(Level)?(\w)(\w*)/, function (m, l, a, b) {
                    return a.toUpperCase() + b;
                });
                ig.game.GameInfo.level = levelName
                ig.game.loadLevelDeferred(ig.global['Level' + levelName]);
                console.log('Level' +levelName);
            }
        },
        
    });
});

Some one! PLEASE! awnser soon!!
By the way, all my requires have been given a once-over.

1 decade ago by lTyl

Do you mean like a Waypoint system? (Player dies and respawns at specific coordinates) or a save system? (IE: Game is closed, player wants to resume progress where he last saved)

For the first, you'd have to store the x and y coordiantes of where to spawn the player when they reach a waypoint. For instance, if the player collides with a Waypoint entity, then you set the new spawn location to the location of the last waypoint and store the value globally. When you respawn the player, respawn him at the last waypoint coordinates.

If you mean a save system, then the easiest way would be to use the LocalStorage API (There are some Impact plugins for this on Point of Impact and in the Code section on the forums). You'd save the appropriate data, such as level name and coordinates, using LocalStorage and then retrieve the data when the player loads their saved game.
Page 1 of 1
« first « previous next › last »