正如其他人所评论的那样,您可以分别使用readGraphicsData和drawGraphicsData。它甚至不会过于复杂。只需循环遍历所需形状的IGraphicsData,一旦找到GraphicsBitmapFill类的实例就停止,使用另一个BitmapFill创建一个新实例,最后将更改应用于原始形状。
嗯,一张图片胜过千言万语。 这是一个例子。这可能看起来有点长,但是只需要很多代码来准备形状并加载图像以用作位图填充。
把目光投向 的 处理() 强> 功能。
package { import flash.display.Sprite; import flash.display.Shape; import flash.display.Graphics; import flash.display.Loader import flash.display.LoaderInfo; import flash.display.IGraphicsData; import flash.display.GraphicsBitmapFill; import flash.display.Bitmap; import flash.display.BitmapData; import flash.events.Event; import flash.net.URLRequest; import flash.utils.getQualifiedClassName; public class Main extends Sprite { private var bitmapData1:BitmapData; private var bitmapData2:BitmapData; private var masterSprite:Sprite = new Sprite(); private var texturesLoaded:int = 0; private var loader:Loader = new Loader(); public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); loader.load(new URLRequest("textureA.jpg")); addChild(masterSprite); masterSprite.x = masterSprite.y = 200; } private function onComplete(event:Event):void { switch (texturesLoaded) { case 0: bitmapData1 = Bitmap(LoaderInfo(event.target).content).bitmapData; loader.load(new URLRequest("textureB.jpg")); break; case 1: bitmapData2 = Bitmap(LoaderInfo(event.target).content).bitmapData; loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete); drawStar(); process(); } texturesLoaded++; } private function process():void { var tempShape:Shape = Shape(masterSprite.getChildAt(0)); var graphicsData:Vector.<IGraphicsData> = tempShape.graphics.readGraphicsData(); for (var a:int = 0; a < graphicsData.length; a++) { if (getQualifiedClassName(graphicsData[a]) == "flash.display::GraphicsBitmapFill") { var bitmapFill:GraphicsBitmapFill = new GraphicsBitmapFill(bitmapData2); graphicsData[a] = bitmapFill; break; } } tempShape.graphics.drawGraphicsData(graphicsData); } private function drawStar():void { var angles:Array = new Array(0, 36, 72, 108, 144, 180, 216, 252, 288, 324, 360); var innerRadius:int = 40; var outerRadius:int = 80; var shape:Shape = new Shape(); shape.graphics.beginBitmapFill(bitmapData1); shape.graphics.moveTo(0 + Math.cos(angles[a] * (Math.PI / 180)) * outerRadius, 0 + Math.sin(angles[a] * (Math.PI / 180)) * outerRadius); for (var a:int = 0; a < angles.length; a++) { angles[a] -= 90; if (a % 2 == 0) { shape.graphics.lineTo(0 + Math.cos(angles[a] * (Math.PI / 180)) * outerRadius, 0 + Math.sin(angles[a] * (Math.PI / 180)) * outerRadius); } else { shape.graphics.lineTo(0 + Math.cos(angles[a] * (Math.PI / 180)) * innerRadius, 0 + Math.sin(angles[a] * (Math.PI / 180)) * innerRadius); } } shape.graphics.endFill(); masterSprite.addChild(shape); } } }