Thursday, August 19, 2010

Programming fundamentals - day 3

Events :  This section is very inportant as it the basis of user interactivity. event is an instantenous point which determines a change. so we need to check these events and increase the intreactivity by adding more functionality.

  • The event source: Which object is the one the event is going to happen to? For example, which button was clicked, or which Loader object is loading the image? The event source is also known as the event target. It has this name because it’s the object where the computer targets the event (that is, where the event actually happens).
  • The event: What is the thing that is going to happen, the thing that you want to respond to? The specific event is important to identify, because many objects trigger several events.
  • The response: What steps do you want performed when the event happens?

lets build an anology for Events.

Now look how the funciton is called.




English: getting close to language

function eventResponse(event:MouseEvent):void
{
 // Actions performed in response to the event go here.
}

myButton.addEventListener(MouseEvent.CLICK, eventResponse);
  
let's read.
“I want to do open file  on when I click the button ,”
The above line is very simple.

Lets convert this in code .

Start tracing the event. Here its “mouse click”
Start tracing where the even will be taking place on . Here its “button”


         button : where click will take place
         mouse : which will produce click
        
On button I  when it gets clicked I want to open file.
mybutton.addEventlistner(MouseEvent.CLICK, fileopen);
fileopen(event:MouseEvent):void{  // action that you need to perform}


Now you want to do something on a click, but you need to know that  CLICk is associated with what class. Here its MouseEvent, So  it becomes easy , to write MouseEvent.CLICK, to  we need one listener who will listen your mouse clicking , so we add action Listener. Lets talk about the eventSource,  clicking and listening must be  at a source , here its button, but it can be any object, so we call it source object
Each event is affiliated with a specific class.
Let continue with the same example. Were we need to my button to respond to mouse click and perform certain action, we add or attach addActionListern to the button by writing ,
Mybutton.addEventLisner();  at this stage the information is abstract , its more generic like , we have added the eventListner But what to listen, and also tell him that , event is mouse click which it need to listen and what to do but letting him about the function which needs to be executed, by passing parameters , so we write  now
myButton.addEventListener(MouseEvent.CLICK, eventResponse);


An object is created that’s an instance of the class associated with the event in question (MouseEvent in this example). For many events, this object is an instance of the Event class. For mouse events, it is a MouseEvent instance. For other events, it is an instance of the class that’s associated with that event. This object that’s created is known as the event object, and it contains specific information about the event that happened: what type of event it is, where it happened, and other event-specific information if applicable.



this.stop(); 
 
function playMovie(event:MouseEvent):void 
{ 
    this.play(); 
} 
 
playButton.addEventListener(MouseEvent.CLICK, playMovie);

my button object source maintains the list of all the listeners,


No comments:

Post a Comment