Checking collision with emitters?


(Nate ) #1

I have a particle.as class and inside of it I have 4-5 different effects that I use in my game. I am looking to have the player able to attack, and for this I have set up a particle inside of my particle.as class. The question is, how would I go about checking collision for just one of my emitters?

Thank you!


(Zachary Lewis) #2

Particle and Emitter are both very lightweight classes designed to help draw lots and lots of particles. To do this efficiently, the particles are very stripped down graphics and don’t hold collision.

If you’re trying to perform per-particle collision, it would be much easier to fake it with a distance check or hitbox.


(Nate ) #3

Okay, thank you Zach I will look into doing this! :dancers:


(Nate ) #4

Zach can you elaborate please on the distance check/hitbox check that you mentioned? Thank you!


(Nate ) #5

I am using this code to collect the gems in my game, and this similar code to check for collision with enemies. How would I for example make my gems disappear when the player attack particle hits them?

Thanks!

	var collisionGem:theGem = theGem(this.collideTypes("gem", this.x, this.y));
		
		if (collisionGem != null)
		{
			world.remove(collisionGem);
			sfxGem = new Sfx(theGameSounds.SND_GEM_SOUND);
			sfxGem.play(.5);
			
			
				var q:int = 25;
				while (q --)
				emit.emit("get_gem", x , y + 50 );
		
			score++;
		}

(Nate ) #6

This is what I have coded up in an attempt for my attack to hit the gems and it throws an error which is basically telling me that collisionGem.x is null, which I know I just am not sure what I need for this to work. 550 is the max range of the particles in the attack. This code is in my player.as update function.

	if (isShooting == true && this.x + 550 >= collisionGem.x)
		{
			world.remove(collisionGem);
		}

(Nate ) #7

Any suggestions guys? I am really scratching my head over this. I would really like my hero to be able to shoot enemies D:


(Zachary Lewis) #8

Instead of using particles, it may be easier to just spawn a bunch of entities with actual hitboxes and run collision checks against them.


(Nate ) #9

Okay would I be able to set the motion of these entities like I was able to do in the particle class? Such as the angle, distance etc?


(Zachary Lewis) #10

Sure you can. I’d recommend creating a special Entity class for your bullets that has these properties.


(Nate ) #11

Okay thank you Zach!


(Nate ) #12

So can you help me possibly? This is what the attack looks like in my particle.as file, how would this translate to an attack entity for the same effect?

			var v:ParticleType = emitter.newType("attack",  [4]);
		v.setMotion(360, 550, .2, 5, 5, .3, Ease.cubeOut);
		v.setColor(0xf5ef4f, 0xf5ef4f, Ease.quadIn);

(Zachary Lewis) #13

You’d want an animated sprite with a motion tween and a color tween.


(Nate ) #14

Thank you, Can you help me set up this tween?


(David Williams) #15

I lost the source code to my game where I did something similar for a flamethrower, but here is the class decompiled. Fireball.as

package 
{
	import net.flashpunk.*;
	import net.flashpunk.graphics.*;
	import net.flashpunk.tweens.misc.*;

	public class fireball extends Entity
	{
		private const flame:Class;
		public var fireGFX:Image;
		private var angle:Number;
		private var vx:Number;
		private var vy:Number;
		private var speed:Number;
		private var duration:Number;

		public function fireball(param1:Number, param2:Number, param3:Number, param4:Number, param5:Number)
		{
			this.flame = fireball_flame;
			this.fireGFX = new Image(this.flame);
			super();
			this.x = param1;
			this.y = param2;
			this.angle = param3;
			this.speed = param4;
			this.duration = param5;
			this.vx = Math.sin(this.angle * FP.RAD);
			this.vy = Math.cos(this.angle * FP.RAD);
			this.fireGFX.alpha = 0.75;
			graphic = this.fireGFX;
			setHitbox(6, 6);
			type = "fire";
			layer = 1;
		}

		override public function added() : void
		{
			var _loc_1:VarTween = new VarTween(this.die);
			_loc_1.tween(this.fireGFX, "alpha", 0, this.duration);
			addTween(_loc_1, true);
		}

		override public function update() : void
		{
			if(Player.pause)
			{
			}
			else
			{
				x = x + (this.vx * this.speed);
				y = y + (this.vy * this.speed);
				if(collide("enemy", x, y))
				{
					die();
				}
			}
		}

		private function die() : void
		{
			if(this.world)
			{
				world.recycle(this);
			}
		}

		override public function updateTweens() : void
		{
			if(Player.pause)
			{
				return;
			}
			super.updateTweens();
		}
	}
}

(Nate ) #16

Okay thank you! I will look into using this code! For now I have a temporary fix/work around! I made a separate image class using the particle image that I wanted. I re-created roughly what my particle/emitter was doing, then I overlayed the particle/emitter on the image that I re-created and make the visibility of the image false and it seems to be working now, so it looks like the particle/emitter is what you shoot with but there is an invisible line of hitboxes also spewing out in the same direction! :grin: