Check Collisions For Multiple Objects


(Zouhair Elamrani Abou Elassad) #1

In my gave there is a chance that i have multiple worlds, so far i have just one, and i have on player in my game and i have to check for collisions with multiple objects, i started testing collisions in the world update function but then i found that i’m gonna have to do that for every object, i tried testing collisions in the update method in every object just like this :

Example Object :

  override public function added():void 
	{
		initWorld();
		
		super.added();
	}

   private function initWorld():void {
		
		_cave = this.world as CavernWorld;
		_player = _cave.player; // i get the player reference from the world
	}

   override public function update():void 
	{
		
		if (Globals.playerAlive == true) {
			var bonus:Entity = _player.collide("speedBonus", _player.x, _player.y);
			
			if (bonus != null) {
				trace('player hit bonus');
				Globals.playerVelocityX = 100;
			}				
		}
		
		super.update();
	}

My World :

 override public function update():void 
	{
			var level:Entity = _player.collide("level", _player.x, _player.y);
			if (level != null) {
				Globals.playerAlive = false;
				trace("lost from level");
				FP.world.active = false;
			}
			
			var stala:Entity = _player.collide("stala", _player.x, _player.y);
			if (stala != null) {
				trace("lost stala");
				FP.world.active = false;
			}
          }

I’m wondering what’s the best way to check for collisions in case i have several objects and possibly multiple worlds as well .


(Jean) #2

I’m not really sure of what you truly want, but I believe that this hint could help: You can set the same type to multiples entities of different classes aswell.


(Zouhair Elamrani Abou Elassad) #3

I Didn’t Know That, Thank You So Much :slight_smile: