110 likes | 228 Views
Event Handling in flash. Event: is an instantaneous occurrence usually triggered by user. هو احداث تقع فورا وتطلق عادة باستخدام المستخدم Ex) clicking a button, moving mouse, clicking on a mouse
E N D
Event Handling in flash • Event: is an instantaneous occurrence usually triggered by user.هو احداث تقع فورا وتطلق عادة باستخدامالمستخدم • Ex) clicking a button, moving mouse, clicking on a mouse • Event Handling: writing action script that will be executed when the event occurs.هو كتابة الاكشن الذي سينفذ عند وقوع الحدث • Events can be handled by using Callbackيمكن التعامل مع الحدث من خلال استدعاءه • Callback: function tied directly to a particular event.اقتران مربوط مباشرة مع حدث معين
Event Handling in flash • Ex) onEnterFrame = function() { …. } • Event handling examples: • Handling button events: • onPress: the event is triggered when a button is pressed.الحدث سيطلق عندما يكون الزر مضغوط • ex) btn.onPress = function() { …. } Code written inside function is tied to the event (onEnterFrame) الكود المكتوب داخل الاقتران مربوط مع الحدث • Code written here will be executed when (btn) is pressed • This callback function is written in the first frame btn: is the instance name of a button
Event Handling in flash • onRelease: The event is triggered when a button is released after being pressed.الحدث سيطلق عند بعد الضغط (أي بعد مانضغط على الزر • ex) btn.onRelease = function() {…} • onRollOver: The event is triggered when the mouse cursor moves over a button. الحدث سيطلق عند ما يكون مؤشر الماوس يتحرك فوق الزر • Ex) btn.onRollOver = function() {…} • Handling mouse events: • onMouseDown: it is triggered whenever the mouse button is pressed anywhere in the stage ex) onMouseDown = function() {}الحدث سيطلق عندما يضغط زرالماوس في أي مكان على المسرح
Event Handling in flash • onMouseUp: it is triggered whenever the mouse button is released after being pressed ex) onMouseUp=function() { … } • onMouseMove: it is triggered whenever the mouse cursor moves over a componentالحدث سيطلق كلما تحرك مؤشر الماوس فوق عنصر ex) onMouseOver=function() { … }
Mouse Events Application • Ex) This example shows how to drag a movie clip symbol from place to another using mouse events. • Create a new flash document • Insert a movie clip symbol onto the stage • Give it an instance name (m_shape) • In the action panel of the first frame: • Declare a number variable (i) and initialize it to zero • Add a (onMouseDown) handler that assigns one to (i)
Mouse Events Application • Add a (onMouseMove) handler and insert the following code into it: if(i==1) { m_shape._x=_xmouse; m_shape._y=_ymouse; } • Add a (onMouseUp) handler that assigns zero to (i) • Test the Program To move the movie clip symbol with the mouse cursor To check whether the mouse button is being pressed
Handling Key Events • Key events are handled byالاحداث التي تتناولها المفاتيح(عاى الكيبورد) • Creating an object to listen to key eventsانشاء كائن للاستماع إلى الأحداث • Associate the object with the key event typesربط كائن مع نوع من الاحداث • Register the object to start listening to key eventsتسجل الكائن للبدء في الاستماع للحدث • Event types triggered when pressing keyboard keys:انواع الاحداث التي تطلق عند الضغط مفاتيح الكيبورد • onKeyDown: it is triggered whenever a keyboard button is clicked downالحدث سيطلق(يبقى شغال)طالما زر الكيبورد كان مضغوطا للاسفل • onKeyUp: it is triggered whenever a keyboard button is released الحدث سيطلق(يبقى شغال)طالما زر الكيبورد كان مضغوطا للاعلى(أي غير مضغوط)
Handling Key Events • Ex) This example receives events from keyboard and prints the code of the pressed key: • Create a new flash document • In action panel of the first frame, type the following code:
Handling Key Events • Some important functions found in class key: • Key.getAscii(): returns the ASCII code of the pressed alphanumeric character (s,r,5,3,….)يرجع الاسكي كود للمفاتيح (الارقام او الاحرف) التي تضغط • Key.getCode(): returns the code of the pressed non-alphanumeric keys (shift, ctrl, tab…) يرجع الاسكي كود للمفاتيح (غير ارقام والاحرف مثل سباس وشفت وكنترول واسهم الاتجاهات) التي تضغط
Handling Key Events var i:Object = new Object(); i.onKeyDown = function() { trace(Key.getCode()); } Key.addListener(i); Creating an object to listen to key events Associate the object with the key event types This code will be executed when pressing on any keyboard key Register the object to start listening to key events
Excersice • Write a flash application that moves a movie clip symbol using Keyboard.