GG Presents AS: THEORY/LOGIC
----------Introduction----------
Just knowing how to do actionscript and knowing the functions and such will not be sufficient enough to make an advanced game. Sure, you can go and make shooting games and simple enemy AI's and such, but it takes a lot more to make a game with complex AI or a complicated puzzle game. You have to be able to analyze the scripts and know what they do. Then, you have to be able to think up your own scripts.
THE GOAL OF THIS THREAD:
The goal of this thread is not to make a specific example or tutorial. It will accomplish 2 things.
1. You will be able to think like a computer in terms of processing script, which in turn will make you able to pull bugs out of script easier
2. You will be able to work backwards to make complex scripts
---------------Part 1-------------
Ok, how does the computer parse script? From top to bottom. Let's look at this simple script.
nmbr = 2
nmbr2 = nmbr*5
Now, you can just come to the conclusion that this script makes nmbr 2 = 10. Read every line of code.
make a variable named number and set its value to 2
make a variable named nmbr2 and set its value to nmbr times 5
I know this is simple stuff, but that is exactly how the computer thinks.
Consider this:
nmbr2 = nmbr*5
nmbr = 2
Now, all I did was switch the 2 lines of code around. But this time, even though the code is pretty much the same, nmbr 2 will end up as NaN. Why? Because nmbr was undefined at the time nmbr2 = nmbr*5 was read by the computer.
Here's a slightly more complicated script involving function recursion.
function freeze(){
freeze()
}
At first glance, that script might appear to be one that would freeze the computer. And yes it is, but just punching that into flash wont.
Translated into how the computer thinks:
declares a function named freeze with no parameters with the following actions
call function named freeze
end of function
That's it. ALl it does is declare a function. But, the next 4 lines of code the computer treats as ? lines of code.
function freeze(){
freeze()
}
freeze()
Into computer language:
declare function, blah blah we know this part
call freeze
end of function
// now the computer calls the function it just made
call freeze
call freeze
call freeze
call freeze
...
end function
In essence, the computer gets stuck in an endless loop.
If you have a code that's buggy, all you have to do is read the code from top to bottom. If something doesn't seem right, break it down. Why wont it work? I have some examples at the bottom of this thread for you to try.
---------------Part 2----------------
So now you know how to read script. But how can you write complex scripts? Here's some more simple problems.
You want score to increase by 1 every 10 frames. How do you do it without set interval?
Well, seems like a simple problem, but you have to realize that the computer takes orders in a basic form.
First, you have to translate the problem into computer speak.
actions to do once:
make a score variable
make a timer to keep track of when to execute the code
actions to do every frame:
add 1 to the timer
if timer is 10, indicating 10 frames, do the following actions:
add 1 to score
reset timer
Now, that might not be what you get the first time you think it through. You'll have to think how do i get an action to execute every 10 frames.
Well, every 10 frames is like a timer. So you make a count up or count down timer.
Well, once you know what you need to do, break it down to even simpler steps. After you get what I showed you above, it's time to translate into actionsctipt
onClipEvent(load){
score = 0
timer = 0
}
onClipEvent(enterFrame){
timer += 1
if(timer==10){
score += 1
timer = 0
}
}
Simple, eh?
----------------Part 3---------------
Here's some exercises for you to try, based on scripts i made in one of my puzzle games. These are much more complicated than the examples I showed above.
--EX 1--
Describe to me what this function does in STEP BY STEP form:
function ffffffffffffff(tgt) {
if (_root.meteor.enam == 1) {
tgt.xdiff = _root.meteor._x-tgt._x;
tgt.ydiff = _root.meteor._y-tgt._y;
tgt.radius = Math.sqrt(Math.pow(tgt.xdiff, 2)+Math.pow(tgt.ydiff, 2));
if (tgt.radius<=tgt._width/2+_root.meteor._wi
dth/2) {
backangle = Math.atan2(tgt._y-_root.meteor._y, tgt._x-_root.meteor._x)/(Math.PI/180)+270;
xv = _root.meteor.x_velocity;
yv = _root.meteor.y_velocity;
newspeed = Math.sqrt(Math.pow(xv, 2)+Math.pow(yv, 2));
_root.meteor.x_velocity = newspeed*Math.sin((backangle-180)*-1*(Math
.PI/180))*.90;
_root.meteor.y_velocity = newspeed*Math.cos((backangle-180)*-1*(Math
.PI/180))*.90;
_root.meteor._x = tgt._x+((tgt._width/2+_root.meteor._width/
2)*Math.sin((backangle-180)*-1*(Math.PI/18
0)));
_root.meteor._y = tgt._y+((tgt._width/2+_root.meteor._width/
2)*Math.cos((backangle-180)*-1*(Math.PI/18
0)));
if (tgt.movableing == true) {
_root.meteor.x_velocity += tgt.xA;
_root.meteor.y_velocity += tgt.yA;
}
hit.setVolume(100);
hit.setVolume(newspeed*5);
hit.start();
}
}
}
--EX 2--
Describe to me what these function do in ENGLISH:
function declareVariablesOnLoad(tgt, radius, speed) {
tgt.m_dir = 0;
tgt.m_speed = speed;
tgt.m_rot = radius;
tgt.movableing = true;
tgt.cptx = tgt._x;
tgt.cpty = tgt._y;
tgt.xA = 0;
tgt.yA = 0;
}
function xxxxxxxxxxxxxxxxx(tgt) {
tgt.m_dir += tgt.m_speed;
olxd = tgt._x;
olyd = tgt._y;
tgt._x = tgt.cptx+tgt.m_rot*Math.sin(tgt.m_dir*(Mat
h.PI/180));
tgt._y = tgt.cpty-tgt.m_rot*Math.cos(tgt.m_dir*(Mat
h.PI/180));
tgt.xA = tgt._x-olxd;
tgt.yA = tgt._y-olyd;
if (tgt.dead == true && _root.meteor.enam == 2) {
_root.meteor._x = tgt._x+((tgt._width/2+_root.meteor._width/
2)*Math.sin((tgt.backangle-180)*-1*(Math.P
I/180)));
_root.meteor._y = tgt._y+((tgt._width/2+_root.meteor._width/
2)*Math.cos((tgt.backangle-180)*-1*(Math.P
I/180)));
}
}
--EX 3--
Write a code for this:
I want a recursive exponent function that handles positive integers without using flash's built in Math functions, only *, +, -, /
Bonus: Make it able to handle negative integers too
--EX 4--
Write a code for this:
You know the ! thingy? (5! = 5*4*3*2*1, 3! = 3*2*1) Write a function for that that handles positive integers. You can only use *, +, -, /
This has been another AS thread by GG. (68 characters remaining)