2011-09-11

FLASH CS3 - Action Script part 3

 
In Previous Flash (MX), to create event.. We can do by click the movie (example) then make script with script "onEnterFrame". In CS3, we need a little approach

To respond to the enterFrame event, you have to tell your application to listen for that event and specify which method you want to be called when the event occurs. This is done with the addEventListener method, which is defined as follows:
addEventListener(type:String, listener:Function)
example:addEventListener(Event.ENTER_FRAME, onEnterFrame);

Event.ENTER_FRAME
the first parameter tell about what event will be listen to.be careful because if you type wrong.. the script aren't working.
onEnterFrame
the second parameter tell the function name that execute related to the function. beware to use simple name and avoid Commonly used function name.



If you remember the first script AS3 I give you earlier.. this is the another script you can test
package {
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class ExampleApplication extends Sprite {
        
        public function ExampleApplication(  ) {
            graphics.lineStyle(1, 1, 1);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(event:Event):void {
            graphics.lineTo(Math.random(  ) * 300+10, Math.random(  ) * 300+10);
        }
    }
}
As you can see in here, I must called/include
  • import flash.display.Sprite;
  • import flash.events.Event;

public class ExampleApplication extends Sprite {
the class name is ExampleApplication.. is mean you should like the class filename.
public function ExampleApplication( ) {
then we build same funcion like above. This mean the script will be run for the first time.
graphics.lineStyle(1, 1, 2);
this script make you create the line, in this script we have
  • tickness 1px
  • color number 1
  • aplha 1.. it mean will show!
addEventListener(Event.ENTER_FRAME, onEnterFrame);
this script mean like I describe earlier.. the script will respont to event for ENTER_FRAME and then will execute onEnterFrame Funcion.. the function type below.
graphics.lineTo(Math.random( ) * 300+10, Math.random( ) * 300+10);
the code will type create line to the describe position above. which is Random and I can tell you where.
But after a while running the script you will understand this script make a scratch image.
Now to limit how many line can be create, you should add somehing that will stop if the code move until we think maximal! And for this problem, I should ask you to build how to.

Tidak ada komentar: