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 igr

Hi there!
I am having trouble to implement the logic used in Biolab: the Hero approaches enemies and at a certain distance the enemies start shooting.

I used distanceTo (within update-function) and it worked fine. Then at a certain distance I spawnEntity (the grenade). But then the enemy is shooting as many grenades and there are frames, which I do not need! How do I take control over the grenade shooting numbers?

Thank you!

1 decade ago by MikeL

Probably the simplest approach is to incorporate a timer so that the enemy can only shoot so often while the Hero is in range (say every 2 seconds or something like that). Probably what is happening now is that for every frame the hero is in range, the enemy fires.

What you want to do is check if the Hero is in range AND that sufficient time has passed so that the enemy can fire. Hope that helps.

1 decade ago by igr

Thank you MikeL! That works like charm!

1 decade ago by janix2011

1 decade ago by Dhoombaby

hello igr,

can you please explain with source code how you used that timer with the distanceTo function??

1 decade ago by stillen

This is a super old thread.

This code works for me:
update:function(){
			this.parent();

			var player = ig.game.getEntitiesByType(EntityBaseplayer)[0];
			if(player){

				if(this.touches(player)){
					player.receiveDamage(20,this);
				}

				if(this.attackTimer.delta() < 3 ){
					//waiting
					this.vel.x = 0;
					this.currentAnim = this.anims.idle;
				}else if(this.attackTimer.delta() < 5 && this.attackTimer.delta() > 3){
					//trying to attack
					if(this.distanceTo(player) < 800 && this.distanceTo(player) > 200){
						this.currentAnim = this.anims.walk;
						if(this.pos.x > player.pos.x){
							this.vel.x = -50;
							this.flip = false;
						}
						if(this.pos.x < player.pos.x){
							this.vel.x = 50;
							this.flip = true;
						}
					
					}else if(this.distanceTo(player) < 200){
						if(this.pos.x > player.pos.x){
							this.flip = false;
						}
						if(this.pos.x < player.pos.x){
							this.flip = true;
						}
						this.vel.x = 0;
						this.currentAnim = this.anims.attack;
						if(this.currentAnim == this.anims.attack && this.currentAnim.frame == 5 && this.spawnChild != this.currentAnim.frame){
							var spot = this.flip ? this.pos.x+10 :this.pos.x-70;
							ig.game.spawnEntity(EntityBadblueshot,spot,this.pos.y-15,{flip:!this.flip});
							this.spawnChild = this.currentAnim.frame;
							this.currentAnim = this.anims.attack.rewind();
						}
						if(this.currentAnim.frame != 5){
							this.spawnChild = this.currentAnim.frame;
						}
					}else{
						this.currentAnim = this.anims.idle;
						this.vel.x = 0;
						
					}
				}else{
					// reset cycle
					this.attackTimer.reset();	
				}	
			}
		},

This is a patrolling enemy. He walks around and if the player is within a certain rage, he tries to fire and attack. If the bad guy attacks, he has to wait a few seconds before he can attack again. He also has a spawn timer as well, so he can only fire a certain amount of attacks within an attack sequence.
Page 1 of 1
« first « previous next › last »