Action Script

Action Script 2 การใช้ Mouse Wheel กับเนื้อหา

มีหลายคนที่มีปัญหาการเริ่มเขียน Action Script 2 กับ Falsh Animation กับเรื่องของ content ที่ต้องดึงจาก XML และ ถ้ามันยาวต้องทำ Scroll Bar ให้มันเลื่อน หนำซ้ำยังต้องมาทำให้การใช้งาน Movie ของเราง่ายขึ้นด้วยการ Scroll Mouse Wheel บนเนื้อหาได้อีก วิธีนี้จะเป็นการตอบโจทย์คำถามและการแก้ปัญหาเบื้องต้น

Flash Action Script

 

เปิด Adobe Flash CS3 หรือใครมี Macromedia Flash 8 ก็เปิดขึ้นมาเลยครับ พิจารณา Code ตามนี้

 


import mx.transitions.Tween;
import mx.transitions.easing.*;
 
track_mc._visible = false;
drag_mc._visible = false;
 
mouseListener = new Object();
 
function scrollInfo() {
// If scrolling is not needed, hide scrolling assets
track_mc._visible = false;
drag_mc._visible = false;
// Find height of the target_mc
var clipheight:Number = target_mc._height;
// Find y position of target_mc
var starty:Number = 0;
target_mc._y = 0;
// Find height of mask over target_mc
var maskheight:Number = mask_mc._height;
// Limit scrolling
var maxscroll:Number = maskheight-clipheight+starty;
// Start dragging position
var startposition:Number = drag_mc._y=track_mc._y;
// Find y position of scrollbar face
var startdragT:Number = drag_mc._y;
// Limit var for scrollbar face
var startdragB:Number = track_mc._height-drag_mc._height+track_mc._y;
// Calculation for scrolling clip
var diff:Number = (clipheight-maskheight)/(track_mc._height-drag_mc._height);
// Var for activating the MouseWheel scrolling
var mScroll:Boolean = false;
// Disable all this if scrolling isn't necessary
if (target_mc._height<mask_mc._height) {
track_mc._visible = false;
drag_mc._visible = false;
mScroll = false;
mouseWheelScroll();
} else {
track_mc._visible = true;
drag_mc._visible = true;
mScroll = true;
mouseWheelScroll();
}
// drag_mc on press
drag_mc.onRollOver = function() {
this.gotoAndPlay("start");
};
drag_mc.onRollOut = function() {
this.gotoAndPlay("end");
};
drag_mc.onPress = function() {
// Start dragging, limit the dragging to parameters
startDrag(this, false, this._x, track_mc._y, this._x,
(track_mc._height+track_mc._y)-this._height);
// Update scrolling clip in conjunction with the y position of the drag_mc
this.onMouseMove = function() {
difference = Math.abs(startposition-this._y);
target_mc._y = -Math.round(difference*diff+starty);
};
};
// On release, kill function
drag_mc.onRelease = function () {
stopDrag();
delete this.onMouseMove;
};
drag_mc.onReleaseOutside = function () {
stopDrag();
this.gotoAndPlay("end");
delete this.onMouseMove;
};
// Mouse Wheel Scrolling
function mouseWheelScroll() {
mouseListener.onMouseWheel = function(delta) {
// Only do it if the mouse is hovering over the text area
if (_xmouse > mask_mc._x && _xmouse < mask_mc._x+mask_mc._width
&& _ymouse > mask_mc._y && _ymouse < mask_mc._y+mask_mc._height) {
if (delta<0 && mScroll == true) {
if (target_mc._y>maxscroll+20) {
target_mc._y += delta*10;
} else {
target_mc._y = maxscroll;
}
}
if (delta>0 && mScroll == true) {
if (target_mc._y<starty-20) {
target_mc._y += delta*10;
} else {
target_mc._y = starty;
}
}
drag_mc.gotoAndPlay("end");
}
// Move the scroll button to correspond with the position of the text (this was the hard part)
difference = Math.abs(startposition-target_mc._y);
var mT:Tween = new Tween(drag_mc, "_y",
Regular.easeOut, drag_mc._y, difference/diff+starty, 0.2, true);
};
Mouse.addListener(mouseListener);
}
}

ที่มาของบทความและตัวอย่างโปรแกรม คือ
http://www.bensonarts.com/flash/freebie-flash-mousewheel-scrolling-as2xml/

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Leave a Reply

Back to top button

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน