Hello, this project is created in FlashDevelop. It is a waving a flag by clicking on it,but, how I can make the flag flown directly when i execute the swf?.
Here’s the code:
package
{
import com.innerdrivestudios.visualeffect.WrappingBitmapData;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.GraphicsBitmapFill;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.filters.DisplacementMapFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
/**
* Same as previous example, but now with a good looking displacement map. We use the built in perlinNoise
* function for that. Since this is a costly operation we generate a tiny noise map and scale it.
*
* @author JC Wichman
*/
public class Example5 extends Sprite
{
//what's a flag without a sky;)!
[Embed(source = '../sky.jpg')]
private var Sky:Class;
/** A boring checker flag image */
[Embed(source = '../flag_image.jpg')]
private var CheckerFlag:Class;
/** A cool iron maiden flag image */
[Embed(source = '../flag_image2.jpg')]
private var IronMaidenFlag:Class;
//the underlying bitmapdata source for the dispMapBitmap, used to feed to the actual
//displacement map applied to the flag/lightmap composite
private var _dispMapBitmapData:BitmapData = null;
//container for onscreen dispMapBitmapData, with effect to provide lighting effect
private var _dispMapBitmap:Bitmap = null;
//displays our source flag image with a border around it as buffer for the displacement offset
private var _flagBitmap:Bitmap = null;
//the underlying pixel data for the flagimage
private var _flagBitmapData:BitmapData = null;
//the bounds of the embedded pixeldata within the flagBitmapData
//the real flag pixels are centered in the flagBitmap, so that the pixels don't fall of the screen
private var _flagBounds:Rectangle = null;
//contains composite of flag image and displacementmap-used-as-lightmap
private var _mainNode:Sprite = null;
//the perlin noise offset
private var _offset:Number = 0;
//a small bitmap used to generate the perlinnoise is, after which we scale it
//this is cheaper than generating a large perlinnoise map
private var _small:BitmapData = null;
private var _scaleMatrix:Matrix = null;
//we want the right part of the flag to be displaced more than the left, so we
//mix in a gradient from black to white, left to right
private var _gradient:BitmapData = null;
//track whether we have activated the flag yet
private var _active:Boolean = false;
//contains the lines from pole to flag
private var _lines:Sprite = null;
//switches the flag
private var _switch:Boolean = false;
private var _skyBitmap:Bitmap = null;
public function Example5():void {
_skyBitmap = new Sky();
_skyBitmap.bitmapData = new WrappingBitmapData (_skyBitmap.bitmapData);
_skyBitmap.y = -50;
addChild (_skyBitmap);
//create container for displacement data and
_dispMapBitmapData = new BitmapData (350, 250);
_dispMapBitmap = new Bitmap (_dispMapBitmapData);
//create bitmap to hold result
_flagBitmapData = new BitmapData (_dispMapBitmapData.width, _dispMapBitmapData.height, true, 0x0);
_flagBitmap = new Bitmap (_flagBitmapData);
//paint in first flag
_setupBuffer();
//create small perlinnoise holder and scale matrix, which is needed later
_small = new BitmapData (_dispMapBitmapData.width / 10, _dispMapBitmapData.height / 10, false, 0x0);
_scaleMatrix = new Matrix ();
_scaleMatrix.scale (10, 10);
//create the gradient which we combine with the perlinnoise map to create a kind of inverted falloff
//for the displacement map
_gradient = new BitmapData (_dispMapBitmapData.width, _dispMapBitmapData.height, false, 0x0);
var lGradient:Sprite = new Sprite();
var lMatrix:Matrix = new Matrix();
lMatrix.createGradientBox (_dispMapBitmapData.width, _dispMapBitmapData.height, 0, 50, 50);
lGradient.graphics.clear();
lGradient.graphics.beginGradientFill (GradientType.LINEAR, [0x202020,0xffff30], [1,1],[0, 255], lMatrix, "pad");
lGradient.graphics.drawRect (0, 0, _dispMapBitmapData.width, _dispMapBitmapData.height);
lGradient.graphics.endFill();
_gradient.draw (lGradient);
//setup the composite of the flag and displacement map
_mainNode = new Sprite ();
_mainNode.addChild (_flagBitmap);
_mainNode.addChild (_dispMapBitmap);
//move map to align with flag image painted in flagbitmap
//the +2 is not necessary, it happens to create a funny egde
_dispMapBitmap.x = _flagBounds.x+1;
_dispMapBitmap.y = _flagBounds.y+1;
_dispMapBitmap.scrollRect = _flagBounds;
//you can play a lot with the blendmodes and color transforms to imitate different lighting effect
_dispMapBitmap.blendMode = BlendMode.MULTIPLY;
_dispMapBitmap.transform.colorTransform = new ColorTransform (5, 5, 5, 1, 140, 140, 140, 0);
addChild (_mainNode);
//setup the disp map on the main node
_mainNode.filters = [
new DisplacementMapFilter (_dispMapBitmap.bitmapData, new Point (0,0), BitmapDataChannel.RED, BitmapDataChannel.GREEN, 45, 45, "ignore", 0, 0)
];
//paint an ugly flagpole
var lFlagpole:Sprite = new Sprite ();
lFlagpole.graphics.clear();
lMatrix.createGradientBox (20, 0, 0, 10, 20);
lFlagpole.graphics.beginGradientFill (GradientType.LINEAR, [0x505050, 0xd0d0d0, 0x606060, 0x808080, 0x303030], [1,1,1,1,1],[0, 128,180,200,255], lMatrix, "pad");
lFlagpole.graphics.drawRect (10, 30, 20, 400);
lFlagpole.graphics.endFill();
addChild (lFlagpole);
_lines = new Sprite();
addChild (_lines);
[COLOR=Red]addEventListener (Event.ENTER_FRAME, _onEnterFrame);
addEventListener (MouseEvent.CLICK, _onMouseClick);
_render();
}
private function _onMouseClick(e:MouseEvent):void
{
if (!_active) {
_active = true;
} else {
//_setupBuffer();
}
}
private function _onEnterFrame (pEvent:Event):void {
if (!_active) return;
_render();
}[/COLOR]
/**
* Repaints buffer with a different image.
*/
private function _setupBuffer():void {
var lData:BitmapData = null;
if (_switch) {
lData = Bitmap(new CheckerFlag()).bitmapData;
} else {
lData = Bitmap(new IronMaidenFlag()).bitmapData;
}
_flagBounds = new Rectangle (
(_flagBitmapData.width - lData.width) / 2,
(_flagBitmapData.height - lData.height) / 2,
lData.width,
lData.height
);
//copy it directly for a straight flag
_flagBitmapData.copyPixels (lData, lData.rect, new Point (_flagBounds.x, _flagBounds.y));
_switch = !_switch;
}
private function _render():void {
//generate the noise
_small.perlinNoise (10, 10, 1, 1, false, false, 7, true, [new Point(-_offset, _offset/5)]);
_offset += 1;
//redraw the noise scaled up (smooth it or it will look awful)
_dispMapBitmapData.lock();
_dispMapBitmapData.draw (_small, _scaleMatrix, null, null, null, true);
_dispMapBitmapData.draw (_gradient, null, null, "multiply");
_dispMapBitmapData.unlock();
_mainNode.x = 5*Math.sin (_offset/7);
_mainNode.y = 5*Math.sin (_offset / 5);
_lines.graphics.clear();
_lines.graphics.lineStyle (2, 0, 0.5);
_lines.graphics.moveTo (30, 40);
_lines.graphics.curveTo (_mainNode.x+41, _mainNode.y+49, _mainNode.x+50, _mainNode.y+50);
_lines.graphics.moveTo (30, 260);
_lines.graphics.curveTo (_mainNode.x + 51, _mainNode.y + 246, _mainNode.x + 50, _mainNode.y + 245);
_skyBitmap.bitmapData.scroll ( -1, 0);
}
}
}
I think that i have to modify this:
[COLOR=Red]addEventListener (Event.ENTER_FRAME, _onEnterFrame);
addEventListener (MouseEvent.CLICK, _onMouseClick);
_render();
}
private function _onMouseClick(e:MouseEvent):void
{
if (!_active) {
_active = true;
} else {
//_setupBuffer();
}
}
private function _onEnterFrame (pEvent:Event):void {
if (!_active) return;
_render();
}[/COLOR]
I want to waving the flag without clicking on it,
what is the correct as3 code to do it, please?
Regards