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 Alex

Timer documentation is SO complicated. I've reread it, but all I got was "TIME TIME RELATIVE TIME DELTA TIME -4 SET DELTA RELATIVE TICK TIME SECONDS" :)

Could please someone write an example of entity being killed on check after 1 second.

check: function( other ) {
BLAH BLAH TIMER DELTA TICK -5328 SECONDS TIME RELATIVE RESET SET DELTA BLAH BLAH
if (1 second has passed) { this.kill(); }
}

Thanks! I think this basic example should go to the documentation.

1 decade ago by Alex

I've got help from IRC, pure JavaScript:

check: function( other ) {
		setTimeout(function(){this.kill()}.bind(this), 100);
	},

Works great. But I'd still want to see how that would look like with Impact timer, because the one above will work independent of engine's timescale, not that I'm planning to change it.

1 decade ago by MikeL

Alex, you can have a look at the YOSS game code under entities >> explosion.js for a concrete example.

Here is a repost of the code:
ig.module(
'game.entities.explosion'
)
.requires(
'impact.entity'
)
.defines(function(){

EntityExplosion = ig.Entity.extend({
size: {x: 48, y: 48},

type: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.NEVER,

animSheet: new ig.AnimationSheet( 'media/Explode5-m.png', 48, 48),

  init: function( x, y, settings ) {
    //Received the coordinates of the center of the creating entity.
    //Subtract half of the size of the explosion from these numbers
    //in order to center the explosion on top of the entity.
    x = x - (this.size.x/2);
    y = y - (this.size.y/2);
    this.parent( x, y, settings );

    //We only want the explosion animation to loop once so stop is true.
    this.addAnim( 'idle', 0.05, [0,1,2,3,4,5,6,7], true );
    //Start timer so this entity can be killed after 0.5 seconds.
    this.timer = new ig.Timer(0.5);

  },

  update: function(){
    //If it has been more than 0.5 seconds then kill this explosion entity.
    if (this.timer.delta() > 0) {
      this.kill();
    }
    this.parent();
  },

});

});

As you can see, as soon as the explosion is initiated, a timer is set at 0.5 seconds. What happens from there is that this.timer.delta() is called during each update. Upon the first update that number will be something like -0.5 and will get incremented upwards. Eventually that number will become > 0 and instruction to kill the explosion entity is sent and that particular explosion is finito.

I like setting the timers in my init with the timer number in place. Even if the timer isn&039;t used until later for that particular entity, you can use #this.timer.reset() to simply start the timer over. In this way, all of your check functions only have to look for a delta of > 0. If you need to tweak the timers then they are all easily accessible in your init.

Also one gotcha, for some reason I will forget to put the ( ) after the delta call which will foul things up, because null will be returned. Make sure to use this.timer.delta() , of course substituting your own timer's name. Hopefully that helps. :)

1 decade ago by Alex

Thanks a lot, you made timers clear for me now :)

Had an issue, but worked it out. What I'm doing, is that a dropped item(entity) when overlapped by player(check) will disappear in 0.1seconds. Was a bit tricky for me because I thought overlap function was doing the check and then stopped, but it's actually doing it every frame while being overlapped. Didn't know :) So had to add

if(!this.timerstart) { ... } 

Without it it would require player to move out of the overlap for item to get "killed".

There it is:
init: function( x, y, settings ) {
		this.addAnim( 'idle', 1, [0] );
		this.parent( x, y, settings );
		this.timer = new ig.Timer(0.1);
	}, 
	
	check: function( other ) {
		if(!this.timerstart){
			this.timer.reset();
			this.timerstart = 1;
		}
	},
	
	update: function() {
		if(this.timerstart) {
			if (this.timer.delta() > 0) {
				this.kill();
			}
		}
		this.parent();
	}

1 decade ago by stahlmanDesign

@ MikeL This was really clear and helpful.
Page 1 of 1
« first « previous next › last »