Tuesday, May 15, 2007

Actionscripting and Flash

Those just starting in Flash need to, first and foremost, learn how the logic of ActionScripting works. ActionScripting is a programming language used for Macromedia (Adobe) Flash that focuses on movie-clips, text fields and sounds. Its syntax is similar to JavaScript, but JavaScript deals more with windows and documents within HTML.

It took me a few months to finally have the logic of ActionScript click with my brain. Here's why:

I was working on a Flash-based videogame; a simple side-scroller action game. The idea was to have a sprite have three movie clips (MC) within him (stand, run, attack) and have the attack_mc hitTest with a demon_mc.

Now, the problem is that I have the demon_mc hitTesting with my sprite_mc in order to decrease health points so that you actually have a somewhat interesting game. I am also using the attachMovieClip function so that I did not have to place >50 demon_mcs on my timeline (which would increase my file size).

So there is a conflict - my sprite is trying to hitTest with a demon that is not on the timeline and my demon is hitTesting with a sprite. So what is the problem?

Well, after countless meetings with my professor and peers on how to solve this issue (as well as hours upon hours of searching for tutorials), the logic finally clicked. See, it WOULD make sense to have a hitTest on my attack_mc in my sprite_mc, right? WRONG. It actually makes MORE sense to have the hitTest on my demon_mc for when it connects with the attack_mc.

The final code looked something like this on my demon_mc:
onClipEvent (enterFrame) {
  if (this.hitTest(_root.sprite_mc.fight_full_mc)) {
  _parent.gotoAndPlay("die");
} else {
 if (this.hitTest(_root.sprite_mc)) {
  _global.hp -= (random(5)+1);
  _root.hpbar_mc.gotoAndPlay("hit");
  }
 }
}