This article is part of a new series on computer programming. Tyler will be sharing select experiences with programming along with the actual code which is used in solving the problem. The purpose is to allow others on the internet to learn programming along with Tyler and other contributors.
TURNIP PROGRAM:
In this Flash Program, you have the ability to move the box. Once you move the box to cover the turnip, you can attempt to make the turnip disappear.
Did your magic trick work? Was the turnip completely gone by the time you moved the box away?
OK! So several things are happening in this little Flash – Actionscript 3.0 exercise.
Although this program isn’t an actual “game” or anything of true entertainment value, it does illustrate 2 important programming issues:
1. The difference between “Alpha” or “Transparency” shifts and how both Boolean values used in conjunction with “if” statements can solve problems.
2. Simple “x” or “horizontal” movement of objects can create illusions in which the programmer can control.
In this article, we will have small snippets of code in another font type. But if you want to read the entire source code for this project, you are free to do so by clicking HERE. (please read the Terms of Service when using source code and reading these more technical articles. Thank you.)
Trying to get the turnip to turn invisible after a few clicks of the mouse didn’t work at first. It would get very translucent, but the turnip would never actually disappear. In terms of this magic trick, a partially disappeared turnip doesn’t have the full effect for our audiences.
The original code looked somewhat like this:
bg.turnip.alpha -= .1;
The problem with this code is that it never gets the turnip…completely gone! So when it decides to turn up, it actually has no intense effect on the viewer when the box is moved for the big reveal.
So the code was changed to this:
bg.turnip.alpha -= .1;
if (bg.turnip.alpha <= .2) { bg.turnip.visible = !bg.turnip.visible; }
This above code actually works. When the user clicks the “ATTEMPT TURNIP DISAPPEAR SPELL” several times they are actually reducing the “alpha” levels by .1 over and over again.
Then when the alpha levels get low enough, the “If” statement checks to see if the levels are “less than” .2 this used in conjunction with the “!” before the visible call creates a Boolean true/false statement.
So in English this code is really saying:
After each click of the “ATTEMPT TURNIP DISAPPEAR SPELL” button, please reduce the alpha or transparency levels of the turnip which is nested in the “bg” or background movie clip.
If the alpha levels are “lower or equal to” 20% of the total opaqueness of the turnip… then please make the turnip change from it’s current visibility designation, to the opposite of its current visibility designation.
As you can see, being able to mentally translate the computer code into English language is an important thing to keep in your hat. It doesn’t require any special skills either. All you have to do is say out loud what you’re trying to do and have some scratch paper by your desk for jotting down notes. If you have occasional notes and are willing to take short breaks, working through the debugging process is not very frustrating at all.
Also, here’s a tip:
When setting up your actionscript class codes, make sure to link the empty stage to your “Main” class. That was the most frustrating aspect of transitioning from my JavaScript training at Codecademy to my current training on Actionscript. When at Codecademy, you’re only using a temporary console. This isn’t an independent system such as a flash SWF file, so there’s nothing to actually “link” to when making projects.
Now if I see a blank SWF file, double checking the linkage between the document empty stage and the “Main” class in the properties section is the first thing I do.
I hope you enjoyed this article, and if you have any questions or comments feel free to leave one. Or if you prefer to talk via Facebook you can visit our page by clicking HERE.
-Tyler