Jump to content

  • Log In with Google      Sign In   
  • Create Account


dgb

Member Since 19 Aug 2012
Offline Last Active Aug 23 2012 11:26 AM

Topics I've Started

AS3 Collision Issue

19 August 2012 - 11:35 PM

In this project I've been working on, the Player remains stationary in the center of the screen while the maps moves around based on keyboard input.
-Map is tile-based; all of the tiles I have placed manually into a Movie Clip which is 1280x896 pixels, the script then places each of these tiles into an Array (blockArray). Historically, I have always been fairly successful in passing collision information between the Parent script and the Child script (Movie Clip tile-holder) without much trouble using point collision.

-In more physics-based projects, I've had more success with rectangular collision (getRect, and such). In these cases, however, I've never used it before in conjunction with Tile Holder/Movie Clip approached described above.

The following code runs as part of the MoveBall/Player script, which updates per frame. When I run this in the test window, it always crashes the program - perhaps it can't get out of the "for" Loop?

for(var i:int=blockArray.length-1;i>=0;i--)
{
	 var theBlock = blockArray[i];
	 var blockRect:Rectangle = getRect(theBlock);
	 if (blockRect.intersects (newBallRect))
	 {
		  //collision treatment here
	 }
}

Below is the method I have been using to build the level in the constructor function:

public function startGame()
{
	 //constructor code
	 stage.focus = stage;
	
	 if (_Level == null)
	 {
		  _Level = new level_obj(); // this Movie Clip, "level_obj",  is where the blocks that are loaded into the blockArray reside.
		  addChild (_Level);
		
		 for (var i:int = _Level.numChildren-1;i>=0;i--) // this "for" Loop loads the blocks from inside the _Level Movieclip into the blockArray
		 {
			   if (_Level.getChildAt(i) is block_obj)
			   {
					blockArray.push(_Level.getChildAt(i));
			   }
		 }
}

the main problem I've been encountering here is that "getRect(theBlock)" is not getting the Rectangle of the specific block that the Player/Ball is supposedly colliding with - "blockArray(i)". According to my traces, the size of the "blockRect" is 1280x896 - the size of the entire Movie Clip that holds all of the blocks in the "blockArray" - rather than just one of the blocks "(i)", which are 64x64.

Curiously, I made a sort of marker Movie Clip that gets added to the _Level Movie Clip during the for Loop in the collision/movement script; this Movie Clip always attaches to the registration point of first Block that I placed there, rather than the registration point of the _Level Movie Clip, which is in the upper-left corner on (0,0), and is what the "getRect" command seems to be referencing.


So, my question is: is this a lost cause or is there something that I can put in or take out to make this work?

and if I am wasting my time with this approach, then which of these approaches sound the most viable?
1) use the regular Array method (newArray = [Block, Block, Block, Lavapit, Block, etc.] - the construction of the level is handled completely in the Game/Constructor Class.

2) go back to point collision; not sure how well this will work for a physics-based project, though.

3) or is there perhaps an alternate method that I am unaware of?

In any case; much obliged, all! Good night/morning/afternoon/evening.

PARTNERS