Hi Everyone,
This week, I want you all to review what we did last week and make certain that you understand most of it. This blog will deal with re-stating what you did for homework last week.
I know it seemed difficult what we have been doing, probably especially what we started last week for homework, but I promise you'll be happy with the results in the end. Try to keep in mind that fully-functioning scripts each week are NOT the most important thing. Try not to get stuck on any one thing to the detriment of other things for this class or for your other classes.
The most important thing is that you begin to UNDERSTAND what we are doing and that IF YOU DO NOT UNDERSTAND, THAT YOU COME TO CLASS WITH QUESTIONS!!!.
The second most important thing is that you begin to learn to find your errors. As I've mentioned, most of your errors continue to be naming errors. These are silly and annoying and keep you from finding out what really is the problem. Once you start to get the handle on things, you'll see that Flash and ActionScript are very powerful and can be extremely fun and interesting with what you can create.
Carter-
- WEEK 6
- Classwork
- LINK class files
- Review:
- MIDTERM—Exercise 1: Look through the steps below to review and reinforce what we have done over the past week for homework and in class.
- Let's start off where we left off in class. I'll try to explain once again the small script that we'd ended up with up to that point:
- package {
- public class MPlay {
- public var mName:String="";
- public var mVolume:int=10;
- public var mTList:Array;
- public var mTCurrent:int=0;
- public var mMix:Boolean = false;
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- public function next():void {
- if (mMix) {
- mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (mTCurrent==mTList.length - 1) {
- mTCurrent=0;
- } else {
- mTCurrent++;
- }
- }
- play();
- }
- }
- }
- This is our class declaration, and it includes 5 properties and 2 methods. The top 5 items are the properties, created as you would variables. And the bottom 2 items are methods, created as you would functions.
- Recall that a class is like a group of objects. I myself have several classes at TCI. What sort of objects are contained within my classes? Why student objects, of course.
- So, if I were to define one of my student classes, I would say that the student objects within my student class should have the following properties:
- person (no fish or antelopes allowed),
- education up to HS level,
- desire to learn,
- open mind,
- creativity;
- come to class,
- study,
- learn,
- create,
- bring apples to teacher.
- The properties are pretty self-explanatory, so let's examine the first method, the play() method:
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- That line of code is simply the function declaration, which we have seen many times so far in this class. When used inside of a class definition as we have here, we call this a method. A method is really just a function, but a function which belongs to an object In this case, our object is the class. Beyond that, it is a function of type void which means it will not return any data.
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- That line of code above is held within the function. It is a simple function because it holds only one line of code within it; and this line is a trace. All this will do is trace out some data and some text. The trace contains a concatenation which is simply combining the string "Playing" with the output from our Array, named mTList. As you should know, an array is an object which can hold more than one element of data, unlike a variable which can hold only one data element. To separate the data elements, each piece of information is given an index number. These index numbers, unless told to do otherwise, begin counting at 0. By default, our variable, mTCurrent, holds a piece of numerical data, and its default value is 0. Therefore, unless we somehow change that number, our player will continue to play track 0 over and over.
- The following line of code is the next function declaration which creates the function named next(). It is also of the type void because it will not be returning any data either. Remember, a function returns data when it is typed as something other than void and when it has a return statement within it. This function is a bit more involved than the previous, as it has several more lines within it.
- public function next():void {
- if (mMix) {
- mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (mTCurrent==mTList.length - 1) {
- mTCurrent=0;
- } else {
- mTCurrent++;
- }
- }
- play();
- }
- What we have inside this function are a pair of nested if/else statements. Nested means that one is inside of the other. An if/else statement deals with at least 2 conditions. This means, one thing will happen under one condition, and another thing will happen under another condition.
- With the first part, the if portion of our first if/else statement, the script must first determine whether mMix is true. If it IS true, then it runs the code between the curly brackets that follow (mTCurrent=Math.floor(Math.random() * mTList.length);).
- Basically, what that line of code is doing is generating a random number. Now, that number canNOT be completely random. Why not? Well, if we have only 10 music tracks, but we generate a completely random number, that number could be something completely strange and irrelevant, such as 8,237.41. For one thing, it is far beyond the number of tracks within our array (mTList), and for another thing, it has a decimal. What kind of track would be track number .41 ? Instead, what we need is to generate a random number that is no greater than our current number of tracks, whatever that may be. Currently, we have 10 tracks, at least I do in my player; however, it could be more or less in your player or some other player. We're not certain how many tracks it will be: we only know that we want is the total number of tracks in our array.
- It turns out that there is something that I have introduced to you that tells us the total number of elements within an array, and that is the length property. Therefore, mTList.length would tell us how many elements (i.e. how many tracks) were within our array.
- There is also a code element that is part of ActionScript that will automatically generate a random number; however, it only generates a random number between 0 and 1. This means that it will be some decimal number less than 1 but greater than zero, such as 0.2, or 0.88, or 0.413. All of those numbers are greater than zero, but they are still less than one. Now, once again, how does that help us? We need a number between between 1 and 10 (or 0 and 9), so how do we get from those decimal numbers less than 1, to some whole numbers? The answer is that we multiply those decimals TIMES the total number of tracks that we have: 0.2 X mTlist.length, 0.88 X mTlist.length, 0.413 X mTlist.length.
- But what numbers will we get when we do that? Well, 0.2 is easy:
0.2 X mTList.length = 2, and 2 is between 0 and 9 and it is a whole number without decimals. But, 0.88 and 0.413 are more complicated:
0.88 * mTList.length = 8.8,
and
0.413 * mTList.length = 4.13.
Those are both between 0 and 9, but they are decimal numbers. In fact, whatever random number we come up with, if we multiply it by our total number of tracks, we will always get some number between 0 and our total. Great, so what we need to do is type the following:
Math.random() * mTList.length;
You might've noticed that they are not whole numbers. They are NOT integers. To change them into integers, we must round them out to the nearest whole number. - It turns out that flash also has a little built-in function that rounds numbers to the nearest whole number: Math.floor. If you were to put some number, say 7.22, in between the parentheses, what you'd get would be the nearest whole number. In this case, it would be rounded down to 7. This solves the last problem, then, so we must put our number between the parentheses of the Math.floor() function.
We get our number with this: Math.random() * mTList.length;
Therefore, we must put that between the parentheses as follows:
Math.floor(Math.random() * mTList.length).
Therefore, the first IF portion of our if/else statement reads as follows: - if (mMix) {
- mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- . . .
- . . .
- }
- However, when mMix in the if portion does NOT evaluate to true, but instead evaluates to FALSE, then it skips that code within the curly brackets. What it does instead is move on to the else portion of the if/else statement and runs the code there between its curly brackets instead.
- public function next():void {
- if (mMix) {
- mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (mTCurrent==mTList.length - 1) {
- mTCurrent=0;
- } else {
- mTCurrent++;
- }
- }
- play();
- }
- But here, we also have ANOTHER if/else statement. This is where the nested conditions occur.
- The first if statement that we have already examined, has to determine whether mMix is true or if it is false. This is essentially deciding if we want the songs to play all mixed up, or to play in sequence, in order. If mMix is true, then that means we want them mixed up (to shuffle).
- If mMix is false, then that means we want them to play in sequential order from 1 - 10 (or 0 -9, according to the array).
- So, let's consider that mMix is false, for example. This means that the tracks will play in sequence; but what happens when the track number reaches the end? What if the track number keeps going up, say to 14, or 15, or 88? We don't have 14 or 15, much less 88 tracks. That means that we have to have some way to start back at the beginning once we reach the end of the track list. Once we reach the number of tracks in our list, and right now it's 10, we have to start back at the beginning.
- As mentioned, we currently have 10 tracks, so that means the length of our array, mTList would come out to 10, perfect! However, there's one little problem, the last item of our array has an index number of 9. Recall, we start counting in arrays at the number, 0. So, our tracks are numbered 0 through 9 and NOT 1 -10. This will always happen with arrays: the last item of the array will always be one less than the total number of tracks. In an array of 45 items, the last item will have an index of 44. In an array of 178 items, the last item will have an index of 177. This means that someArray.length minus one will give us the last index number, which for our array (mTList), we'd type like so:
mTList.length - 1. - So how does that help us? Well, it will help us determine when we reach the last track number, when we reach the end, so that we know when to start over again. Recall, here, we also have 2 conditions:
- one: if we have reached the end of the array, then we must start back at the beginning;
- and two: if we have NOT reached the end of the array, then we should just count like normal in sequence.
- The if portion of our if/else statement, should ask if we have counted to the end of the array, if the current track number is equal to the last track number of the array:
- if (mTCurrent == mTList.length - 1) {
- If that is true, then we have to start counting at 0 again:
- if (mTCurrent == mTList.length - 1) {
- mTCurrent = 0;
- }
- If that is false, then that means we haven't gotten to the end of the array, and that we haven't counted to the last number; therefore, we can continue counting normally. We can just add one to the previous track number and increment it upwards.
- if (mTCurrent == mTList.length - 1) {
- mTCurrent = 0;
- } else {
- mTCurrent++;
- }
- Finally, whatever the condition, whether we've either counted upwards, started over from the beginning, or mixed up the tracks, we then must play the track. This means we must call or invoke the play() function.
- public function next():void {
- if (mMix) {
- mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (mTCurrent==mTList.length - 1) {
- mTCurrent=0;
- } else {
- mTCurrent++;
- }
- }
- play();
- }
- Now, let's try and simplify a few things. As always, when I say simplify, what that means is to provide a way of making things more efficient in the long run. At times, especially when creating a small script like this one, it may not seem so much more efficient; however, when typing more lengthy scripts, it is always much better. One of those ways is to create, as mentioned last week, a constructor.
- A constructor allows you to initialize a class instance with only a few properties. Now, that first sentence may seem very difficult to understand, with the new term constructor, together with the terms initialize, class instance, and properties, so let me break it down.
- To initialize means to set an object at its beginning or starting point.
- A class instance, you will recall from my discussion in class this past week, is a particular example of a class. If I were talking about one of MY CLASSES, I would be referring to students; and each individual student is an instance of one of my student classes. We create our classes in an .as file, and we create its instances in .fla files.
- A property, once again, is a characteristic of an object. In this case, our object is the class we create, and its properties are the variables and arrays we create in the class.
- Finally, back to where we started, a constructor allows you to start up (initialize) a new example of the class (an instance) by setting only a few of its absolutely required characteristics (properties).
- package {
- public class MPlay {
- public var mName:String="";
- public var mVolume:int=10;
- public var mTList:Array;
- public var mTCurrent:int=0;
- public var mMix:Boolean=false;
- public function MPlay(
- mName:String="",
- mVolume:int=10,
- mMix:Boolean=false
- ):void {
- this.mName = mName;
- this.mVolume = mVolume;
- this.mMix = mMix
- mTCurrent=0;
- mTList = new Array();
- }
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- public function next():void {
- if (mMix) {
- mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (mTCurrent==mTList.length - 1) {
- mTCurrent=0;
- } else {
- mTCurrent++;
- }
- }
- play();
- }
- }
- }
- In order to test the class that we have so far, in the same folder, you'll need to create a new .fla file named myMPlay_ex.fla, and type the code in the actions panel that you see below:
- var myMPlay:MPlay = new MPlay("Carter's Music Player", 15, true);
- myMPlay.mTList =
- ["The XX - Crystalized", "Fever Ray - If I Had a Heart", "Crystal Castles - Celestica", "Animal Collective - My Girls", "Bat for Lashes - Moon and Moon", "New Order - Crystal", "Cut Copy - Hearts on Fire", "Of Montreal - Gronlandic Edit", "Janelle Monae - Many Moons", "The Rapture - Get Myself Into It"];
- function onNextClick(evt:MouseEvent):void {
- myMPlay.next();
- }
- next_btn.addEventListener(MouseEvent.CLICK, onNextClick);
- Once you do that, you'll need to follow the steps below as you did last week:
- In your .fla file, create a simple movieclip out of a rectangle or circle, or whatever you want by selecting it, choosing convert-to-symbol.
- Give it a name, select movieclip as type, and then click okay.
- Give this new movieclip the instance name of next_btn.
- When you test the movie and click on that button, it should activate the code and "play" the next song.
- The next step is to add a new property, but not one that is publicly available as those that we have typed up to now are. What we want is a variable that is set to private, which means it will NOT be available to modify. We use this for those variables or those properties that should not ever be changed, but which may be different for each class instance. The example that we used last week was the serial number. A serial number does not ever change, but it is different for each person and item.
- package {
- public class MPlay {
- public var mName:String;
- public var mVolume:int;
- public var mTList:Array;
- public var mTCurrent:int;
- public var mMix:Boolean;
- private var _mSerial:String;
- public function MPlay(
- mSerial:String,
- mName:String="",
- mVolume:int=10,
- mMix:Boolean=false
- ):void {
- _mSerial = mSerial,
- /*
- Notice that when I place the serial property above here, that I do not put underscore. That is because it is actually a different variable here in the constructor. The one without the underscore, is the one that will be available in the .fla file. The one with the underscore belongs here in the .as file.
- */
- this.mName = mName;
- this.mVolume = mVolume;
- this.mMix = mMix
- mTCurrent=0;
- mTList = new Array();
- }
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- public function next():void {
- if (mMix) {
- mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (mTCurrent==mTList.length - 1) {
- mTCurrent=0;
- } else {
- mTCurrent++;
- }
- }
- play();
- }
- }
- }
- Recalling from last week, if you were to try to trace() the value of mSerial, you would get an error. That is because it is set to private, but also because we haven't put it into the code in the .fla file:
- var myMPlay:MPlay = new MPlay("B031167", "Carter's Music Player", 15, true);
- Okay, now we're up to the part of the script that we did not get to last week and which is a little complicated. I want to add the get/set methods. These properties behave as if they were functions in that they not perform the actions of actually getting a value from a property and returning it for us to use; or setting a value, giving it the particular number or string that we desire.
- They are a pair of methods, and they have the same name. The get function is called whenever you need to read or see the property; and the set function is called when you want to assign a value (give a particular number or string) to that property.
- The reason we might want to use these two methods right now is because we may wish to change the current track number to something new, to set it to the next track number in sequence, from 7 to 8, or from 3 to 4, or from 22 to 23, for example; or we may wish to pick another track number at random.
- The variable that we have that pertains to the track number is named mTCurrent. Now, because we don't want this change just whenever, but only in particular circumstances, we'll first change it to a private property, and then we'll go about the get/set methods.
- package {
- public class MPlay {
- public var mName:String;
- public var mVolume:int;
- public var mTList:Array;
- public var mTCurrent:int;
- public var mMix:Boolean;
- private var _mSerial:String;
- private var _mTCurrent:int;
- public function MPlay(
- mSerial:String,
- mName:String="",
- mVolume:int=10,
- mMix:Boolean=false
- ):void {
- _mSerial = mSerial,
- this.mName = mName;
- this.mVolume = mVolume;
- this.mMix = mMix
- _mTCurrent = 0;
- mTList = new Array();
- }
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- public function next():void {
- if (mMix) {
- _mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (_mTCurrent==mTList.length - 1) {
- _mTCurrent=0;
- } else {
- _mTCurrent++;
- }
- }
- play();
- }
- }
- }
- The first of the methods is simple and straightforward: all we need it to do is to get a value from the property of the same name. This means, we want it to get whatever value is held inside of _mTCurrent, we want this method to extract it and feed it back to us. We will then use that value in the next method:
- package {
- public class MPlay {
- public var mName:String;
- public var mVolume:int;
- public var mTList:Array;
- public var mMix:Boolean;
- private var _mSerial:String;
- private var _mTCurrent:int;
- public function MPlay(
- mSerial:String,
- mName:String="",
- mVolume:int=10,
- mMix:Boolean=false
- ):void {
- _mSerial = mSerial,
- this.mName = mName;
- this.mVolume = mVolume;
- this.mMix = mMix
- _mTCurrent = 0;
- mTList = new Array();
- }
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- public function next():void {
- if (mMix) {
- _mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (_mTCurrent==mTList.length - 1) {
- _mTCurrent=0;
- } else {
- _mTCurrent++;
- }
- }
- play();
- }
- public function get mTCurrent();int {
- return _mTCurrent;
}
- var myValue:int = Math.min(12, 8);
- trace(myValue); // this would trace the value 8
- var myValue:int = Math.max(12, 8);
- trace(myValue); // this would trace the value 12
- We want to change the value back to zero if we've somehow gotten a track number that is below zero, but let's go backwards one step, and that is to the very important statement that reads: mTList.length -1: right now, we have 10 tracks in our list, if our current track number, mTCurrent, is 10, that means we've gotten to the end of the list and that we must start over from the beginning at 0, (we count from 0 - 9 in an Array of 10 elements). Also, remember that mTList.length is always going to be one greater than the index number of the last track. For an array of 10 elements—our array has 10 songs—the last element has an index number of 9 (once again, we count from 0 -9 in an array).
- var mTCurrent:int = 10;
- var myValue:int = Math.max(mTCurrent, mTList.length - 1);
- trace(myValue); // this would trace the value 10, because, for our array, mTList.length - 1 will be 9.
- From our script, the line of code at the beginning of this function
(public function set mTCurrent(value:int):void {
is where we first see the variable named value. The other method of the pair of get/set methods, the get mTCurrent method, actually goes and gets the value of _mTCurrent. That value is used by the set mTCurrent method. It is placed inside the value variable in that first line, for later use later down inside the function. - The next line of code:
value = Math.max(0, value);)
is going to compare the current value of the variable named value with 0. Obviously, any positive number other than 0 is greater than zero; so, only if the variable named value already has a value of something less than 0 will its value change. Only if it is negative will that line of code return zero as its value. This line of code is simply there to prevent negative numbers from entering into the picture. Remember, this line of code picks the GREATER of the two numbers; so, if 0 gets picked, that means that the variable named value had a value less than 0. - The 2nd line of code inside the function,
value = Math.min(value, mTList.length -1);
is going to compare the current value of the variable named value with the total number of tracks minus 1, which in our case is going to be 9, since we have 10 tracks. This means that if at some point the variable, value, goes above the total number of tracks, the script will automatically return the total number of tracks. Essentially, this line of code does NOT allow that variable to get any higher than the total number of tracks. - The 3rd line of code inside the function,
_mTCurrent = value;
sets whatever ends up being the value of the variable named value is also given as the value here to the variable that holds the current track number, _mTCurrent.
- Therefore, what we will have in the following new lines added to the script is code that prevent our track number from either being 0 or being greater than the total number of tracks.
- package {
- public class MPlay {
- public var mName:String;
- public var mVolume:int;
- public var mTList:Array;
- public var mMix:Boolean;
- private var _mSerial:String;
- private var _mTCurrent:int;
- public function MPlay(
- mSerial:String,
- mName:String="",
- mVolume:int=10,
- mMix:Boolean=false
- ):void {
- _mSerial = mSerial,
- this.mName = mName;
- this.mVolume = mVolume;
- this.mMix = mMix
- _mTCurrent = 0;
- mTList = new Array();
- }
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- public function next():void {
- if (mMix) {
- _mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (_mTCurrent==mTList.length - 1) {
- _mTCurrent=0;
- } else {
- _mTCurrent++;
- }
- }
- play();
- }
- public function get mTCurrent():int {
- return _mTCurrent;
- public function set mTCurrent(value:int):void {
- value = Math.max(0, value);
- value = Math.min(value, mTList.length - 1);
- _mTCurrent = value;
- }
- }
- package {
- public class MPlay {
- public var mName:String;
- public var mVolume:int;
- public var mTList:Array;
- public var mMix:Boolean;
- private var _mSerial:String;
- private var _mTCurrent:int;
- public function MPlay(
- mSerial:String,
- mName:String="",
- mVolume:int=10,
- mMix:Boolean=false
- ):void {
- _mSerial = mSerial,
- this.mName = mName;
- this.mVolume = mVolume;
- this.mMix = mMix
- _mTCurrent = 0;
- mTList = new Array();
- }
- public function play():void {
- trace("Playing: " + mTList[mTCurrent]);
- }
- public function next():void {
- if (mMix) {
- _mTCurrent=Math.floor(Math.random() * mTList.length);
- } else {
- if (_mTCurrent==mTList.length - 1) {
- _mTCurrent=0;
- } else {
- _mTCurrent++;
- }
- }
- play();
- }
- public function get mTCurrent():int {
- return _mTCurrent;
- public function set mTCurrent(value:int):void {
- value = Math.max(0, value);
- value = Math.min(value, mTList.length - 1);
- _mTCurrent = value;
- public function get mSerial():String {
- return _mSerial;
- }
- }
- }
- var myMPlay:MPlay = new MPlay("Carter's Music Player", 15, true);
- trace(myMPlay.mSerial);
100% success is NOT the most important thing. A SERIOUS and SINCERE EFFORT IS!!!.
No comments:
Post a Comment