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 oronbz

Hey guys,
I'm about to complete Jesse's book and tutorial and trying to figure out one piece of code from its "Resident Raver" game.

This is the entity:
EntityDeathExplosionParticle = ig.Entity.extend({
		size: {x: 2, y: 2},
		maxVel: {x: 160, y: 200},
		lifetime: 2,
		fadetime: 1,
		bounciness: 0,
		vel: {x: 100, y: 30},
		friction: {x: 100, y: 30},
		collides: ig.Entity.COLLIDES.LITE,
		colorOffset: 0,
		totalColors: 7,
		animSheet: new ig.AnimationSheet('media/blood.png', 2, 2),
		
		init: function(x, y, settings) {
			this.parent(x, y, settings);
			var frameID = Math.round(Math.random()*this.totalColors) + (this.colorOffset * (this.totalColors+1));
			this.addAnim('idle',0.2, [frameID]);
			this.vel.x = (Math.random() * 2 - 1) * this.vel.x;
			this.vel.y = (Math.random() * 2 - 1) * this.vel.y;
			this.idleTimer = new ig.Timer();
		},
		
		update: function() {
			if (this.idleTimer.delta() > this.lifetime) {
				this.kill();
				return;
			}
			this.currentAnim.alpha = this.idleTimer.delta().map(
				this.lifetime - this.fadetime, this.lifetime, 1, 0
			);
			this.parent();
		},
	});

What i'm trying to understand is this couple of lines:
this.currentAnim.alpha = this.idleTimer.delta().map(
this.lifetime - this.fadetime, this.lifetime,
1, 0
);

I know .map() is an array method which also returns an array.
Here it applied on "delta()" and the return value is going to the "alpha" property, both (to my knowledge) are not arrays. what's the trick here? can anyone simplify this for me please?

Thanks,
Oron

1 decade ago by vincentpiel

map is one of the extension set by impact on numbers :

http://impactjs.com/documentation/class-reference/ig-core#number-map


Rq : using an extension method on a number first creates a Number - a wrapper around that number- then executes the method.
Garbage is created just for nothing each time.
All those handy helpers defined in Core should be defined as functions, not methods.

1 decade ago by oronbz

Thanks man, that helped alot!
Page 1 of 1
« first « previous next › last »