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 paulh

Hi All

Im a bit confused (and a noob at coding) but ive tried to make a main menu page using entities as the menu items ... then spawn particles on a ,mouse press, the particles will then collide with the menu item to select it.

I knwo this works cos its in the core gameply but when i tried to make a menu not only does the frame rate drop massivly but i also dont see the particles. Im guessing it has something to do with the way i structured my code but i cant seem to work it out :-(

Menu = ig.Game.extend({
	
	
	font: new ig.Font( 'media/blue.font.png' ),//font
	MenusImage:new ig.Image('media/been.png' ),//define intro image


update: function() {
	ig.input.bind( ig.KEY.MOUSE1, 'mouseLeft' );
	ig.game.spawnEntity(EntityStats,0,0);//spawn the game stats
	var mx = ig.input.mouse.x ; //Figures out the x coord of the mouse in the entire world
	var my = ig.input.mouse.y; //Figures out the y coord of the mouse in the entire world
	
	if (ig.game.menu == 0){
		//console.log(ig.game.menu);
		this.loadLevel( LevelTest );// LOAD LEVEL DATA
		ig.game.spawnEntity(EntityMenu,22 ,46, { blockType:"Match" });//spawn a menu item
		ig.game.spawnEntity(EntityMenu,180 ,16, { blockType:"Free" });//spawn a menu item
		ig.game.spawnEntity(EntityMenu,342 ,46, { blockType:"Story" });//spawn a menu item
		
		ig.game.spawnEntity(EntityMenu,12 ,200, { blockType:"Options" });//spawn a menu item
		ig.game.spawnEntity(EntityMenu,180 ,192, { blockType:"Time" });//spawn a menu item
		ig.game.spawnEntity(EntityMenu,340 ,200, { blockType:"Level" });//spawn a menu item
	}
	ig.game.menu ==1;
	this.parent();
	

	myGameMode = 5;
    },
    
run: function(){
	if (ig.input.state('mouseLeft')) {
	ig.game.spawnEntity( FireGib, mx,my );
    }
    this.parent();
}
});

1 decade ago by quidmonkey

.init() is where your game&039;s initialization takes place. You have your initialization logic in your #.update() loop. Reorganize your code so that .spawnEntity() occurs within the .init().

Your .run() logic should also be merged into .update().

Menu = ig.Game.extend({

	font: new ig.Font( 'media/blue.font.png' ),	//font
	MenusImage:new ig.Image('media/been.png' ),	//define intro image
	
	init : function() {
		//keys
		ig.input.bind( ig.KEY.MOUSE1, 'mouseLeft' );
		
		//level
		this.loadLevel( LevelTest );
		
		//game stats
		ig.game.spawnEntity(EntityStats,0,0);
		
		//menu
		ig.game.spawnEntity(EntityMenu,22 ,46, { blockType:"Match" });
		ig.game.spawnEntity(EntityMenu,180 ,16, { blockType:"Free" });
		ig.game.spawnEntity(EntityMenu,342 ,46, { blockType:"Story" });
		
		ig.game.spawnEntity(EntityMenu,12 ,200, { blockType:"Options" });
		ig.game.spawnEntity(EntityMenu,180 ,192, { blockType:"Time" });
		ig.game.spawnEntity(EntityMenu,340 ,200, { blockType:"Level" });
		
		//set default mode
		myGameMode = 5;
	},

	update: function(){
		if (ig.input.state('mouseLeft')) {
			var m = ig.game.mouse;
			ig.game.spawnEntity( FireGib, m.x,m.y );
		}
		this.parent();
	}
	
});

Best.

1 decade ago by paulh

Quidmonkey, im not sure what the forums would be like without your continued help, thank you so much. Your an awesome part of this community!

Is there a breakdown/overview/documentation of how code should be structured with regards to the different functions?

thx again

p

1 decade ago by quidmonkey

Thank you for your kind words.

The main.js template provided with Impact is a good guideline for how you should setup your code.

Here's a typical breakdown:

init: function(){
	//bind keys
	//load level
	//spawn entities
	//set config varaibles
	//anything else that needs to be done prior to game starting
},

draw: function(){
	this.parent();		//draw all entities and maps
	//whatever else needs to be drawn i.e. hud, menu, etc.
},

update: function(){
	this.parent();		//updates all entities and maps
	//whatever else needs to be updated by the game each frame
}

You'll notice that most objects (Entities. Fonts, Game) have this init/draw/update structure, and most game engines in general copy it or use a variation. This is because it's easy to see what each step needs to do: init - setup shit; draw - draw shit; update - do shit.

Best.
Page 1 of 1
« first « previous next › last »