1 / 127

ActionScript For Flash

ActionScript For Flash. Object Oriented Programming OOP. Game Programmer.

spierre
Download Presentation

ActionScript For Flash

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ActionScript For Flash Object Oriented Programming OOP

  2. Game Programmer Using programming languages such as C++ and Java, a programmer writes the code that allows a game’s text and graphics to be displayed, develops the control systems that allow a player to interact with the game, creates the camera system that allows the player to view the game world, programs the physics system that affects the player and game world, writes the AI system that controls enemies and object scripting… you get the idea.

  3. Game Programmer One programmer may work exclusively on tools to help team members build the game more efficiently. Another programmer may write code to simulate real-world physics making water look realistic or develop inverse kinetics for characters. Inverse kinematics is the process of determining the parameters of a jointed flexible object (a kinematic chain) in order to achieve a desired pose. Inverse kinematics is a type of motion planning. Inverse kinematics are also relevant to game programming and 3D animation, where a common use is making sure game characters connect physically to the world, such as feet landing firmly on top of terrain.

  4. Game Programmer Like many of the jobs in the game industry programming jobs are becoming more specialized. Regardless of the position, a programmer needs to have an excellent understanding of mathematics, 2-D and 3-D graphics, physics, particle systems, user interface, artificial intelligence, input devices, and computer networking. These skills are always in high demand and some programmers make a good living as contractors, moving from project to project as “hired guns”, writing code and providing temporary solutions to beleaguered teams.

  5. Fundamentals • trace • variables • comparisons and conditions • loops • functions • relative paths and objects

  6. Fundamentals Trace • In Flash development environment, we can make good use of the trace function. With trace, we can display any text we want in the output window. The output window is simply an empty window that appears when we run a movie from within the development environment. This means that if we want to determine whether or not flash is actually executing a certain section of code, we could say something like: • trace (“I am executing this section now”);

  7. Fundamentals • When we ran the movie, Flash would display “I am executing this section now” in the output window as soon as it reached that line of code. The trace statement is also very useful for showing the value of variables at any given time, with something like this: trace (i); or trace (“The value of i is “ + i);

  8. Fundamentals • In the first case, you should see undefined, this happens because we have not defined the variable i. In the second case, “The value of i is undefined” will appear in the output window.

  9. Fundamentals Variables • Variables • In ActionScript, we can create and manipulate variables much as in any other compatible programming language. By default, if you refer to a variable before it has been defined, it will return the value undefined.

  10. Fundamentals • Numbers • At the core, variables are defined by simply setting a value to a variable name, like so: j = 5; trace (“The value of j is “ +j); • This would place the number value 5 in the variable j, and then display “the value of j is 5: in the output window. In this case, j is a number, and it can be adjusted with code as any number variable can be adjusted.

  11. Fundamentals • For example: j=5; trace (“The next value of j is “ + (j + 1)); This would display the output “The next value of j is 6”, because we’re outputting (j + 1) and not merely j.

  12. Fundamentals • In most programming languages, numbers are specified as either integer or decimal. In ActionScript, you don’t have to specify, Simply setting the variable will do the required work. So you can say: j = 5; and that’s just as valid as saying j = 5.1; • In both cases, j will be a number, but in the second case, it will contain the decimal number 5.1 (also called a floating point number). • Numbers can be manipulated in many ways. We can perform all of the basic arithmetic operations on them. • As seen by the next two tables:

  13. Fundamentals Basic Arithmetic Operations Assume j = 5 Addition k = j + 1; Value 6 Subtraction k = j – 1; Value 4 Multiplication k = j * 2; Value 10 Division k = j / 2; Value 2.5 As well, we can modify a number by simply adjusting its value with the increment and decrement operators, as seen in the next table:

  14. Fundamentals Use of Increment and Decrement Operators Assume j =5 Increment by 1 j ++; Value 6 Increment by anything k = 5; Value 10 j += k; Decrement by 1 j --; Value 4 Decrement by anything k = 5; Value 0 j -=k;

  15. Fundamentals • It’s important to know that incrementing with j ++ is faster and more optimized than incrementing with: j = j + 1; and decrementing with j – is more optimized than decrementing with: j = j - 1;

  16. Just for fun

  17. Fundamentals Strings • Strings • Strings are how we store words, letters and sentences. Yes, we can also store numbers as a string if we wanted to. The default way to create a string is very much like creating a number variable, except we use quotes, like so: s = “Hello World”; trace (s);

  18. Fundamentals • When you run, this code would display the text “Hello World” in the output window. Unlike a number, we cannot perform any arithmetic on a string, but we can “add” strings together, like so: s = “Hello”; t = “World”; trace (s + t); • This code would display the text “HelloWorld” (with no space between the two words.)

  19. Fundamentals • To place a space, we’d have to modify the code like so: s = “Hello”; t = “World”; trace (s + “ “ + t); or, we could have included the space character in the string, like so: s = “Hello “; t = “World”; trace (s + t);

  20. Fundamentals • When we use the + operator between two strings, then what we’re doing is known as concatenation. We’re concatenating the strings together into one long string. If we add a string and a number, we get a concatenation only, as shown: k = “Hello”; g = “3”; trace (k + g); This will send the text “Hello3” to the output window, even though g is a number and not a string. In fact, if we declare two variables that are both strings that contain numbers, the concatenation will still take place: k = “5”; g = “3”; trace (k + g); This would trace 53 to the output window, not 8, because the 5 and 3 are both strings, as they are defined with quotes, so they are simply placed next to each other, concatenated.

  21. Fundamentals Booleans • Booleans • A Boolean variable is a variable that has one of two values, true or false. A Boolean variable is used to keep track of states of thing, for example, has the player collected the magic ring? we might have a Boolean variable called hasRing, and it will be false until the player collects the ring, at which point hasRing will be set to true. Booleans are assigned and created like so: hasRing = true; or hasRing = false; • There are no quotes placed around the true or false, as they’re not strings. They’re treated specially by flash. Generally, we can safely think of a Boolean as either zero or non-zero, where zero always evaluates to false, and anything non-zero evaluates to true. The result of any comparison always returns a Boolean value, but we’ll be looking at this shortly.

  22. Fundamentals Comments • Any time you want to put text in your code that you do not want to execute, you can convert it into a comment. These are readable only by the developer, and they’re ignored by the compiler. To place a comment, simply precede the line in the code with two forward slashes (//). k = 4; // this is a comment k ++; The line “this is a comment” has no bearing on the execution of the other two lines of code. It’s there strictly to make the code clearer to the reader. One of the useful applications of comments is to “comment out” a line of code if you temporarily don’t want it to execute.

  23. Fundamentals • You could take the aforementioned code and do this: k = 4; // this is a comment // k ++; Now k will not be incremented, because the line of code k++ will be ignored and skipped. You can also comment out larger blocks of code by using a comment block. A comment block contains an opener /* and a closer */. Anything between the opener and closer will not run. For example: k = 5; /* j = 4; k += j; */ trace (k); While k would have been incremented by j, which was 4, it will not now because we have commented out the two middle lines of code. All that will happen now is that k will trace out as 5.

  24. Fundamentals Comparisons and Conditions • In ActionScript, we have all of the standard comparisons and conditions that are in other programming languages. By using the if statement, we can perform any logic, which will, in turn, affect the flow of our program. At its core, the if statement works on the Boolean principle. It’s structured like this: if (true) { //this code will execute } if (false) { //this code will not execute }

  25. Fundamentals • In cases in which you have only one single line of code running based on the if, then you don’t need curly braces: if (condition) trace (“My single line of output”); Now, we don’t normally put the straight values true and false inside an if statement. we normally put conditions in the if statement, which in turn evaluate to Boolean true or false. For example, we could say this: k = 5; if (k == 5) { trace (“k is indeed 5”); }

  26. Fundamentals • the condition is k == 5, which means, “k must be equal to 5”. If it is (and it will be because we’ve set k to 5 just the line before), then the text “k is indeed 5” will trace to the output window. Remember, a condition results in a Boolean: true or false. So, if we do the following: k = 5; trace ( k == 5); What will happen?

  27. Just for fun

  28. Fundamentals • The word “true” will appear in the output window. We don’t need an if statement to evaluate a condition. On the other hand, we can say: k = 5; trace (k == 6); This will trace the word “false” to the output window, because the condition is false; k does not equal 6.

  29. Fundamentals Conditions a == b equals a != b Not equal a = 4; b = 4; if (a == b) { trace (true) } if (a!=b) { trace (false) }

  30. Fundamentals Conditions a < b less than a <= b less than or equal to a > b Greater than a >= b Greater than or equal to a = 4; b = 5; if (a <= b) { trace (true) } if (a >=b) { trace (false) }

  31. Fundamentals Conditions • All of these comparisons can be performed with either numbers or strings. For example: s = “Hello”; t = “Hello”; if (s == t) { trace (“They’re Equal Hellos”); }

  32. Fundamentals Conditions • We can also combine conditions by using the AND and OR operators. The AND operator is specified like so: if (condition 1 && condition 2) { } • The OR operator is specified like so: if (condition 1 || condition 2) { }

  33. Fundamentals Conditions • For example, “if you finish your homework AND you are good for the sub you can go watch a movie.” It is clear that both of these conditions must be satisfied in order for the movie trip to occur. In code, this could be written like so: homeWorkFinished = true; goodForSub = true; if (homeWorkFinished && goodForSub) { trace ("Let's watch the movie"); } else { trace ("No movie for you"); }

  34. Fundamentals Conditions • On the other hand, imagine this statement. “if you get an A+ or you are good for the sub you will get to watch a movie. grade = “A+”; goodForSub = false; if (grade == “A+ “|| goodForSub) { trace (“You can watch a movie”); } else { trace (“No movie for you”); }

  35. Fundamentals Conditional Operation Results • Condition 1 Operator Condition 2Result true && true true true && false false false && false false true || true true true || false true false || false false

  36. Just for fun

  37. Fundamentals Conditional • The use of the NOT operator becomes more applicable when we want to use a Boolean variable like so: paidTicket = false; If (!paidTicket) { trace (“you haven’t paid your ticket”); } else { trace (“It’s about time”); }

  38. Fundamentals Conditional • In the previous example, the text “You haven’t paid your ticket” would be traced to the output window, because paidTicket is false, meaning that NOT paidTicket is true, because NOT false is true. (When is a false not a false? When it’s a true.)

  39. Fundamentals Conditional • Along with the if statement, we have the else statement. When we say else, it’s as if we’re saying “otherwise.” Imagine this statement. “If you start the fire, you will be warm. Otherwise, you will be cold.” See if you can write this in code now.

  40. Fundamentals Conditional fire = false; if (fire) { trace (“you are warm”); } else { trace (“You are cold”); }

  41. Fundamentals Conditional • We can, if we want, have more than two conditions, by using the else if statement. Imagine this statement. “if you have a chicken you will have chicken eggs, otherwise if you have a goose you will have goose eggs, otherwise you will have nothing.”

  42. Fundamentals Conditional haveChicken = false; haveGoose = false; if (haveChicken) { trace (“You have a chicken, and chicken eggs.”); } else if (haveGoose) { trace (“you don’t have a chicken, but you do have” + “ a goose and its eggs”); } else { trace (“You have neither chicken nor goose eggs”); }

  43. Fundamentals Conditional • The nature of “if / else / else if “ is that it will move on to the next condition only if the previous condition is not satisfied. If we wanted to handle a case in which we wanted to allow for having both chicken and goose eggs, or just chicken eggs, or just goose eggs, or nothing. What would we have to do? Code it now.

  44. Fundamentals Conditional haveChicken = true; haveGoose = true; if (haveChicken && haveGoose) { trace (“You have chicken and goose eggs.”); } else if (haveChicken) { trace (“You have a chicken, and chicken eggs”); } else if (haveGoose) { trace (“You don’t have a chicken, but you do have” + “ a goose and its eggs”); } else { trace (“you have neither chicken nor goose eggs”); }

  45. Just for fun

  46. Fundamentals Loops • The purpose of a loop in ActionScript is to provide us with a mechanism for repeating an action as many times as we want, without needing to re-write that action in code over and over. There are two types of loops that we’re interested in for this: • for loops and while loops

  47. Fundamentals For Loops • In a for loop, we’re essentially telling Flash to do something “for” a certain number of time, just as we would say to someone “walk forward for 15 steps.” We could represent this action with code like so: step = 0; trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++)); trace (“Taking step number ” + (step++));

  48. Fundamentals For Loops • That code would produce Taking step number 0 Taking step number 1 Taking step number 2 Taking step number 3 Taking step number 4 Taking step number 5 Taking step number 6 Taking step number 7 Taking step number 8 Taking step number 9 Taking step number 10 Taking step number 11 Taking step number 12 Taking step number 13 Taking step number 14

  49. Fundamentals For Loops • Notice that we’ve repeated the same line of code 15 times, and that we’ve also embedded the ++ increment within the line of code itself. We’re allowed to do this; the value of step will be incremented after it is used in the trace statement. If we wanted the value of step to be increased and then used in the trace statement we could say: trace (“Taking step number “ + (++step)); It’s important to notice that it’s doing the code 15 times, even though the last step is number 14. This is because the first step is step 0, so it’s 15 steps total, starting with step 0, and ending with step 14.

  50. Fundamentals For Loops • So it’s pretty clear that there has to be a better way of doing this. What if we wanted to take 150 steps or 1500 steps? Would we write the code 1500 times? No. We tell ActionScript to do something repeatedly, like so: for (step = 0; step < 15; step++) { trace (“Taking step number “ +step); } this will produce the exact same results as the previous example, except it has three lines of code rather than 15. The format of the for statement is as follows: for (run this once; this must evaluate to true; do this every loop iteration)

More Related