An Actionscript 3 Flag and compatible with Flash Player 9+. Using a bitmap for the flag from the library this creates perlin noise and applies it as a displacement map to create an simple flag wave. I then add the perlin noise BitmapData on top for highlights using BlendMode.HARDLIGHT. Quite a lot can be achieved with this. Download the source fla here (it’s timeline code) – Download zip Tags: AS3 Flag,
Here’s the code for it too, though it’s probably messy …
import flash.display.*;
//
var flagWidth:Number = 320;
var flagHeight:Number = 240;
//
var _bmpd:BitmapData=new BitmapData(flagWidth,flagHeight,false,0xff0000);
var _bmp:Bitmap=new Bitmap(_bmpd);
var num1:Number=1;
var offset0:Point= new Point(10,10);
var offset1:Point = new Point(10,10);
var offset2:Object = new Point(10,10);
var rippleSize:Number=150;
//
stage.addEventListener(Event.ENTER_FRAME, loop);
var _tempFlag:BitmapData=new flags_sprite(18,10);
var _tflag2:BitmapData=new BitmapData(_tempFlag.width,_tempFlag.height,false,0×000055);
var flagRect:Rectangle=new Rectangle(0,0,_tempFlag.width,_tempFlag.height);
_tflag2.copyPixels(_tempFlag,flagRect,new Point());
//
flagRect=new Rectangle(0,0,_tempFlag.width,_tempFlag.height);
var _flagBitmapData:BitmapData=new BitmapData(flagWidth,flagHeight,false,0×00FF00);
//
var matrix:Matrix = new Matrix();
var scaleVal:Number=flagWidth/flagRect.width;
var scaleVal2:Number=flagHeight/flagRect.height;
matrix.scale(scaleVal,scaleVal2);
flagRect.width*=scaleVal;
flagRect.height*=scaleVal2;
_flagBitmapData.draw(_tflag2,matrix,null,null,flagRect,true);
//
var flag:Bitmap=new Bitmap(_flagBitmapData);
var mapPoint:Point=new Point(0,0);
var channels:uint=BitmapDataChannel.RED;
var componentX:uint=channels;
var componentY:uint=channels;
var scalex:Number=50;
var scaley:Number=100;
var dmode:String=DisplacementMapFilterMode.COLOR;
var color:uint=0;
var dalpha:Number=0;
I needed to access a public static var (called ‘description’ in this case), from a class that hasn’t been instantiated. The description var is simply used to fill a text box.
I had several classes all name Test_1, Test_2 etc, All I had was it’s ID number. Something to note though is each class must be referenced in the class so the compiler knows to include it, I did this by storing them in an array.
Main class
package {
import com.*;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
public class ClassTest extends Sprite {
public function ClassTest() {
var _num:int = 1
var className:String = “com.Test_” + _num
trace(getDefinitionByName(className).description)
}
private var classArray:Array = [Test_1, Test_2]
}
}
package {
import com.*;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
public class ClassTest extends Sprite {
public function ClassTest() {
var _num:int = 1
var className:String = “com.Test_” + _num
trace(getDefinitionByName(className).description)
}
private var classArray:Array = [Test_1, Test_2]
}
}
In the com folder we have two classes …
Test_1
package com {
public class Test_1 extends TestBase {
public function Test_1() {
}
public static var description:String = “DESCRIPTION #1″
}
}
& Test_2
package com {
public class Test_2 extends TestBase {
public function Test_2() {
}
public static var description:String = “DESCRIPTION #2″
This bit of code came about as I needed a way to make enemies get more progressively more difficult as the player played through levels. I didn’t want the enemies getting difficult at a steady rate, but rather to have a ebb and flow of difficulty, this was the result. X represents the level, and the y represents the difficuly, displayed here as pixels on a bitmap. It’s pretty straightforward and the values can be tweaked to provide a range of results. Leave a comment if you don’t understand!
var scaleFactor:Number = 1.5;
var _bmpd:BitmapData = new BitmapData(40,40,false,0xff999999);
var _bmp:Bitmap = new Bitmap(_bmpd);
var ypos:Number = 0;
var _rotAmount:Number = 90;
for (var i:int=0; i<40; i++) {
var _rad:Number =( _rotAmount/180)*Math.PI;
var yFactor:Number = ( 1+(i *.01) );
var scaling:Number = i + Math.cos(_rad) * yFactor;
_bmpd.setPixel32 (i,scaling,0xFF0000);
scaleFactor *= 1.01;
_rotAmount+=30;
}
addChild (_bmp);
_bmp.scaleX=_bmp.scaleY=5;
This is my first go at setting up a reusable camera class so it may need a bit of tweaking when used in a real situation.
Setup a camera, giving it x,y, render window width and height, zoom, a maximum zoom value, and constraints. The camera will then keep the bitmap edges within the render window frame regardless of the zoom (it works out the furthest you can zoom out based on the constraints you give it, but you can make you maximum zoom whatever you like.
This camera class needs to work with a rendering system to support it, which I have in the main class.
I have used my ‘landscape generator‘ code to make the map for this, but it’s very easlily adapted to using a library bitmap.
A top down terrain / landscape generator using perlinNoise and paletteMap to create the effect. The gradient that applies the colours can easily be adjusted.
CLICK TO GENERATE NEW IMAGES.
var MENU_ITEM_0:String=”www.terrypaton.com”;
var MENU_ITEM_1:String=”Made by Terry!”;
var MENU_ITEM_2:String=”LOW Quality”;
var MENU_ITEM_3:String=”HIGH Quality”;
var myContextMenu:ContextMenu;
myContextMenu = new ContextMenu();
addCustomMenuItems ();
function addCustomMenuItems ():void {
myContextMenu.hideBuiltInItems ();
var defaultItems:ContextMenuBuiltInItems=myContextMenu.builtInItems;
defaultItems.print = false;
var item0:ContextMenuItem=new ContextMenuItem(MENU_ITEM_0);
myContextMenu.customItems.push (item0);
var item1:ContextMenuItem=new ContextMenuItem(MENU_ITEM_1);
myContextMenu.customItems.push (item1);
var item2:ContextMenuItem=new ContextMenuItem(MENU_ITEM_2);
myContextMenu.customItems.push (item2);
var item3:ContextMenuItem=new ContextMenuItem(MENU_ITEM_3);
myContextMenu.customItems.push (item3);