2006/04/20 | Delegate类巧妙的用法~~
类别(网摘) | 评论(0) | 阅读(98) | 发表于 07:00

摘自:http://www.darronschall.com/weblog/archives/000126.cfm

 // 1) create a blank fla, drag a v2 button to the
// stage then delete the button instance so that
// the button only resides in the library.
// 2) paste this code on frame 1 and test movie

import mx.utils.Delegate;
import mx.controls.Button;

function init() {
// create and place our buttons
createClassObject(Button, "btn1", 1, {label:"plus 1"});
createClassObject(Button, "btn2", 2, {label:"plus 5", _y: btn1._y+btn1.height});

// both buttons use the same onIncrement event
// handler - but the handler behaves differently
// based on the step value set
var f = Delegate.create(this, onIncrement);
f.step = 1;  // btn1 steps up by 1
btn1.addEventListener("click", f);
f = Delegate.create(this, onIncrement);
f.step = 5;  // btn2 setps up by 5
btn2.addEventListener("click", f);

// initialize our running total
total = 0;
}

// generic increment function used as
// event handler for the buttons
function onIncrement() {
// grab the step property from the
// delegate function we created, and use
// that as the increment value

total += arguments.caller.step;
trace(total);
}

init();

0

评论Comments

日志分类
首页[28]
flash[3]
javascript[3]
C#[1]
feeling[5]
网摘[16]