圣叹@游戏开发

我们面对现实,我们终于理想
  《Advanced ActionScript 3 with Design Patterns》有一篇讲解了如何使用as实现singleton模式,很是巧妙。AS3版的Singleton例子如下:

package kono.designPattern
{
  public class SingleTon
  {
    static private var _instance:SingleTon;
    
    public function SingleTon(enforcer:SingleEnforcer){}
    
    public static function getInstance():SingleTon
    {
      if(SingleTon._instance == null)
      {
        SingleTon._instance = new SingleTon(new SingleEnforcer());
      }
      return SingleTon._instance;
    }
    
    public function action():void
    {
      trace("Do Something!");
    }
  }  
}
class SingleEnforcer{}

妙就妙在AS版的singleton使用内部定义的一个class阻止实例化SingleTon类。假如使用 var a:SingleTon = new SingleTon() 方法,则错误提示参数不正确,即使使用下面的代码仍然提示参数类型错误:

var a:*;
var a:SingleTon = new SingleTon(a);

当然也有例外发生,如果使用 “null” 做参数:var a:SingleTon = new SingleTon(null)则通过编译。如下面的代码都是正确的:

package {
  import flash.display.Sprite;
  import kono.designPattern.SingleTon;

  public class DesignPattern extends Sprite
  {
    public function DesignPattern()
    {
      
      SingleTon.getInstance().action();
      var a:SingleTon = new SingleTon(null);
      a.action();      
    }
  }
}


附:singleton模式的定义:
The Singleton design pattern is used to limit a class to one instance and provide global access to that instance. In
many cases, you need to limit a class to only one instance. Common examples include classes that manage
resources that are intrinsically singular such as selection focus, navigation history, and window depth. Consider the
case of a class that manages the state of the user's cursor. Because there is only ever one cursor, you shouldn't
have more than one instance of that class. Another example is a class that loads application settings from an XML
file and provides access to those settings. You wouldn't want to waste resources by having two instances of that
class or by loading the XML file more than once.
1、A private static property, _instance, to hold the single instance of the class.
2、A constructor with a parameter typed to a class, SingletonEnforcer, that is available only to the Singleton
class. The SingletonEnforcer class definition is also required.
3、A public static method, getInstance(), that provides access to the single instance and creates it if it does not
exist.
Oct
6
2007
  之前真是钻牛角尖了...倒,喊了一天“为什么?”也没一点进展,原来一切是如此简单....
问题的提出:    
  用datagrid显示数据,并且在用户编辑数据后希望将这一列的数据更新(editable=true)到数据库。所以此时selectedItem用不了——不是取得这一行的数据,也不是取得鼠标点击的数据,而是整个一列——甚至是整个datagrid被修改后的数据。

  这里难倒了绝大多数人,因为理论上存在一个getter方法返回某一列的数据。在试验了所有属性后,我甚至把datagrid组件的class也看了一遍。然而面对这个修改的千疮百孔的源程序,实在是只有套用老肖的口头禅:“Are u joke me?”伴随着独有的那种抑扬顿挫的声调。
  晚上无意间google到了一个flex debug的类包,Flex2 Debug Component (作者 Mike Nimer),在监测 dataProvider 属性的变化时突然发现——我要找的就是这个属性...Shit
  原来,大家都理解为dataProvider 为datagrid提供数据,但没想到的是,这个值会随着某个数据的修改而修改——换个角度想,这个值也必须被修改,因为该datagrid同样可能成为其它组件的dataProvider 。咋早没意识到这点呢...so:


/*********************************************
var a:Object = _DataGrid.dataProvider;
Debug.show(a[0].id);
/*********************************************


  即可取得任何期望的数据。

iaipvfhp在论坛回复有itemEditEnd 事件在编辑结束后触发。这样只发送被修改的数据是个好办法:


/*************************************************************************************
<mx: DataGrid    editable="true"    itemEditEnd="onEditEnd(event)" />
/*************************************************************************************


Flex2 Debug Component 是个调试的好东东,然而作者提供的swc文件似乎有问题,不如直接在需要用到debug的地方调用

import com.mikenimer.components.*

Sep
9
2007
(点击观看,需要flash player9),简陋版windows播放器视觉插件效果。

//////////////////// in frame1 ///////////////////////////////
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.filters.*;
import flash.geom.*;

var bmp:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x0);
var image:Bitmap = new Bitmap(bmp);
addChild(image);

var world:Sprite=new Sprite();
world.addEventListener(Event.ENTER_FRAME, worldDraw);

var _mc:Sprite=new Sprite();
_mc.name="_mcmc";
_mc.x=stage.stageWidth/2;
_mc.y= stage.stageHeight/2;
world.addChild(_mc);

var rad_speed:Number = 2;
var total_nums:uint = 10;
var f:Number = -.01;

var blurFilter:BlurFilter = new BlurFilter(3, 3, 1);
var colorFilter:ColorMatrixFilter = new ColorMatrixFilter([.9, -.1, 0, 0, f, 0, .8, -.2, 0, f, .1, 0, .9, 0, f, 1, 0, 0, 1, 0]);

function downGraph() {
world.removeChild(_mc);
_mc=new Sprite();
_mc.name="_mcmc";
_mc.x=stage.stageWidth/2;
_mc.y= stage.stageHeight/2;
world.addChild(_mc);
var a:Number = Math.random()*.2;
var t:Number = (Math.random()-.5)*a;
var z:Number = (Math.random()-.5)*a;
/*var a:Number = 2.1;
var t:Number = 0.005*a;
var z:Number = 0.003*a;*/
rad_speed = (Math.random()-.5)*3.8;
//rad_speed = -0.22*3.8;

var line_bool:uint =Math.random()*5;
var swirl_bool:uint = Math.random()*3;
var rnd_xy_bool:uint =Math.random()*4;
var i:uint = 0;
for (var k:uint=0; k<total_nums; k++) {
   for (var m:uint=0; m<360; m += 5) {
    var line:SpriteLine =new SpriteLine(t,z,line_bool,swirl_bool);
    _mc.addChild(line);
    line.ox=Math.random()*30;
    //trace(line.name+"    "+line.ox);
    if (rnd_xy_bool) {
     line.ox = line.__x=line.x=k*Math.sin(m/180*Math.PI)*8+Math.random()*20;
     line.oy = line.__y=line.y=k*Math.cos(m/180*Math.PI)*6+Math.random()*20;
    } else {
     line.ox = line.__x=line.x=k*Math.sin(m/180*Math.PI)*8;
     line.oy = line.__y=line.y=k*Math.cos(m/180*Math.PI)*6;
    }
    line.lx = line.x*.3;
    line.ly = line.y*.3;
    line.lineStart();
    if (new int(Math.random()*3)) {
     line.transform.colorTransform=new ColorTransform(1,1,1,1, i*512/k/m-256,   i*255/k/m, new int(Math.random()*512)-256,0);
    } else {
     line.transform.colorTransform=new ColorTransform(1,1,1,1, 255,   255,   255,0);
    }
   }
}
}
var val:uint;
clearInterval(val);
val = setInterval(downGraph, 2000);
downGraph();
function worldDraw(event:Event):void {
var _speed:Number =100;
var target = world.getChildByName("_mcmc");
target.rotation += rad_speed;
bmp.draw(world);
bmp.applyFilter(bmp,bmp.rect,new Point(0, 0),blurFilter);
bmp.applyFilter(bmp,bmp.rect,new Point(0, 0),colorFilter);

if (!gfx2) {
   var gfx2 = bmp.clone();
}
var trans:Matrix = new Matrix();
var scale:Number = 1+(0.05*_speed/100);
trans.scale(scale, scale);
var offset:Number = -(scale-1)/2;
trans.translate(offset*bmp.width,offset*bmp.height);
gfx2.draw(bmp, trans);
bmp.draw(gfx2);
}


//////////////////// SpriteLine类 ///////////////////////////////
package {
import flash.display.Sprite;
import flash.events.Event;

public class SpriteLine extends Sprite {
   public var ox:int;
   public var oy:int;
   public var lx:int;
   public var ly:int;
   public var __x:int;
   public var __y:int;

   //private var a:Number;
   private var t:Number;
   private var z:Number;

   private var line_bool:uint;
   private var swirl_bool:uint;

   public function SpriteLine(tt:Number,zz:Number,ll:uint,ss:uint) {
    super();
    this.t=tt;
    this.z=zz;
    this.line_bool=ll;
    this.swirl_bool=ss;
   }
   public function lineStart():void {
    this.addEventListener(Event.ENTER_FRAME, graStart);
   }
   private function graStart(event:Event):void {
    //var matrix=new Matrix;
    //matrix.createGradientBox(512,512,512,0,0);
    if (swirl_bool) {
     var x:int = this.__x;
     this.__x += 10*Math.cos(this.__y*z)*Math.sin(this.__y*t);
     this.__y += 10*Math.sin(x*z)*Math.cos(x*t);
    } else {
     this.__x += 10*Math.cos(this.__y*z)*Math.sin(this.__y*t);
     this.__y += 10*Math.sin(this.__x*z)*Math.cos(this.__x*t);
    }
    this.graphics.clear();
    this.graphics.lineStyle(3,0x990000);
    //this.graphics.lineGradientStyle( "radial", [ 0x99FF00, 0xFF0000 ], [ 100, 100 ], [ 0, 255 ],matrix );
    if (line_bool>1) {
     this.graphics.moveTo(this.ox-this.x,this.oy-this.y);
     this.graphics.lineTo(this.__x-this.x,this.__y-this.y);
    } else if (line_bool == 1) {
     this.graphics.moveTo(this.ox,this.oy);
     this.graphics.lineTo(this.__x+this.lx,this.__y+this.ly);
    } else {
     this.graphics.moveTo(this.ox-this.lx,this.oy-this.ly);
     this.graphics.lineTo(this.__x-this.lx,this.__y-this.ly);
    }
    this.ox = this.__x;
    this.oy = this.__y;
   }
}
}
Jun
29
2007
分页: 24/24 第一页 上页 19 20 21 22 23 24 最后页 [ 显示模式: 摘要 | 列表 ]