1 Replies - 5713 Views - Last Post: 07 July 2012 - 11:27 AM

#1 jay1459  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-November 10

AS3 Colliding multiple MovieClips

Posted 02 July 2012 - 03:22 PM

hello, I'm trying to check Collision between player() and enemySpawner()

however I have absolutely no idea how to, Id really appreciate any insight into how to do this properly and have supplied all appropriate code, case "gScreen" is where this starts as i have cut off the code before, thankyou for reading this.

		case "gScreen":
		{
			var level:MovieClip = new Level1();
			
			level.x = stageWidth/2;
			level.y = stageHeight/2;
			
			addChild(level)
			
			playerSpawn();
			
			gameState = "game";
			
			break;
		}
		
		case "game":
		{		
			enemySpawner();
			break;
		}
		
		default: break;

/////////////////////////////////////////////////////////////////////////////////////////////////

function playerSpawn()
{
	var player:MovieClip = new cPlayer();
	
	player.x = 260;
	player.y = 260;
	player.scaleX = 0.35;
	player.scaleY = 0.35;
	
	player.addEventListener(Event.ENTER_FRAME, playerHandle)
	addChild(player);
}

var toggleMove:Boolean = true;
var pressMove:Boolean;

function playerHandle(event:Event)
{
	var player:MovieClip = event.currentTarget as MovieClip;
	
	if((!pressMove) ||(!toggleMove) )
	{
		if(keyPressedW && player.y > 100)
		{
			player.y -= 66.85;
		}
		if(keyPressedS && player.y < (stageHeight - 100))
		{
			player.y += 66.85;
		}
		if(keyPressedA  && player.x > 100)
		{
			player.x -= 66.85;
		}
		if(keyPressedD && player.x < (stageWidth - 100))
		{
			player.x += 66.85;
		}
		
		pressMove = true;
	}
	
	if(keyPressedW == false && keyPressedA == false && keyPressedS == false && keyPressedD == false)
	{
		pressMove=false;
	}
}

function enemySpawner()
{
	var cannon:MovieClip = new Cannon1();
	var sideSpawn:int = randRange(1, 4);
	
	if(sideSpawn == 1)
	{
		cannon.x = globalSpawn;
		cannon.y = enemySpawn[randRange(0,6)]
		cannon.dir = "left"
		cannon.scaleX = -0.6;
		cannon.scaleY = 0.6;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
	
	if (sideSpawn == 2)
	{
		cannon.x = enemySpawn[randRange(0,6)]
		cannon.y = globalSpawn
		cannon.dir = "up"
		cannon.scaleX = -0.6;
		cannon.scaleY = 0.6;
		cannon.rotation = 90;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
	
		if (sideSpawn == 3)
	{
		cannon.x = stageWidth + globalSpawn;
		cannon.y = enemySpawn[randRange(0,6)]
		cannon.dir = "right"
		cannon.scaleX = 0.6;
		cannon.scaleY = 0.6;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
	
	if (sideSpawn == 4)
	{
		cannon.x = enemySpawn[randRange(0,6)]
		cannon.y = stageHeight + globalSpawn
		cannon.dir = "down"
		cannon.scaleX = -0.6;
		cannon.scaleY = 0.6;
		cannon.rotation = 270;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
}

function enemyMover(event:Event)
{
	var cannon:MovieClip = event.currentTarget as MovieClip;
	
	if(cannon.dir == "left")
	{
		cannon.x += enemyMoveSpeed
		
		if(cannon.x > (stageWidth + 40))
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	if(cannon.dir == "up")
	{
		cannon.y += enemyMoveSpeed
		
		if(cannon.y > (stageHeight + 40))
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	if(cannon.dir == "right")
	{
		cannon.x -= enemyMoveSpeed
		
		if(cannon.x < -40)
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	if(cannon.dir == "down")
	{
		cannon.y -= enemyMoveSpeed
		
		if(cannon.y < -40)
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	
}




Although messy, I know the code, if any clarification is required, please ask

This post has been edited by GunnerInc: 02 July 2012 - 03:36 PM
Reason for edit:: Removed big font tag


Is This A Good Question/Topic? 0
  • +

Replies To: AS3 Colliding multiple MovieClips

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: AS3 Colliding multiple MovieClips

Posted 07 July 2012 - 11:27 AM

Did you try using hitTest function?

You can read about it here: http://help.adobe.co...t=00001314.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1