Saturday, April 11, 2009

Learning by Trial

My Flash newbishness really shined through today.

I've had this bug that's been plauging me for about a week. I drew a goal object - even cut n pasted code from several sources - but it would just never show up. Well, turns out I had to not only addChild(crap I just drew), but you also have to addChild(object that drew that crap) and addChild(object that called that object). I guess I understand the logic behind it - one big chain of display objects linked together to the first Sprite iteration - but this is the kind of thing that you can't solve by poring over the API documentation or simple tutorials. Sigh.

I was very excited to fix this bug, as it allowed me to move all my other drawing code - ie, the HUD - out from the main file. I had some hideous hacks in place to get those score counters rolling. I went through my code and started OOP-ifying everything up and moving code bits out into their own files.

Unfortunately, this broke something else that I just can't figure out.

Now the Hud is it's own proper object. It works fine and dandy, until you hit the Reset button. The reset button creates a new instance of the world - which then in turn creates a new instance of the Hud. My traces show that the Hud is still receiving input and drawing things, but the screen doesn't update anymore.

My hunch is that this glitch is being caused because my textboxes still exist in the world (they weren't destroyed with the reset button) and I've just created new ones behind them or something. If anyone has any tips, let me know.

Some good news: The code I wrote for the goal area seems to work great. I have some coordinate conversions to do, but as soon as those are squared away I'll have a brand new form of gameplay: Get your squid to an objective without dying. :)

2 comments:

  1. I had been having confusing moments of drawing and adding children, but I've switched to a different structure: I will addChild in the constructor and then write a separate function that draws to it, which I call after the addChild. I try to write drawing code that takes a Graphics argument, and then I use graphics.clear() before redrawing that layer.

    For resetting, I wipe out all children (that me sound like a psycho killer) with: while (layer.numChildren > 0) { layer.removeChildAt(0); }

    P.S. My squid keeps dying, even with keyboard input. :( I think it needs some laser beams attached to its arms. ;)

    ReplyDelete
  2. Thanks a lot, Amit! I had found a similar solution looking through forums (the one for killing all your kids) and implemented that. I haven't tried your first suggestion though, adding the child first then using graphics.clear() later. I'll give that implementation a go.

    A lot of people complain it's too hard - I know it's a little difficult right now, but I'm ramping up the difficulty as I go along, to keep it a challenge to myself and keep the gameplay fun. When the end game turns up with a bunch of levels, these difficulty equivalents will be quite deep in the game. Nothing to worry about there. :)

    ReplyDelete