Friday, June 6, 2014

Simple Sprite

alright i am going to show you how to make a simple sprite using action script 3
, sprites are a form of movie clip that do not have frames in them, and because of that they take up less space, there will be times when you need to use sprites instead of a movie clip, so here are the steps you need to do to create a sprite with as3:

Making a sprite

  1. first open up your text editor (notepad or whatever)
  2. write the basic structure of an as 3 class, which is : 
  3. 1
    2
    3
    4
    5
    6
     package{  
     public class yourclass extends MovieClip{  
     public function yourclass():void{  
     }  
     }  
     }  
    
  4. the code above is very simple, you are making a class called "yourclass"( you can change this, just save it as the new class name.as), and that class extends a movieclip, the public means that it is an open class, all properties (variables, etc) in it can be accessed by other classes, the code underneath it declares a function called yourclass (again change it to whatever you did on the above code), everything in it will be executed when the class is loaded, the "()" is empty, we do not need to fill it, yet, it is for a parameter (variable that gets passed to a function to be processed), and the :void means that this class will not return any value.
  5. after that you need to declare a sprite variable with this code, put it after class declaration
  6. 1
     public var spriteName:Sprite = new Sprite();  
    
  7. spriteName is an instance name, you can change it, just remember, you will need the instance name to call that variable. this code just declared a new sprite. seems simple enough, now lets add an object into it:
  8. add this code after function declaration:
  9. 1
    2
    3
     spriteName.graphics.beginFill(0xff0000);  
     spriteName.graphics.drawCircle(0, 0, 10);  
     spriteName.graphics.endFill();  
    
  10. the first line uses a hexadecimal value of red, wow, thats a big word, anyway the "0x"does not do anything (at least i think), the 6 digits after declares the colour, the first 2 digits after 0x is the red value, the 2 digits after is for green, and the last 2 is for blue, thus RGB, feel free to experiment with the value, "ff" is the highest while "00" is the lowest.
  11. the next line of code calls a function in the graphics class with 3 arguments, we are sending it variables, the first 2 is for the position, 0 and 0 is the x and y coordinates which is at the top right of the stage, the third argument is the radius of the circle in pixels, the size of it.
  12. finally the last line ends the fill, hence endFill();
  13. try to compile your class, oh... something's wrong, it did not appear, what could have gone wrong??? well you never did add the sprite to the page so just add this line of code under the previous lines within the function:
  14. 1
     addChild (spriteName);  
    
  15. remember change the "spriteName" to the instance of the variable in the var declaration...
  16. now try compiling the class again, and it should work fine, if it didn't it must be CRAZY!!! just kidding, you must've done something wrong, or i did... 
Anyway that is it for this tutorial hope this helps you, this was  especially intended for my friend berlian, because he just started flash and i wanted to help him so yeah...

No comments:

Post a Comment