Good afternoon! I'm using the Starling engine and I'm trying to rasterize a regular flash movieclip into a starling bitmap clip. I decided to write a small library myself, because the only real (convenient and fast) working similar engine is GAFMedia Converter, but it does not always allow you to log in and convert graphics.
wrote a simple class in which I rasterize the clip every frame and every object:
package ru.animation {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.StageQuality;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.Transform;
import flash.utils.ByteArray;
import flash.utils.setTimeout;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
public class Clip extends Sprite {
public var _objects:Array = [];
public var _objectsNames:Array = [];
public var _screen:Array = [];
private var _currentFrame:int = 1;
public var MainClip:MovieClip;
public function Clip() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
gotoframe();
}
private function gotoframe():void {
while (numChildren > 0) {
removeChildAt(0);
}
// trace("currentFrame " + _currentFrame);
// for(var j:int = 0;j<_screen.length-1;j++)
for each(var scene: Object in _screen[_currentFrame]) {
var clipName:String = scene.name;
var id:int = _objectsNames.indexOf(clipName);
var clip:Image = _objects[id];
clip.x = scene.x;
clip.y = scene.y;
// var t:Matrix = (scene.transform is Matrix) ? scene.transform : scene.transform.concatenatedMatrix;
// clip.transformationMatrix = t;
// clip.pivotX = scene.x;
// clip.pivotY = scene.y;
// clip.rotation = scene.rotation;
// clip.scaleX = scene.scaleX;
// clip.scaleY = scene.scaleY;
clip.width = scene.width;
clip.height = scene.height;
// clip.transform = scene.transform;
// clip.scaleX=.5;
// clip.scaleY=.5;
addChild(clip);
}
_currentFrame++;
if (_currentFrame > _screen.length) {
_currentFrame = 1;
}
setTimeout(gotoframe, 50);
}
public function buildCacheFromMovieClip(object:MovieClip, name:String = ""):void {
this.name = name;
if (object != null) {
MainClip = object;
for (var i:int = 1; i <= MainClip.totalFrames; i++) {
_screen[i] = [];
MainClip.gotoAndStop(i);
var r:Rectangle;
for (var j:int = 0; j < MainClip.numChildren; j++) {
var clip:flash.display.DisplayObject = MainClip.getChildAt(j);
if (_objectsNames.indexOf(clip.name) == -1) {
r = clip.getBounds(clip);
if (r.width > 0 || r.height > 0) {
var test:JPEGEncoder = new JPEGEncoder(1);
_objectsNames.push(clip.name);
var bitmapData:BitmapData = new BitmapData(r.width, r.height, true, 0x000000);
var m:Matrix = new Matrix();
m.translate(-r.x, -r.y);
bitmapData.drawWithQuality(clip, m, null, null, null, true,StageQuality.BEST);
var data:ByteArray = test.encode(bitmapData);
var loader:Loader = new Loader();
loader.loadBytes(data);
bitmapData.draw(loader);
var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.transform = clip.transform;
//bitmap.rotation = rad2deg(clip.rotation);
var t:Image = Image.fromBitmap(bitmap);
// t.rotation =rad2deg(clip.rotation);
t.x = r.x+clip.x+MainClip.x;
t.y = r.y+clip.y+MainClip.y;
t.scaleX /= MainClip.scaleX;
t.scaleY /= MainClip.scaleY;
_objects.push(t);
var tr:Transform = new Transform(clip);
var m:Matrix = tr.concatenatedMatrix;
_screen[i].push({
transform: m,
rotation: clip.rotation,
name: clip.name,
x: r.x,
y: r.y,
scaleX: clip.scaleX,
scaleY: clip.scaleY,
width: clip.width,
height: clip.height
});
}
}
{
_screen[i].push({
transform: new Transform(clip),
rotation: clip.rotation,
name: clip.name,
x: clip.x,
y: clip.y,
scaleX: clip.scaleX,
scaleY: clip.scaleY,
width: clip.width,
height: clip.height
});
}
}
}
}
}
public function getClone():Clip {
var clip:Clip = new Clip();
clip.MainClip = MainClip;
clip.buildCacheFromMovieClip(MainClip,name)
return clip;
}
override public function get height():Number {
return MainClip.height;
}
override public function get width():Number {
return MainClip.width;
}
public function getChild(name:String):Image {
var index:int = _objectsNames.indexOf(name);
return _objects[index];
}
}
}
but as a result of using this class, the object is animated with curved internal animation of child objects.
Source (project) for viewing: https://drive.google.com/file/d/1ptN4VSS7py4qUGOrgHQ4tshhMgExuCGq/view?usp=sharing
Please help with setting coordinates for child objects inside the object: advice, code example or project, I found a similar work - https://forum.starling-framework.org/d/2179-flash-movieclip-converter-preserves-display-list-hierarchy/104
but it has problems...
thanks for the help