1 Replies - 1233 Views - Last Post: 15 May 2017 - 04:59 PM

#1 iSuckatProgramming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-May 17

AS3 targeting array objects for Collision detection

Posted 15 May 2017 - 04:09 PM

New to programming and really struggling with this one. I'm trying to set up my game so that each time you click the button on stage it adds an instance of an object to the stage and then apply a hitTest to each of those instances with the button that is dragging. Then once there are 8 instances on stage, I need to both remove and add one instance at the same time so there is no more an no less than 8 instances on stage. I can't figure out how to loop through the array so that each instance is targeted for a hitTest.



var pinkBox: pinkClass = new pinkClass();
var pinkArray: Array = new Array();


myButton.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownleftBtn); {
	function mouseDownleftBtn(e: Event): void {
		for (var i = 0; i < 4; i++) {

			var pinkBox: pinkClass = new pinkClass();
			addChild(pinkBox);
			pinkArray.push(pinkBox);
			pinkBox.x = Math.random() * stage.width;

		}
		myButton.startDrag();
	}
}

addEventListener(Event.ENTER_FRAME, checkCollision);

function checkCollision(event: Event) {


	if (myButton.hitTestObject(pinkBox)) {

		trace("hit")
	} else if (myButton.hitTestObject(pinkBox)) {    //this is the line causing problems. How do I target the box spawned?

		trace("hit")
	}
}
:code:

This post has been edited by modi123_1: 15 May 2017 - 04:30 PM
Reason for edit:: In the future, please use the [code] tag button in the editor..


Is This A Good Question/Topic? 0
  • +

Replies To: AS3 targeting array objects for Collision detection

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2411
  • View blog
  • Posts: 7,382
  • Joined: 15-January 14

Re: AS3 targeting array objects for Collision detection

Posted 15 May 2017 - 04:59 PM

You need to have a for loop that goes through pinkArray and accesses each of the objects in that array to test it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1