creative programming in actionscript 3.0
TRANSCRIPT
Creative Programming in ActionScript 3.0
• Flash Platform Consultant• Lecturer Devine• Adobe Community Expert• Technical author• Full time web geek
Creativity is a mental process involving the generation of new ideas or concepts, or new associations of the creative mind
between existing ideas or concepts.
Programming is a the process of writing, testing, debugging and maintaining the source code of computer programs.
What will we talk about?
• Object-Oriented Programming
• Classes and instances
• Inheritance
• Encapsulation
• Basic trigonometry
• Distance between points
• Polar and Cartesian coordinates
• Experimentation
• BitmapData
• Sound Spectrum
Object-Oriented Programming
Classes and instances
Classes
• Classes are blueprints of functionality
• Allow for reusable and modular code
• Contain methods (functions) and properties (variables)
Ball class
Instances
• Instances (objects) are copies based on a class blueprint
• Instance property values are independent of its class
Ball class
Ball instance Ball instance Ball instance
Example (Library panel)
package {
public class HelloWorld {
public var name:String;
public function HelloWorld(value:String) { this.name = value; trace(“Hello world from “+ this.name); }
}
}
HelloWorld.as
var myWorld:HelloWorld = new HelloWorld(“Peter”);// Hello world from Petertrace(myWorld.name); // Peter
var otherWorld:HelloWorld = new HelloWorld(“Barack”);// Hello world from Baracktrace(myWorld.name); // Barack
HelloWorld instances
Inheritance
• Classes can extend each others functionality
• Each class can only inherit from one super class
• Use the “extends” keyword
Animal class
Dog class Cat class
Tiger class
Example (inheritance.fla)
Encapsulation
• Protect the inner workings of your class
• Access modifiers (public, private, protected, internal)
• Use getter/setter methods to provide access
getter/setter
class internals
other class
Access modifiers
• public - accessible from anywhere
• private - only accessible from within the class
• protected - only accessible within class and subclasses
• internal - only accessible within same package (default)
Getter/setters
• Looks like a method, behaves like a property
• Provides a single entry point for setting a property
• Useful for input validation
private var _age:Number;
public function get age():Number { return this._age;}
public function set age(val:Number):void { if(val >= 18) { this._age = val; } else { trace(“you are too young”); }}
package { public class Account { private var _email:String;
public function set email(val:String):void { if(val.indexOf(“@”) != -1) { this._email = val; } else {
trace(“invalid email”); } }
}}
Account.as
var myAccount:Account = new Account();myAccount.email = “[email protected]”;
Basic Trigonometry
point 2
point 1
Distance between points
A
BC
point 2
point 1
Distance between points
A² + B² = C²
d(P1,P2)=(x2‐x1)2+(y2–y1)2 √
Example (distance.fla)
(x,y)
Cartesian coordinates
x
y
(r, θ)
θ
r
Polar coordinates
(r, θ)
θ
C
A
B
A² + B² = C²
x=Math.cos(θ)*radiusy=Math.sin(θ)*radius
Converting polar coordinates
θistheangleinradians
Example (polar-animation.fla)
Experimentation
flash.display.BitmapData
• Allows you to get pixel level access to bitmaps
• Work with color channels
• Perform bitmap operations
SoundMixer.computeSpectrum
• Returns spectrum information of sound
• ByteArray with floating point values between -1 and 1
• Raw wave form or Fast Fourier Transform (FFT)
• Doesn’t work for microphone input (yet) :(
• see www.getmicrophone.com
Thank you!
Books
Foundation ActionScript 3.0 Animation “Making Things Move” Object-Oriented ActionScript 3.0
Websiteswww.gotoandlearn.comwww.levitated.net
Get in touch
Email: [email protected]: www.peterelst.comTwitter: peterelst