1 / 26

Actionscript Object Oriented Programming

Packages & Classes. Actionscript Object Oriented Programming. ActionScript OOP & Classes. Essentially, planning and organizing your code into simple chunks of data or objects that can be passed through reusable functions is OOP .

Télécharger la présentation

Actionscript Object Oriented Programming

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. Packages & Classes ActionscriptObject Oriented Programming

  2. ActionScript OOP & Classes • Essentially, planning and organizing your code into simple chunks of data or objects that can be passed through reusable functions is OOP. • Reusing functions and objects saves a lot of time when expanding a project. • However, It takes a while to organize a project that will incorporate OOP, so pick projects that are best suited for it. When to use OOP • Large website with multiple developers. • Retail websites where production artists will update pages daily. • When developing a prototype for a website that you will not specifically be doing the production on. • Parsing PHP generated XML for RIA. • Creating Games. • Creating a particle effect animations. When not to use OOP • proof of concepts. • one off animations. • static websites. • banners (in most cases)

  3. ActionScript Classes Move code from first-frame/timelineinto a separate .as Class file. Create an empty, external .as Class file to hold AS3 code. Your project will consist of two files: a Flash .fla file, which has a Ball Movieclip in the library, and a Class file with an .as extension. The .fla and the .as file should exist side-by-side in the same directory.

  4. ActionScript Classes Here’s the frame code: • Let’s take a piece of frame code, and bring it outside a Flash .fla file into a separate .as Class file. • This code grabs a MovieClip called “Ball” from the library and places it on the stage. • On every frame, the ball moves to the right 5 pixels and down 5 pixels. • (Note: you don’t need to copy/paste this code and try it.)

  5. ActionScript Classes Classes Classes demand a particular structure: Notice a few things: Package (whatever that is) wraps the whole thing the Import statements (whatever they are) come next the Class Definition (huh?) wraps everything else the Instance Variables, Constructor Function and other methods/functions are tucked inside the Class wrapper. You can put these three things in any order, but this is how you’ll usually see them in the wild.

  6. ActionScript Classes Package • The Package declaration is a way of grouping all your Classes together.:

  7. ActionScript Classes • When working outside Flash, you have to tell the compiler to grab portions of the AS3 language before you can use them. • So if you say this in an .as Class file: • the compiler will throw you an error, effectively saying “What’s a MovieClip?” • You must use an Import Statement to explicitly tell the compiler that you are using a certain part of the AS3 language. • Use an ImportStatement like this at the top of the file: Import Statement ActionScript 3 language is already split up into packages. When you put your code on a frame in Flash, all of those packages are automatically accessible. You don’t have to do any extra work.

  8. ActionScript Classes Class Definition This is where you actually start writing your Class. Here’s the format:

  9. ActionScript Classes Class Definition Let’s break down the line piece by piece: public Class Main extends MovieClip use “public” here. If you don’t use “public” in the Class that replaces your first-frame code, you’ll get an error. public Class Main extends MovieClip Starts Class definition. public class Main extends MovieClip This is the name of the Class. Feel free to use your creativity here – you can call it whatever you like. This is where you actually start writing your Class. Here’s the format:

  10. ActionScript Classes Class Definition Naming classes - a few important rules: You must use standard naming conventions. Don’t start the name with a number, and stay away from spaces and special characters. Use a capital letter to begin your Class name When you save the file, the file MUST have the same name as your Class. If your Class is called Main, save the file as Main.as. If it’s called MyProject, save the file as MyProject.as. If you don’t do this, there will be trouble. This is where you actually start writing your Class. Here’s the format:

  11. ActionScript Classes Class Definition public class Main extendsMovieClip Uses the concept of “inheritance.” That’s what the “extends” keyword is all about. Without exploring too deeply, we need to extend / subclass / inherit from an ActionScript 3 Class called MovieClip. public class Main extends MovieClip That’s the Class we’re inheriting from. It’s like saying “my Class called Main can do everything a MovieClip can do, and more.” This is where you actually start writing your Class. Here’s the format:

  12. ActionScript Classes Instance Variables, sometimes called Fields, are variables that you need to access throughout your Class. Recall the idea of scope, so if you wrote this: You wouldget an error doing this: because myVariable is scoped to myFirstFunction. It doesn’t exist outside that function. Instance Variables

  13. ActionScript Classes • If you want variables to be accessible to all the different functions in your Class, you declare them in the orange area on the diagram. • You’ll also need to decide whether you’re making these variables public, private, internal, protected, etc. • One recommendation: make all your variables public for now. It’s the least error-prone approach when you’re learning, and when you know more, you can get a little fancier. Instance Variables

  14. ActionScript Classes So here’s what a typical Instance Variable would look like at this point in the .as Class file: publicvarmyVariable:String; Notice that you are not setting a value yet. You can, but you don’t have to. This just reserves space in the computer’s memory for this variable so that the whole Class can use it. Instance Variables

  15. Encapsulation(Hiding the details) • Encapsulation allows us to hide class properties and methods from other areas of our project, but still allows us to manipulate them in a controlled way (classes hide their own internal data) • An Access modifier is a modifier that changes the visibility of a class or its members (variables, functions etc) • In AS3 there are four different Access modifiers: • Public: The public modifier provides access to external members • Private: The private members are not exposed to external, instances or extended classes • Protected: Access from sub-classes • Internal: Public to classes in same package • Note! In this lecture we are going to focus on the Public and the Private modifier

  16. ActionScript Classes Constructor Function A Constructor Function is like the point of entry in any Class. It’s the first function that’s fired when a Class is encountered by the interpreter. Here’s what our Constructor Function should look like:

  17. ActionScript Classes Constructor Function Rules for the Constructor Function: The Highlander rule: “there can be only one in AS3. The name of the Constructor function must be the same as the name of the Class. Make sure it’s public. Don’t return a value – not even “void”. Constructor Functions are not allowed to return a value.

  18. ActionScript Classes Complete Package

  19. ActionScript Classes • You would save it as “Main.as”, or whatever you called your Class. • TellFlash that instead of looking to the timeline/the first frame of the movie as the point of entry, look at the “Main” Class file. • Open up the Properties panel in Flash and look for an empty field labeled “Class:” (CS4). Type “Main” (without the quotes) into this field. Then save and compile. You should see the trace statement “It works!” in the Ouput panel.

  20. ActionScript Classes package { // This is the commented version. import flash.display.MovieClip; // since both Ball and Main are MovieClips, // we have to import the MovieClip Class import flash.events.Event; // Later on, we use the Event Class, so // we need to import it here. public class Main extends MovieClip { // See how the Class definition and import statements // are inside the package curly brackets? public varball:MovieClip; // We declare "ball" up here // so that the whole Class can refer to it public function Main() { // This is the constructor function! ball = new Ball(); // Since we already declared "ball" // as an instance variable, we can just refer to it // directly without redefining it addChild(ball); // Where does ball get added? Main // is representing our Flash movie's main timeline, // so that's where the ball appears ball.speed = 5; addEventListener(Event.ENTER_FRAME, moveBall); // note: no need for the word "this", in case you // were wondering } public function moveBall(e:Event):void { ball.x += ball.speed; ball.y += ball.speed; // Again, because "ball" was defined up top, // the moveBall method can refer to it } } }

  21. Create The Class file • First, in the Flash Main menu we select New ActionScript file (.as) • The Package Keyword in the class file is a mandatory structure that ensures that our class file is known to the compiler package{ //define package import flash.display.MovieClip; //import movieclip class public class Ball extends MovieClip{ //define our class //constructor for our class public function Ball():void{ trace("Ball class created!"); //output } } } //end package/class

  22. Testing The Class file • Create a new Flash AS3 File • In the Main Timeline, write the following code: varball_mc:Ball = new Ball(); //create a new ball object addChild(ball_mc); //add to displaylist trace(ball_mc); //traces "Ball class created!" • Note! The Ball class has only one method, containing the trace-command

  23. Adding a Symbol Instance(add/attach a MovieClip to our class) • If we want to add/connect a MovieClip to our Ball class it could be done like this: 1. Create a MovieClip and give it the name ball_mc 2. Right Click on the ball_mc in Library, select Linkage (properties in CS4), type in the Class name 3. Then check the box “Export for ActionScript” 4. Now the MovieClip is connected to Our Ball class and will automatically be attached from library on startup

  24. Instance Parameters(create new objects with unique properties) • We can also create new Objects with unique properties, by sending parameters to the class constructor, part of the code looks like this: //a part of the class file (Ball.as) //constructor for the Ball class, takes 3 parameters public function Ball(xPos:Number, yPos:Number, scale:Number):void{ x = xPos; y = yPos; scaleX = scaleY = scale; } _____________________________________________ //in the main timeline (.fla file) //create a new ball object, send 3 parameters to constructor varball_mc:Ball = new Ball(200, 150, 2.5); addChild(ball_mc); //add to display list

  25. Get and Set Methods(class file) • Get and Set Methods (getters and setters) can be used to access properties or/and set new properties after an Object has been created //in class file (Ship.as), get and set methods for speed public function getSpeed():Number{ return _speed; //return speed } public function setSpeed(speed:Number):void{ _speed = speed; //set new value } _____________________________________________ //in the main timeline (.fla file) //create a new ship object with speed 200 varship:Ship = new Ship(200); ship.getSpeed(); //traces 200 ship.setSpeed(350); ship.getSpeed(); //traces 350

  26. References • Tutorial: Understanding Classes in AS3 Part 1 - http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/- Ryan Henson Creighton

More Related