This is a blog about Flash by betaruce :-)

Calendar

October 2005
M T W T F S S
« Sep   Nov »
  1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31  

October 26, 2005

Some hints on AS 3 coding

Filed under: Using Flash — betaruce @ 5:52 pm

after several days of coding, I learn some new syntax and coding techniques concerning AS 3.

1> to draw things with commands like lineTo(), beginGradientFill(), etc, we need to class Graphics:

MovieClip.graphics.lineTo(10,10);

2> the method drawCircle() of class Graphics has the following parameters:

xCenter:Number - The x location of the center of the circle relative to the registration point of the parent movie clip (in pixels).
yCenter:Number - The y location of the center of the circle relative to the registration point of the parent movie clip (in pixels).
radius:Number - The radius of the circle (in pixels).

If you set the xCenter and yCenter to some values, the circle drawn will have the offset even you change the x y properities later. e.g:

this.graphics.drawCircle(50,50,100)
x=100;
y=100;

The above circle will be positioned at (150,150) from the stage top left corner (if the stage is the container of that circle).

3> In previous version of Flash, I can set a 2D array like that:

size=10;
arr=new Array();
for(i=0;i){
  arr[i]=new Array();
  for(j=0;j){
    arr[i][j]=j;
  }
}

this doesn’t seems to work in AS 3 (maybe that’s my problem). So I use the following method:

var size:int=10;
var arr:Array=new Array()
for(var i:int=0;i){
  var temp:Array=new Array();
  for(var j:int=0;j){
    temp[j]=j;         
  }
  arr.push(temp);
}

I need to set up another array “temp” and then push it into “arr” to make it 2D. It works anyway, although I would prefer another more convenient methods.

4> You cannnot have a BitmapData show directly as by attachBitmap to a movie clip in Flash 8. Also attachBitmap is gone in AS 3. Instead you need the new Bitmap class:

var b:BitmapData=new BitmapData(100,100,false);
var bm:Bitmap=new Bitmap(b);
this.addChild(bm);

We need to tell the Bitmap “bm” to reference to the BitmapData “b”, and then use addChild to add it to the display list to make it show.

Hope these 4 “findings” can help :) . Feel free to correct me if there’s mistake.

• • •

AS 3 Experiment: Animated Plasma (speed improvement)

Filed under: My Flash Programs — betaruce @ 1:20 pm

Compared with my same experiment produced in Flash 8, the following (you will need Flash Player 8.5 to see it) is much faster and smoother since it is coded with AS 3.

http://www.box-hosting.net/~betaruce/as3/plasma.swf

This animated plasma effect is created with pixel manipulation. In my Flash 8 version, there are 120*120=14400 calculations/frame and of cause it hangs Flash player 8. In the above example, there are 300*300=90000 calculations/frame and the speed is awesome in player 8.5. On my 1.5Ghz computer the speed is still satistfactory for 500*500=250000 calculations/frame.

The following is the code:

package {
        import flash.display.*;
        import flash.geom.*;
        import flash.util.*;
        import flash.events.*;

        public var sx:int;
        public var sy:int;
        public var pixels:Array=new Array();
        public var palette:BitmapData;
        public var b:BitmapData;
        public var bm:Bitmap;
       
        public class plasma extends MovieClip {
                public function plasma() {
                        stage.align=‘TOP_LEFT’;
                        stage.scaleMode=StageScaleMode.SHOW_ALL;
                        sx=stage.stageWidth;
                        sy=stage.stageHeight;
                        //
                        var colors:Array=[0xff0000,0xffff00,0x00ff00,0x00ffff,0x0000ff,0xff00ff,0xff0000];
                        var alphas:Array=[100,100,100,100,100,100,100];
                        var ratios:Array=[0,43,85,128,170,212,255];
                        var mat:Matrix=new Matrix();
                        mat.createGradientBox(256,1);
                        var gradient:MovieClip=new MovieClip();
                        gradient.graphics.beginGradientFill(‘linear’,colors,alphas,ratios,mat);
                        gradient.graphics.drawRect(0,0,256,1);
                        //
                        palette=new BitmapData(256,1,false);
                        palette.draw(gradient,new Matrix());
                        b=new BitmapData(sx,sy,false);
                        bm=new Bitmap(b);
                        addChild(bm);
                        //
                        var den:int=16;
                        for(var i:int=0;i){
                                var temp:Array=new Array();
                                for(var j:int=0;j){
                                        temp[j] = 128+128*Math.sin(i/(den*2));
                                        temp[j] += 128+128*Math.sin(j/(den*4));
                                        temp[j] += 128+(128*Math.sin(Math.sqrt((i-sx/2)*(i-sx/2)+(j-sy/2)*(j-sy/2))/den));
                                        temp[j] += 128+128*Math.sin(Math.sqrt(i*i+j*j)/den);
                                        temp[j] /= 4;               
                                }
                                pixels.push(temp);
                        }
                        addEventListener(EventType.ENTER_FRAME,onEnterFrame);
                }
               
                private function onEnterFrame(e:Event){
                        for (var i:int=0; i) {
                                for (var j:int=0; j) {
                                        var temp:Number = getTimer()/20;
                                        b.setPixel(i, j, palette.getPixel(int((pixels[i][j]+temp)%256), 0));
                                }
                        }
                }
        }
}

• • •
Powered by: WordPress • Template by: Priss