//先是定义一个画矩形函数 import flash.display.BitmapData; import flash.geom.*; function createRectangle(x:Number, y:Number, w:Number, h:Number, mc:MovieClip) { mc.moveTo(x, y); mc.lineTo(x+w, y); mc.lineTo(x+w, y+h); mc.lineTo(x, y+h); mc.lineTo(x, y); mc.endFill(); } //然后对canvas的每个mc写onRelease响应 for (i in canvas) { if (typeof (canvas[i]) == "movieclip") { if (canvas[i].type != "rect") { canvas[i].onRelease = function() { //如果已经被上色了,就会有一个与之绑定的_mc,再次上色之前,要把这个link_mc删掉 if (this.link_mc._name) { this.link_mc._bmp.dispose(); this.link_mc.removeMovieClip(); } //根据_mc的位置和大小,画一个匹配的矩形,然后进行类型和链接的设置 var rect_mc:MovieClip = canvas.createEmptyMovieClip("rect"+canvas.getNextHighestDepth(), canvas.getNextHighestDepth()); rect_mc.type = "rect"; this.link_mc = rect_mc; rect_mc.bmp = new BitmapData(20, 20, false, 0xFF0000); rect_mc.bmp.draw(select_color_mc); rect_mc.beginBitmapFill(rect_mc.bmp); var r:Object = this.getBounds(canvas); createRectangle(r.xMin, r.yMin, r.xMax-r.xMin, r.yMax-r.yMin, rect_mc); //最后做一个mask,这样就达到了上色的效果 rect_mc.setMask(this); }; } } } //对颜色拾取器进行onRelease设定 for (i in colors) { if (typeof (colors[i]) == "movieclip") { colors[i].onRelease = function() { select_color_mc=this }; } }
|