כתבה זו היא המשך לכתבה קודמת בנושא אירועים (events) ומאזינים (listeners)
בכתבה הקודמת נתתי דוגמא שמיישמת את השלבים בכתיבת קוד שמאזין לאירוע ומבצע פעולה כתגובה להתרחשותו. הדוגמא הציגה דג ששוחה אחרי סמן העכבר.
למעשה ראינו את הדג ואת הסמן יחד, על הבמה.
אני רוצה להראות לכם כאן באמצעות הדג, איך ניתן להחליף את סמן העכבר בכל movieclip שתעצבו בעצמכם, או תבחרו להשתמש.
פתחו את ה-FLA שיצרנו בפעם הקודמת (ניתן להוריד אותו בכתבה הקודמת), בחרו בפריים 1 ולחצו F9 כדי לפתוח את חלון עריכת הקוד.
זה מה שאתם אמורים לראות (הוספתי הערות הסבר):
import flash.events.*; //importing library of utilities for events listening
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener); //declare which event to listen to
//this chain of events will happen every time the mouse moves
function mouseMoveListener (event:MouseEvent):void
{
fish.x=mouseX; //place the fish movie clip on x-axis just where the mouse pointer is
fish.y=mouseY; //place the fish movie clip on y-axis just where the mouse pointer is
}
עכשיו, כדי "להחליף" את הסמן ב-movieclip של הדג, פשוט נעלים את הסמן. הוסיפו את שורת הקוד הבאה אחרי השורה השניה בקוד:
Mouse.hide(); //hide the mouse pointer
עכשיו הקוד אמור להיראות כך:
import flash.events.*; //importing library of utilities for events listening
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener); //declare which event to listen to
Mouse.hide; //hide the mouse pointer
//this chain of events will happen every time the mouse moves
function mouseMoveListener (event:MouseEvent):void
{
fish.x=mouseX; //place the fish movie clip on x-axis just where the mouse pointer is
fish.y=mouseY; //place the fish movie clip on y-axis just where the mouse pointer is
}
עכשיו הדג כבר לא שוחה אחרי הסמן, אלא הוא בעצמו הפך לסמן!
את קובץ המקור ניתן להוריד מהשרת של קודאקטיב.
תגובות
הוסף רשומת תגובה