Wednesday, July 9, 2008

Baby Steps

I think this is probably my first post ever that contains any actual coding in it, and frankly, I'm a little scared. This is such foreign territory for me, I don't know where to start.

So I'll start with a link.

Yes, it is in flash. I feel dirty with this being the first "complete" program I post, but we all have our flaws. I actually haven't paid for Flash CS3, but I'm using the 30-day free trial, and working fast. In terms of development, there are some things I like and some things I really dislike, but I'll get into that later.

If you can't tell by the URL, or the name, it's just a cheesy, quick Space Invaders clone. I programmed it in ActionScript 3, despite never having used it before, in about 3 hours. My main problem so far is that when there is contact between the lasers and their target. The lasers are (obviously) placed dynamically, and in their update method, I tried using the following line to "kill" invaders when they touch.


Which, on top of being horribly hardcoded, throws the following error.

Error #2025: The supplied DisplayObject must be a child of the caller.

I tried everything I could think of, and eventually just decided that it was just a "Flash Thing," until I realized that the lasers were being added like so:


The problem here was that when I was looking at how to dynamically add new objects, the tutorial didn't have their code in a function, it was just on the stage. Therefore, using the keyword this on the frame added for the stage, setting up for a similar removal with this.removeChild(). Apparently, when inside the function, this is not the stage. There was another issue, however.

The Invader still would not be removed via stage.RemoveChild. It continued to throw the "Child of the Caller" error. I intend to dig deeper into this, but not too much, though, since the issue is relatively moot. This is because the Invaders were added to the scene statically, which I found is unlikely to happen in any sort of "normal" game. Things that are dynamically added and removed (just as God intended,) come into and out of the scene without an issue. Not terribly enlightening, but at least it wasn't about the iPhone.

No comments: