0
votes

I have some code for Flash to simulate moving clouds. It worked under AS1 and now I have updated the FLA file to AS3 and minimum Flash version 10. What is wrong with this code? Can you help see what is wrong with this function?

function createLiquidFlow(target)
{
    target.counter = 1;
    target.pt = new flash.geom.Point(0, 0);
    target.mpoint = new flash.geom.Point(0, 0);
    // target.myBitmap = new flash.display.BitmapData(target._width, target._height, false, 0);
    target.myBitmap = new flash.display.BitmapData(target.width, target.height, false, 0 );
    target.myDispl = new flash.filters.DisplacementMapFilter(target.myBitmap, target.mpoint, 10, 2, 10, 15, "clamp");
    target.myList = new Array();
    target.myList.push(target.myDispl);
    target.filters = target.myList;
    target.addEventListener(Event.ENTER_FRAME,
                          function ()
                            {
                                trace("target.name = "+target.name);
                                trace("target.myBitmap = "+target.myBitmap);
                                trace("target.myBitmap.width = "+target.myBitmap.width);
                                trace("target.myBitmap.height = "+target.myBitmap.height);
                                trace("target.counter = "+target.counter);
                                var filterList = target.filters;
                                var offset = new Array();
                                offset[1] = new Object();
                                offset[1].x = target.counter;
                                offset[1].y = target.counter / 2;
                                target.myBitmap.perlinNoise(45, 6, 3, 50, true, false, 7, true, offset);
                                filterList.mapBitmap = target.myBitmap;
                                target.filters = filterList;
                                ++target.counter;
                            });
}

createLiquidFlow( movieClipLiquid )

I can trace the event listener, but the bitmap and Perlin function appears not to work. There is nothing visually happening in the output SWF. TIA

target.name = liquid74_mc
target.myBitmap = [object BitmapData]
target.myBitmap.width = 950
target.myBitmap.height = 76
target.counter = 1
myFilterList = [object DisplacementMapFilter]
BEFORE myFilterList.mapBitmap = undefined
AFTER  myFilterList.mapBitmap = [object BitmapData]
BEFORE target.filters = [object DisplacementMapFilter]
AFTER  target.filters = [object DisplacementMapFilter]
target.name = liquid74_mc
target.myBitmap = [object BitmapData]
target.myBitmap.width = 950
target.myBitmap.height = 76
target.counter = 2
myFilterList = [object DisplacementMapFilter]
BEFORE myFilterList.mapBitmap = undefined
AFTER  myFilterList.mapBitmap = [object BitmapData]
BEFORE target.filters = [object DisplacementMapFilter]
AFTER  target.filters = [object DisplacementMapFilter]
1
Are you running the swf so you can see runtime errors, like in Flash CS IDE or Flash Builder or using the debug version of Flash Player? If so, do you see any error messages? One thing is that the event handler function needs to take one argument, the event object, even if you don't use it. An object will be sent to the handler, and the function needs to match was is sent. So like .addEventListener(Event.ENTER_FRAME, function(e:Event) ... instead of what you have now. Also shouldn't you use offset[0] instead of offset[1] (arrays being zero-based)?Lars Blåsjö
I am using Flash CS55 Professional. There are no errors. I added the event handler parameter function(e:Event). I also tried the offset[0] and no change.peter_pilgrim
Thank you Lars for helping me narrow down this issue. Also I like to thank the Flep Studio guy who did this flepstudio.org/forum/tutorials/…peter_pilgrim

1 Answers

0
votes

The answer is ActionScript 3 is more stricter on types rather than ActionScript 1. The Bitmap.perlinNoise requires an offset that is an Array of Point objects. In Java this would be same as

List<Point> points = new ArrayList<Point>()

and is the same as

List<Object> points = new ArrayList<Object>()

In the ActionScript I changed the parameterised type from Object to flash.geom.Point. Also the first element of the Array was never defined. In order to liquidate the animation I kept the second offset and redefined it as a flash.geom.Point also. The working code, thus follows:

function createLiquidFlow(target)
    target.counter = 1;
    target.pt = new flash.geom.Point(0, 0);
    target.mpoint = new flash.geom.Point(0, 0);
    // target.myBitmap = new flash.display.BitmapData(target._width, target._height, false, 0);
    target.myBitmap = new flash.display.BitmapData(target.width, target.height, false, 0 );
    target.myDispl = new flash.filters.DisplacementMapFilter(target.myBitmap, target.mpoint, 10, 2, 10, 15, "clamp");
    target.myList = new Array();
    target.myList.push(target.myDispl);
    target.filters = target.myList;
    target.addEventListener(
        Event.ENTER_FRAME,
          function (e:Event)
            {
                var myFilterList = target.filters;
                var offset = new Array();
                offset[0] = new Point(0, 0);
                offset[1] = new Point(target.counter, target.counter / 2);
                target.myBitmap.perlinNoise(45, 6, 3, 50, true, false, 7, true, offset);
                myFilterList.mapBitmap = target.myBitmap;
                target.filters = myFilterList;
                ++target.counter;
            });
    // Arguments to PerlinNoise function
    // baseX:Number — Frequency to use in the x direction. For example, to generate a noise that is sized for a 64 x 128 image, pass 64 for the baseX value.
    // baseY:Number — Frequency to use in the y direction. For example, to generate a noise that is sized for a 64 x 128 image, pass 128 for the baseY value.
    // numOctaves:uint — Number of octaves or individual noise functions to combine to create this noise. Larger numbers of octaves create images with greater detail. Larger numbers of octaves also require more processing time.
    // randomSeed:int — The random seed number to use. If you keep all other parameters the same, you can generate different pseudo-random results by varying the random seed value. The Perlin noise function is a mapping function, not a true random-number generation function, so it creates the same results each time from the same random seed.
    // stitch:Boolean — A Boolean value. If the value is true, the method attempts to smooth the transition edges of the image to create seamless textures for tiling as a bitmap fill.
    //  fractalNoise:Boolean — A Boolean value. If the value is true, the method generates fractal noise; otherwise, it generates turbulence. An image with turbulence has visible discontinuities in the gradient that can make it better approximate sharper visual effects like flames and ocean waves.
    //  channelOptions:uint (default = 7) — A number that can be a combination of any of the four color channel values (BitmapDataChannel.RED, BitmapDataChannel.BLUE, BitmapDataChannel.GREEN, and BitmapDataChannel.ALPHA). You can use the logical OR operator (|) to combine channel values.
    //  grayScale:Boolean (default = false) — A Boolean value. If the value is true, a grayscale image is created by setting each of the red, green, and blue color channels to identical values. The alpha channel value is not affected if this value is set to true.
    //offsets:Array (default = null) — An array of points that correspond to x and y offsets for each octave. By manipulating the offset values you can smoothly scroll the layers of a perlinNoise image. Each point in the offset array affects a specific octave noise function.                             
}

createLiquidFlow( movieClipLiquid )