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

9 years ago by fjorko

Im completely new to this been trying to figure out how to remove an Entity with a key input then respawn it in a few seconds. Anyone got a clue?

Currently tinkering with the coin from the jumpNrun demo with collision set to FIXED. Only managed to kill it so far :)


ig.module(
	'game.entities.coin'
)
.requires(
	'impact.entity'
)
.defines(function(){
	
EntityCoin = ig.Entity.extend({
	size: {x: 36, y: 36},
	
	type: ig.Entity.TYPE.NONE,
	checkAgainst: ig.Entity.TYPE.A, // Check against friendly
	collides: ig.Entity.COLLIDES.FIXED,
	
	animSheet: new ig.AnimationSheet( 'media/coin.png', 36, 36 ),
	sfxCollect: new ig.Sound( 'media/sounds/coin.*' ),
	
	
	init: function( x, y, settings ) {
		this.parent( x, y, settings );
		
		this.addAnim( 'idle', 0.1, [0,0,0,0,0,0,0,0,0,1,2] );
	},
	
	
	update: function() {
		if( ig.input.state('a') ) {
			this.kill();
		}
		// Do nothing in this update function; don't even call this.parent().
		// The coin just sits there, isn't affected by gravity and doesn't move.

		// We still have to update the animation, though. This is normally done
		// in the .parent() update:
		this.currentAnim.update();
	},
	
	
	check: function( other ) {
		// The instanceof should always be true, since the player is
		// the only entity with TYPE.A - and we only check against A.
		if( other instanceof EntityPlayer ) {
			other.giveCoins(1);
			this.sfxCollect.play();
		}
	}
});

});

9 years ago by stahlmanDesign

You need to keep track of it in your main.js so you can spawn it later.

In main.js add some variables to keep track of coin properties:
coinSpawnPos: {	// to save position of coin before kill 
	x: 0,
	y: 0
},
init: function(x, y, settings){
...
ig.game.coinSpawnTimer = new ig.Timer(2),	// two seconds
...
},
update:(x,y,settings){
	...
	if (ig.game.coinSpawnTimer.delta() > 0) {
		ig.game.spawnEnity(EntityCoin, ig.game.coinSpawnPos.x, ig.game.coinSpawnPos.y);
	}
}
...

In your coin extend the kill function
kill: function() {
	ig.game.coinSpawnTimer.reset(); // will set timer to 2 seconds
	ig.game.coinSpawnPos = this.pos; // record its position
	this.parent(); // kill coin
}

*All this is untested

9 years ago by fjorko

Will try this later on tonight, thanks stahlman :)

9 years ago by fjorko

Was not even close gettin that going... :P

Understand what you want the code to do, but cant seem to grasp where the code should be placed and why. Anyone mind clearing that up?

But lets say I got this working and used 3 coins would the "coinSpawnPos" save the unique x,y for each coin?

BRB just gotta sleep :) Posting the current code of main.js:

ig.module( 
	'game.main' 
)
.requires(
	'impact.game',
	'impact.font',

	'plugins.camera',
	'plugins.touch-button',
	'plugins.impact-splash-loader',
	'plugins.gamepad',
	
	'game.entities.player',
	'game.entities.blob',

	'game.levels.title',
	'game.levels.grasslands',
	'game.levels.snowhills'
)
.defines(function(){
	

// Our Main Game class. This will load levels, host all entities and
// run the game.

MyGame = ig.Game.extend({
	
	clearColor: "#d0f4f7",
	gravity: 600, // All entities are affected by this
	
	// Load a font
	font: new ig.Font( 'media/fredoka-one.font.png' ),

	// HUD icons
	heartFull: new ig.Image( 'media/heart-full.png' ),
	heartEmpty: new ig.Image( 'media/heart-empty.png' ),
	coinIcon: new ig.Image( 'media/coin.png' ),

	
	init: function() {
		// We want the font's chars to slightly touch each other,
		// so set the letter spacing to -2px.

		this.font.letterSpacing = -2;		
		
		// Load the LevelGrasslands as required above ('game.level.grassland')
		this.loadLevel( LevelGrasslands );


	},

	loadLevel: function( data ) {
		// Remember the currently loaded level, so we can reload when
		// the player dies.
		this.currentLevel = data;

		// Call the parent implemenation; this creates the background
		// maps and entities.
		this.parent( data );
		
		this.setupCamera();

	},
	
	setupCamera: function() {
		// Set up the camera. The camera's center is at a third of the screen
		// size, i.e. somewhat shift left and up. Damping is set to 3px.		
		this.camera = new ig.Camera( ig.system.width/3, ig.system.height/3, 3 );
		
		// The camera's trap (the deadzone in which the player can move with the
		// camera staying fixed) is set to according to the screen size as well.
    	this.camera.trap.size.x = ig.system.width/10;
    	this.camera.trap.size.y = ig.system.height/3;
		
		// The lookahead always shifts the camera in walking position; you can 
		// set it to 0 to disable.
    	this.camera.lookAhead.x = ig.system.width/6;
		
		// Set camera's screen bounds and reposition the trap on the player
    	this.camera.max.x = this.collisionMap.pxWidth - ig.system.width;
    	this.camera.max.y = this.collisionMap.pxHeight - ig.system.height;
    	this.camera.set( this.player );
	},

	reloadLevel: function() {
		this.loadLevelDeferred( this.currentLevel );
	},
	
	update: function() {		
		// Update all entities and BackgroundMaps
		this.parent();
		
		// Camera follows the player
		this.camera.follow( this.player );
		
		// Instead of using the camera plugin, we could also just center
		// the screen on the player directly, like this:
		// this.screen.x = this.player.pos.x - ig.system.width/2;
		// this.screen.y = this.player.pos.y - ig.system.height/2;
		if(!ig.ua.mobile){
		if (ig.input.pressed('music_down')){ig.music.volume -= 0.1;}
		if (ig.input.pressed('music_louder')){ig.music.volume += 0.1;}
		if (ig.input.pressed('music_off')){ig.music.volume -= 1;}
		if (ig.input.pressed('music_on')){ig.music.volume += 1;}
		}

	},
	
	draw: function() {
		// Call the parent implementation to draw all Entities and BackgroundMaps
		this.parent();
		

		// Draw the heart and number of coins in the upper left corner.
		// 'this.player' is set by the player's init method
		if( this.player ) {
			var x = 16, 
				y = 16;

			for( var i = 0; i < this.player.maxHealth; i++ ) {
				// Full or empty heart?
				if( this.player.health > i ) {
					this.heartFull.draw( x, y );
				}
				else {
					this.heartEmpty.draw( x, y );	
				}

				x += this.heartEmpty.width + 8;
			}

			// We only want to draw the 0th tile of coin sprite-sheet
			x += 48;
			this.coinIcon.drawTile( x, y+6, 0, 36 );

			x += 42;
			this.font.draw( 'x ' + this.player.coins, x, y+10 )
		}
		
		// Draw touch buttons, if we have any
		if( window.myTouchButtons ) {
			window.myTouchButtons.draw(); 
		}
	}
});

9 years ago by Joncom

When you see "...", it means omitted code.

MyGame = ig.Game.extend({
    
    ...

    coinSpawnPositions: [],
    coinSpawnTimer: new ig.Timer(),
    
    update: function() {        
        
        // Spawn coin after how many seconds?
        var seconds = 3;

        // Is it time to spawn a coin yet?
        if(this.coinSpawnTimer.delta() >= seconds) {
            
            // Is there something TO spawn?
            if(this.coinSpawnPositions.length > 0) {
                
                // Get first postion in array
                var position = this.coinSpawnPositions[0];
                
                // Spawn the entity
                this.spawnEntity(EntityCoin, position.x, position.y);
            }

            // Reset the timer
            this.coinSpawnTimer.set(seconds);
        }

        this.parent();

        ...
    }

    ...
});

EntityCoin = ig.Entity.extend({

    ...

    kill: function() {

        // Push position object into array for spawning later
        ig.game.coinSpawnPositions.push(pos);

        // Carry on with the usual kill code
        this.parent();
    }
});

9 years ago by fjorko

Tried your code, atleast I managed to start the game this time!

But get this error when I try to execute the kill fuction.

TypeError: ig.game.coinSpawnPositions is undefined
ig.game.coinSpawnPositions.push(pos);

main.js
MyGame = ig.Game.extend({
	
		...

		// coin array
	coinSpawnPositions: [],
    coinSpawnTimer: new ig.Timer(),
	
    	...
	
	update: function() {		
		
		// Spawn coin after how many seconds?
        var seconds = 3;

        // Is it time to spawn a coin yet?
        if(this.coinSpawnTimer.delta() >= seconds) {
            
            // Is there something TO spawn?
            if(this.coinSpawnPositions.length > 0) {
                
                // Get first postion in array
                var position = this.coinSpawnPositions[0];
                
                // Spawn the entity
                this.spawnEntity(EntityCoin, position.x, position.y);
            }

            // Reset the timer
            this.coinSpawnTimer.set(seconds);
        }



		// Update all entities and BackgroundMaps
		this.parent();
		...
	},
	
	draw: function() {
		...
	}
});

coin.js
ig.module(
	'game.entities.coin'
)
.requires(
	'impact.entity'
)
.defines(function(){
	
EntityCoin = ig.Entity.extend({
	size: {x: 36, y: 36},
	
	type: ig.Entity.TYPE.NONE,
	checkAgainst: ig.Entity.TYPE.A, // Check against friendly
	collides: ig.Entity.COLLIDES.FIXED,
	
	animSheet: new ig.AnimationSheet( 'media/coin.png', 36, 36 ),
	sfxCollect: new ig.Sound( 'media/sounds/coin.*' ),
	
	
	init: function( x, y, settings ) {
		this.parent( x, y, settings );
		
		this.addAnim( 'idle', 0.1, [0,0,0,0,0,0,0,0,0,1,2] );
	},
	
	kill: function() {

    // Push position object into array for spawning later
    ig.game.coinSpawnPositions.push(pos);

    // Carry on with the usual kill code
    this.parent();
    },
	
	update: function() {
		if( ig.input.state('a') ) {
			this.kill();
		}
		// Do nothing in this update function; don't even call this.parent().
		// The coin just sits there, isn't affected by gravity and doesn't move.

		// We still have to update the animation, though. This is normally done
		// in the .parent() update:
		this.currentAnim.update();
	},
	
	
	check: function( other ) {
		// The instanceof should always be true, since the player is
		// the only entity with TYPE.A - and we only check against A.
		if( other instanceof EntityPlayer ) {
			other.giveCoins(1);
			this.sfxCollect.play();
		}
	}
});

});

9 years ago by stahlmanDesign

You forgot the prefix this


ig.game.coinSpawnPositions.push(this.pos);

9 years ago by fjorko

Awesome you guys its working, thanks! :)

The things is im making a spelling game and have created entities of every letter in the alphabet. Gonna try to get all the letter-entities this respawn function but I dont think it will be smooth sailing the way im thinking of implementing it :D

Any thoughts about how I should think here? For example the way I think would probably be to create 26 arrays and 26 update statements, but that seems very unclean. Hmmm, gonna watch some tutorials and maybe I'll learn something useful :)

9 years ago by stahlmanDesign

I would just create one letter entity that can have 26 different values, and different graphics. Then when one of the instances gets killed, it goes into a respawn queue. That queue array fills up with letters, but each purged when respawn time is up.

So instead of having one respawnTimer, you will have to create an object for each letter killed and push that to an array. Then you would iterate over the objects and check their timers. An object might look like this:

// this code right before calling this.parent() in kill() of letter-entity
var killedLetter = {
	timer: new ig.Timer(2),
	pos: this.pos,
	value: this.letterValue //  "A", "B", "C", etc., however you represent value
}
ig.game.killedLetters.push(killedLetter);

Then in main.js update method iterate over killedLetters:
// iterate backwards because we will remove items from array and its length will change
for (var i = ig.game.killedLetters.length; i > -1; i --){ 
	var kl = ig.game.killedLetters[i];
	if (kl.timer.delta() > 0) {
		ig.game.spawnEntity(EntityLetter,x,y,{value:kl.value});
		ig.game.killedLetters.splice (i,1); // remove 1 item at index i;
	}
}

9 years ago by Joncom

Quote from stahlmanDesign
You forgot the prefix this


ig.game.coinSpawnPositions.push(this.pos);

Oops, good catch :)
Page 1 of 1
« first « previous next › last »