Jump to content
Search Community

BlitMask wrap; hitTest fails...

snick test
Moderator Tag

Recommended Posts

I'm having trouble with the BlitMask "wrap" feature in a game Im working on.

Basically, the game is a car on a scrolling landscape, with obstacles to avoid hitting using the UP and DOWN arrow keys; the "road" is actually an empty strip in the scrolling landscape)

http://www.byrographics.com/flash/drivingGame/

 

Once the scrolling MovieClip (the landscape) has reached the end of the first loop (scrolled on the x-axis to the actual length of the horizontal panel being scrolled; about 6000 pixels), my hitTest function completely stops working.

The masking, wrapping and runtime performance continue to function perfectly. Just the hitTest fails.

 

Thanks

 

Here's the code for the BlitMask:

lsm = new BlitMask(landscape,0,0,stage.stageWidth,stage.stageHeight,true,true,0,true);
lsm.enableBitmapMode();

 

Here's the bitmapData code for the hitTest:

var carRect:Rectangle = car.getBounds(this);
var carBmpData = new BitmapData(carRect.width,carRect.height,true,0);
carBmpData.draw(car);

var landscapeRect:Rectangle = landscape.getBounds(this);
var landscapeBmpData = new BitmapData(landscapeRect.width,landscapeRect.height,true,0);
landscapeBmpData.draw(landscape);

 

Here's the scrolling function (enter frame loop) that contains the hitTest:

function scrollPanel(e:Event):void
{
if (up)
{
	car.y -=  speed;
}
if (down)
{
	car.y +=  speed;
}

if (carBmpData.hitTest(new Point(car.x,car.y),255,landscapeBmpData,new Point(landscape.x,landscape.y),255))
{
	gamestarted = false;

	timer.stop();
	stage.removeEventListener(Event.ENTER_FRAME, scrollPanel);

	controls.txt.text = "You Crashed!";
	car.gotoAndStop(2);
	TweenMax.to(car, 1, {scaleX:10, scaleY:10, alpha:0, ease:Quad.easeOut});

	if (highscore < score)
	{
		highscore = score;
		controls.highScoreText.text = "High Score: " + highscore.toString();
	}
	score = 0;
}
else
{
	controls.txt.text = "Drive Safely!";
	landscape.x -=  speed * 4;
	lsm.update(null, true);
}
}

Link to comment
Share on other sites

First this is a very cool usage of BlitMask.

 

However, I'm not so sure the wrap + collision detection is going to work as you expect.

 

Keep in mind the BlitMask is basically a Sprite that contains a visualization of how the landscape would seamlessly wrap.

 

It appears you are doing a hitTest against the car and landscape, BUT landscape is always moving to the left forever and ever.

 

So once the landscape object moves offsecreen... it just keeps going and the hitTest with the car will never be true BUT BlitMask makes it appear like landscape is wrapping. If you trace landscape.x you will see that the value keeps decreasing (moving left).

 

I think what you need to do is hitTest agains the BitmapData that is being rendered INSIDE the BlitMask. Currently I don't believe you have access to that unless you were to additionally create your own routine to draw() the pixels from the BlitMask into another object that you hitTest against which would probably entail a performance hit.

 

perhaps greensock can shed some light on this.

 

I really don't mean to burst your bubble... I really like the concept behind this.

Link to comment
Share on other sites

Carl is exactly right - BlitMask just makes things appear to wrap seamlessly but your real object keeps going to the left (or wherever). It's impossible for BlitMask to solve your problem for you because imagine a case where your object is half-way off the edge of the BlitMask area - it appears as though half is on the right side and half is on the left side but in reality your object can only exist in one place at a time! BlitMask can't literally split your object into two pieces and distribute them on opposite sides of the stage. It simply uses blitting to copy the pixels accordingly. To accomplish what you're after, you'd have to either literally use 2 unique copies of your object(s) and hitTest() against them both or build your own hitTest() algorithm that figures things out based on the positioning of your object(s) and the way they wrap. I wish I had a simply answer for you :(

Link to comment
Share on other sites

Thanks guys.

I put this little script at the beginning of my enter frame loop to solve the problem.

Basically, I'm checking if landscape.x is equal to its negative width, then setting its x value to 0 if it's true.

It's visually seamless and the hitTest works on subsequent loops. Hope I'm not creating another problem by doing it this way.

I assume I am still leveraging the performance benefits of the BlitMask, although I am technically kludging a bit here.

function scrollPanel(e:Event):void
{
if (landscape.x == -landscape.width)
{
	landscape.x = 0;
	lsm.update(null,true);
}

 

Love the new classes, especially LoaderMax.

Great tutorials Carl!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...