Thursday, August 26, 2010

Algorithms are fun

I've been working on a persnickety (yes, I actually just used that word) little algorithmic issue for a new game I'm working on. The game is kind like Phlinx and Bubble Town, so there is a little projectile that flies up toward the board and, upon matching three or more, causes the pieces to explode and disappear.

That matching bit was fun in and of itself, but I got that one sorted out. The challenge now is the line-of-sight check for spotting where the projectile is allowed to land.

The problem is a little tougher because the board is staggered (Row 0 starts at pixel 45 and Row 1 starts at pixel 90...for example), so you can't just simply check on an array basis. You use the array, of course, but you have to kind of treat it a bit differently.

For example, consider:

[ ][ ][O][ ]
[ ][ ][X][ ]

[P]

Since the row offsets match, we can immediately see that the projectile (P) can not get to the open position (O) because there is a piece (X) in the way.

But, if we stagger the board:

[ ][ ][O][ ]
[ ][ ][X][ ]

[P]

...and if we imagine an line drawn from P to O, then the piece CAN hit that spot.

But how far can it go?

[ ][ ][ ][O]
[ ][ ][ ][O]
[ ][ ][O][ ]
[ ][ ][X][ ]

[P]

...looking at this board, an imagining that line draw up, the projectile can find its way all the way to the top of the board here.

Those are just a couple of the issues associated with this. Either way, it's a fun challenge (so far), but I'm certainly finding myself brooding more than coding at times. :)

Wednesday, August 4, 2010

Flash Debugging

So as I (and seemingly tons of other Flash devs) continue to struggle with memory and CPU issues, I searched for ways to help pinpoint issues. I found two items that I'm planning to try out today to help:

1 - De MonsterDebugger -- an AIR app that allows you to realtime debug on many fronts.

2 - EventController Lib - a library that manages events to make sure you don't end up with lingering event managers in your code.

With any luck I'll have good news on these two items soon. Either way, I'll post up how things are turning now.

**UPDATE:

Wow. The two items listed above are AWESOME. I highly recommend this to anyone serious about developing Flash apps/games. The bug I've been hunting forever has been found and (hopefully) squashed. And, yes, it was my fault.

I fell into the trap of expecting Flash's garbage collector to be more intelligent the larger system gc's because it is smaller world. Unfortunately, it's not. So, just like C's new and delete, so it goes heavily with Flash. If you want more details, check out this great article by Tom at Gabob.

It was in the comments of that article that I found the EventController Lib and from that I found the Monster Debugger.

Even though I was removing the event listeners from everything, I wasn't nulling things properly. I *thought* I was, but with the Monster Debugger I was able to see a lot more than I was getting from the Flash debugger. What I saw with Monster was how I was missing quite a few of nulls, and a couple of deletes too. Oops.

Anyway, now CPU usage is now quite nice (when the GC decides to kick off) and memory is staying low too.

I will likely integrate the EventController Lib too since it seems to be really intelligent in how it works. First, though, I want to see how things go now that I'm nulling/deleting in (again, hopefully) all the right places.

With my luck I'm probably still not 100% solved on this issue, but it's GOT to be better!

** Update Part 2:

Integrated EventController Lib. Nice. It was simple to integrate and it gives a simple interface for controlling events. Plus it can be easily set to log information over to Monster Debugger, so that's sweet.

So far I'm liking these new toys!