Johnny G Junior's NoteBook Treasures: April 2008

Monday, April 28, 2008

Definitions: Operators

Heres your list of symbols and function.

Operators:

+ addition. Adds or combines
+= add expression1 + expression2
[] intiates NEW ARRAY.
= assigns a value
|| OR
&& And
! not
/ divide
== equal to or SAME AS
> greater then
< less then
// start comment LINE
% calulate the REMAINDER of expression 1 divide by expresion 2
* multiply
() Perfoms a group on one or more parameters
- subtract
++ add 1 more
-- minus 1 more


examples:
(trace variable to see result)

nNum = "Number" RESULT Number

trace (1 + 1); RESULT 2

nNum1 = 3;
nNum1 += 1; RESULT 4

nNum[] = "book1, book2, book3"; RESULT from trace(nNum[0]) book1

nNum = 5
if (nNum > 5 || nNum == 5) { - if nNum is greater then 5 or nNum is same as 5

nNum = 5 * 5; RESULT 25

//nNum = 5; <-- line disabled. passed as a comment

trace (12%5); RESULT 2

i = 1;
i++; RESULT i = 2;

Dynamic Text

very quick and easy.

Dynamic text lets you fill in a text fields using scripts... why would you want that. Percentages are a good example. If you want a percent number to display, use dynamic text.

Ok. lets set one up real quick so you could see how it works.

on the stage, draw out a new TEXT box.
open the properties panel and lets turn the text to DYNAMIC and lets make it a variable name so we could REFERECE it in the script. lets name it tText



lets make a frame script to test the communication between the dynamic text and the action script.


tText = "we filled this text field";


alt + enter to test... and you will see that text come up in that dynamic text field.

% Loaders: understanding lesson

Math is very amazing... and the thing is we do it every day. Lets take the good old %. YOu go to a restaurant and its time to leave. you get your bill and its $25. How do you determine the 15% tip. FLASH BACK.. basic math.

EQUATION...

15% X
--- = ----
100% $25

Percentage Part
--------- = ---------
100% Total


we figure it out by cross multiplying. (15 x 25) / 100 = X
the answer is $3.75.
This is NO DIFFERENT THEN FIGURING OUT a percentage for flash. you just need to know the variables.

PERCENTAGE Bytes Loaded
------------ = -------------
100 Bytes Total


getBytesLoaded() - returns a numeral that have been downloaded of CURRENT MOVIE
getBytesTotal() - returns a numeral that of total bytes
_root - targets Main Timeline
_parent - targets Timeline ABOVE current movieclip timeline or object
_this - targets CURRENT timeline
1024 bytes are in a 1 Kbyte

Math equation example.

kbytesloaded = _root.getBytesLoaded()/1024;

kbytes is a variable and it ='s _root (main timeline) getBytesLoaded() (return number of loaded byte) divide by 1024. kbytesloaded will now = a number of bytes loaded in Kbytes.


Ok. anyyyway.

The equation for a percentage looks like this.

percentage = (Math.floor(_root.getBytesLoaded()/_root.getBytesTotal()*100));


percentage (variable) = Math.floor(round integer) of (bytesloaded/bytestotal * 100) same as finding a percentage of a TIP you leave at a restaurant. (look above to reference).

ok great. We now have a Number in the percentage variable, but thats all you will get. 1 number. you have to check that action script over and over again to have a new number... how do we do that. Well we make a loop. once it passes that frame in flash, in the next frame, just have it go back to the previous frame

gotoAndPlay(2); (if the script is on frame 2 - which it should be)

now it will constantly be in that loop.. no matter what... UNLESS you make some kind of CONDITION... if i go back to frame 2 and the bytesloaded and bytetotal are equal to eachother... then lets move to a total different frame (other then the next one because it will loop us again).

if (_root.getBytesLoaded() == _root.getBytesTotal()){
gotoAndPlay(3);
}


== means THE SAME AS. this script will go UNDER the loading check script.


how do i make a bar grow.. well the idea is that you scale the bar according to how much of a percentage is loaded...

make a MovieClip that has a colored box in it. heres the script.

this._xscale=((_root.getBytesLoaded()/_root.getBytesTotal())*100);


look familar. this._xscale = effect this timeline's xscale. the rest is the same equation as before. We didnt use Math.floor because we dont want it to only scale by WHOLE NUMBERS.. part numbers are fine.

I will post a complete script of a loader for flash... but this will help you understand it completely.

be good.

Math.floor()??

Math.floor() finds the integer number thats immediate below it. 85.44334 becomes 85. 55.5555 becomes 55.

used as Math.floor(number to evaluate)

Sunday, April 27, 2008

Loading an XML document

why o why does it have to be this damn confusing. cant it just be some button i can click and it just does it for me...


NO!

now on with the quick lesson.


xData = new XML();

xData.onLoad = function(success){
if (success) {
trace(this);
}
}
xData.load("playlist.xml");


xData = a variable we named. The variable contains a NEW XML document XML();

xData.onLoad = function(success){ = this is the start of a new function. (read functions below to understand a bit.) We are setting up a program to check xData when something is loaded into it. This program (function) will only start if something gets loaded. xData.onLoad = function(success) - onLoad is a command and now we set it with an = sign. (success) will be a boolean. either TRUE of FALSE and success will now be a variable in this function. if (success) { = start if statement saying... if success = true then do whats in { }... trace(this) will display what was loaded.

Now that we wrote a function to look for the xData information, we now need to load something in.

xData.load("filename");

this will excute right away. once it loads, if its successful, then it will trace. if the load is not success (wrong file name) then nothing will happen.

.........

Friday, April 18, 2008

Function() master.

Who doesnt love functions... really. they are mini programs YOU write up and excute them over and over and over and over again.... just to because you can.

Format.


function name(){
do this;
}


Usage:

name(); <- this will execute your mini program. ok.

lets write one right now.


function EvilStatement(){
trace("vampires are evil");
}


what did we just do. Well... we created a program. thats all. nothing at all will happen with you preview this (alt + enter). thats because you need to execute your program. to excute.. use the name you called it with the function symbol.


EvilStatement();


BOOoooM. when you preview this... it will say in the result box- vampires are evil.

what can you do with functions besides that? well... lots of things. i will briefly touch on a few more examples and you're on your own.

function EvilStatement(sentence:string){
trace(sentence);
}


what did i do now? well. ( ) this is like a container that you can make temp. variables to be filled for later. i made a variable called "sentence" and defined ":string" as a string. now when i execute this script, i have to fill it with what information for the container... and i can fill it with many different things. sentence is a variable filled with whatever i tell it to have JUST FOR THAT FUNCTION. it never comes out of it.


EvilStatement("vampires are evil");
EvilStatement("bad breath is evil");


now both things will go into the function one at a time and it will be contained in the variable sentence. then it will produce a result for each.. separate.

More advance example. Lets say you have to write a script for a on release button multiple times. you could write a function with a container waiting for a movieclip to fill... or i will just show you.

function ButtonAction (item:MovieClip){ //<- block a start
item.onRelease = function(){ //<- block b start
getURL("www.google.com");
} //< block b end
} //< blcok a end

ButtonAction(website_btn);
ButtonAction(search_btn);
ButtonAction(more_btn);


now each execution will fill the item with that name and move throught that script.

Party!!!!

IF true then do this or ELSE do this.

If else statements look a bit crazy but its more of a sentence then anything... you just need to know how to format it. I will try to explain it the simpliest way i can. it just may confuse the hell out of you, but thats the price you have to pay being a nerd.

so i go into a gas station and i want to buy a soda. im infront of the soda stand and see 2 drinks that i like. one of the drinks cost $5 dollars (the purple drink) and one is $10 dollars (sunny d). now its time for me to reach into my pocket and see what i have. when i reach into my pocket i could have possiblities. I could have a $5 or a $10 dollar bill in my pocket, (i know its more then a dollar and less then a 20). I pull out the bill and :::


IF (the bill is the same as a 5)
{ then i will buy the purple drink; }
ELSE
{ i can afford to buy the sunny d drink and i will; }


Well... thats the format in english but now i will break it down for real.

If (condition) {
do whatever's in these {blocks}
} else {
if that condition is not true, do this block set
};


Condition - it is a senero like 5 > 1. this is a true condition so the next thing to be read is the command following the condition in the {} <<-- a start and close block. if the condition was like this. 5 < 1. Well thats a false statement and it will move to the command and block. since it is false and we have an else, it will do whatevers in that block.

You can not have an else without an if. but you can have an if without and else.


if (bill > 5){
purchase the sunny d;
}


bill = 1 dollar making condition FALSe

if i reached into my pocket and i had a dollar... then it would just move out of the block {} and do nothing.

if (TRUE) do { this } else { this set }

Now we could have if statements within an if statement. you just have to follow the format already provide. think about it as if one format ate another.

if (condition) { <--- start block a(if)
if (condition) { <-- start block b(if)
do this;
} else { <--- end block b(if) start block b (else)
do this;
} <---- end block b (else)
} else { <---- end block a(if) start block a(else)
do this.
} <---- end block a(else).




if (bill == 5) { ** == is same as
if (bill == ripped){
i cant buy the drink;
} else {
i can buy the drink;
} else { <-- if bill is not the same as a 5 FALSE do this one.
its a 10 dollar bill and i can buy sunny d;
}


WOAH. so the senerio is the same as above. if i pull out 5 dollars out of my pocket, i will do whats in the block... but if my 5 dollars is ripped, then i cant use it... so i will do whats in the next if statement.

the key is ask yourself the logical question. If this is a true statement.. then what will i do... or ELSE i can do this...

Thursday, April 17, 2008

testing directory for WRITE permission

ok. this is just a directory test to see if you can execute a script to create a file on your server.


Create a new PHP file and name it what you want. heres your code.

 ////  
 $filename = "createdfile.txt";  
 $container = fopen($filename, 'w');  
 $content .= "this is content getting created in your text doc. WOW!";  
 $rwstatus = fwrite($container, $content);  
 fclose($container);  
   
 if(!$rwstatus)  
 {  
  echo "You do not have Read and Write permissions…creating files do not work. change your settings to 777 on your folder directory";  
 }  
 else  
 {  
  echo "you're the master. file was saved!";  
 }  
 ?>  


Upload and test file. if it works then a file will be created on your server call createdfile.txt.


Have fun!

Wednesday, April 16, 2008

PHP scanning directory to XML document

This script scans the directory the php files in, displays it on screen and creates an xml file on the server... ALREADY FORMATED!!!

you can change the directory if you need to in $dir and the xmlfile name in..$ourFileName

Im not going to explain this bad boy yet.. copy this script and paste it into a new PHP file of your choice. NAME IT WHATEVER YOU WANT DOT PHP.

Variables vs. Strings Vs. Numbers Vs. Quotes

variables = items u make up. hahaha.
Strings (words, sentences, absolutely anything giberish).. haha (my definition).
Numbers (ah.. its only numbers).

Flash knows numbers but flash dont know strings. for example.

a = 1;
b = word;
c = "word";
d = "1";

lets trace all of these.

trace (a); RESULT is 1
trace (b); RESULT is undefined
trace (c); RESULT is word
trace (d); RESULT is 1

ok.

Variables dont need quotes. they are defined and flash will read the script to find that variable and what it equals.
Numbers dont need quotes. flash loves numbers so it doesnt need anything to figure them out.
Strings need quotes. Think of strings as absolute... junk. "1" does not equal the number 1. it equals the symbol 1. it doesnt have a value. 1 = 1.

now with that said. lets go over the Results.
a's results is 1 because its a defined number
b is undefined because because its not a variable in the script. it equals nothing.
c's result is the word WORD... because it was in quotes. it means nothing to flash.
d's result is 1... but not the value, the symbol.

with that said. im out.

The magic of "TRACE";

trace - what can i say. its great. trace this... trace that... trace().

What does trace do for me?

trace(); is a command that reads your script and echo's a result. For example.

i = 1; // "i" is a variable i made up by using a = sign. i equals the number 1.

trace(i); if i trace the variable i... a result box will come up when you preview (alt + enter) the result will be 1.

name = "sam";
trace (name);

Result will be sam.

Get it?

Good. bye.

Random Swf Files - OH BOY

Quick and simple... thats what we love. We need to make a flash banner that randomly loads different swf files. Here you go.



filename = ["filename","filename"];
path = "http://www.domain.com/swf/";
i = filename.length
k = Math.floor(Math.random()*i);
loadMovie(path+filename[k], MC_box);


what did i write.... i dont know. lets go through it.
this script should be in a MAIN swf that all the other swfs get loaded into.

filename = ["filename","filename"]; - "filename =" this is a variable that we came up with. ["filename","filename"] <-- [ ] this means that you just made an ARRAY and entered the stuff in the middle into that array. (hope that makes sense). ; closes off that script

path = "url"; - "path =" is a variable that we made up again. "url" should be the folder path.

i = filename.length; = i is a variable containing a number. the number is the length (how many items) in the filename array. since i wrote 2, then i = 2;

k = Math.floor(Math.random()*i); = k is a variable also containing a number, but this time its random. we are using Math.floor() and Math.random() to help us choose a number randomly (WITHIN THE ARRAY).

loadMovie(path+filename[k], MC_box); = loadMovie() is a command in flash to load movie file. path+filename[k] <-- this is our 2 variables put together forming a path [k] is the random number. this is choicing the filename in the array. for more information. look up arrays... MC_box is just a movie clip i named in this flash movie. its an empty container.

Damn those Gray Box | Active X flash thing.

Making flash objects are cool and fun, but whats not fun is those damn gray boxes that come around that flash object in IE. I mean, why do we have to click on it to make it disappear, Then the flash becomes activated. why cant it just be activited already????


Well here you go. this script will help you out.

Make a Javascript file and name it
ieupdate.js.

objects = document.getElementsByTagName("object");
for (var i = 0; i < ojects.length; i++)
{
objects[i].outerHTML = objects[i].outerHTML;
}


in your HTML page, lets call out the script after the LAST OBJECT -
Start Javascript and include this in the start.







*** make sure that the src is pointing to the right directory. I would suggest that you keep the html's and the js file together in the same directory so this script works on its own.

Time Display for FLV [ :00/:30]




So you have a FLV file embedded into your flash, but you have no idea how to get some kind of time code on it. Well look no more. Here is a code that can save your life. I will break it down as we go along.

1. Create a layer in your project called "Actions" (not required, but very useful)

2. In a blank frame, put this script in.

**** NOTE OF SOME BASIC COMMANDS *******
; <-- this means END LINE. without this it will combine command and give you an error

FLASH IS CASE SENSETIVE when it is dealing with scripting. "Name","name","NAME","naMe" are all different entities.

******************************************


onEnterFrame = function(){;

/////////// TOTAL TIME SCRIPT ////////////////
timecode = Math.round(YOUR_MOVIECLIP_Name.totalTime);
tmins = Math.floor(timecode/60);
tsecs = Math.floor(timecode%60);
tmins = (tmins <10) ? "0"+tmins : tmins;
tsecs = (tsecs<10) ? "0"+tsecs : tsecs;
vidTime = tmins+":"+tsecs;

/////////// CURRENT TIME SCRIPT //////////////////
htimecode = Math.round(YOUR_MOVIECLIP_Name.playheadTime);
hmins = Math.floor(htimecode/60);
hsecs = Math.floor(htimecode%60);
hmins = (hmins<10) ? "0"+hmins : hmins;
hsecs = (hsecs<10) ? "0"+hsecs : hsecs;
headTime=hmins+":"+hsecs;

////////// Lets show the results in this variable ///////////

displayTime=headTime+"/"+vidTime;

};


ok, let me break this down starting from the top.

1. onEnterFrame = function(){;
- Invoked repeatedly at the frame rate of the SWF file.
This command will repeat this function we created over and over again(only whats in the {};. Usage in this example onEnterFrame = function(){};

2. timecode = Math.round(Your_Movieclip_name.totalTime);
- timecode is a variable that we just made up. it could basically be anything you want but you will have to reference this name thoughout the script. Math.round() - this command produces a number. This number is Rounded up to the nearest integer(up or down). Usage Math.round( x ); In this script we are asking for Flash to give us a number of (our movie clips.totalTime). totalTime is a property of flash, which in this command, will return a value for your movieClip.

3. tmins = Math.floor(timecode/60);
- tmins is a variable that we made up. again, a variable could be anything you name, but it needs to follow thoughout your script. This command we are finding the number of minutes in the clip and assigning it to the variable tmins. Math.floor = this is the closet integer that is less than or equal to the specific number or expression. Usage Math.floor (x). if x = 12.5 then the return a number of 12. In this script we are getting the Math.floor of (timecode divided by 60);

4. tsecs = Math.floor(timecode%60);
- tsecs is a variable we made up. We are getting the Math.floor of (remainder of timecode divided by 60); % = calcuates remainder for example (12 % 5) = 2 the remaining amount is 2.

5. tmins = (tmins <10) ? "0"+mins : tmins;
- wow what does this mean. this is nutty. Well hold on. tmins is a predefined variable that we created before. to explain the rest of this, i must explain the ? mark. ? is used as the following.
expression1 ? expression2 : expression 3. expression1 is evaluted to a Boolean value (true of false). if value is found true, then it will do expression 2, otherwises it will do expression 3. lets say tmins = 5. lets plug it into the equation. tmins = (5 < 10)[true] ? "0"+tmins : tmins; the result would be 05. we are saying by "0"+tmin - if the value is less then 10, but a 0 infront of the number, otherwise, leave the number whole. tmins = 20 tmins. timins = (20 < 10)[false] ? "0"+mins : tmins; the result would be 20.

6. tsecs = (tsecs <10) ? "0"+tsecs : tsecs;
- ok, this should be easier to understand now. if tsecs < 10 is true then add a 0 infront of tsecs, if its false, then just display tsec.

7. vidTime = tmins+":"+tsecs;
- vidTime is a new variable that we just created. in the variable we now placed a the values tmins and this string : and tsecs;.

///// now we created the same process for the current time script. the only difference is the first part of the script.

8. htimecode = Math.round(YOUR_MOVICLIP_NAME).playheadTime);
- this does Math.round on your clip's current playheadTime. the rest of the script is the same as the above script.

9. headTime=hmins+":"+hsecs;
- this is a new variable named headTime that stores the new varibles we created. hmins and hsec. check #7 for more detal.

Now we display the results.

10. displayTime = headTime+"/"+vidTime;
- this variable should be attached to a dynamic text box you created named displayTime [in the Var box].

11. }; this closes off the function that we left open in #1.


I hope this helps everyone to understand what was written. Good luck and thats all for now.

This site is for...

So i decided that this will be the place to store all my insane information...... JUST BECAUSE. Love it or leave it.