I've always loved game design, since I was 8 years old and tried to make my first game in Q-Basic. While my techniques are now a lot more refined, my drive to create compelling, original games remains intact. Oh, and the BA in Game Art and Design doesn't hurt.
Tricky Foregrounds With No Respect
Wed, 02/03/2010 - 16:29

So here's how the foregrounds and backgrounds work in Squirrelfall: I have an array of objects, let's call this one foreArray. foreArray is a series of foreScrolls, all of which have the behavior to move upward at the level's speed * 2. When a foreScroll moves beyond the bottom of the stage, a new one is added to the bottom of foreArray.

The way that the spawning position of the foreScroll is determined is by taking the vertical location of the previous foreScroll on the stage and adding it's own height - so the new one goes to the bottom of the foreScroll's location on the stage. The code to spawn a new foreScroll:

for each( var foreObject:Scroller in foreArray ) {

// Spawn a new piece of foreground when the latest piece has entered the playscreen

if( foreObject.y + foreObject.height < stage.stageHeight && !foreObject.hasSpawnedNew ) {

foreScroll = new Scroller( 0, foreObject.y + foreObject.height );

Makes sense, right? Well I've been having this bug where the bottom-most foreScroll object will spawn higher up on the stage than it's supposed to - overlapping the foreScroll object that comes before it. This causes a problem with tiling - the foreScrolls do not move seamlessly with one another and it looks baaaad.

Not aligned!

Strangely, as the speed of the level increased, the problem seemed to get worse. So I did traces finding heights and positions, I mussed around with how they spawn, where they spawn, their speeds, how they get their speeds ... everything, with no results at all. I couldn't find any pattern.

Then, I decided to measure out exactly how many pixels difference there were in overlapping foreScrolls. I finally noticed a pattern: The difference between the top and bottom of the two was always the same number as the speed at which they were moving. So, if the foreScroll was moving at speed 10, the difference was 10 pixels. Of course, as the speed got higher, so did the difference.

So I altered my equation:

foreScroll = new Scroller( 0, foreObject.y + foreObject.height + foreObject.speed );

...and it works like a champ. In my mind, this does not make sense. The speed is how fast the objects move up the screen, and shouldn't really have any effect on the spawning positions of any foreScrolls. One large bug down, at least.

THEORY: The speed of the object is how far it moves up per 1/30 of a second - so, if the speed is 10, the object moves up 10 pixels per 1/30 of a second.  My theory is that perhaps in the time between when the next foreScroll is set up and when it actually spawns, a whole 1/30 of second goes by.  Thus, the next piece is off by exactly the speed's amount.

Check out the newly fixed backgrounds in Squirrelfall!

 

Making awesome stuff for the world costs money, and I've got shockingly little. Show your support with a donation!