A闪的 BLOG 技术与人文
到目前为止相信大家也应该感到有些兴奋,因为我们正在接近当前项目的逻辑交互部分。对于游戏来说这些无疑是非常令人兴奋的。因为我们所有游戏体验全部都由这些交互效果来构成。那么现在我们基于GPU加速的界面编写与以前稍有不同。因为adobe为我们提供了非常底层的API接口。这些借口在使用的过程中非常的繁杂,所以我们只能依赖于第三方的引擎或者自己编写一套用户GPU加速的引擎。前面的文章已经介绍过很多次了,我们使用的是strling引擎。这款引擎在使用的时候大家可能会遇到很多不适应的地方,文章中会为大家讲解清楚。
由于我们的2D界面全部使用strling引擎来实现。所以界面部分我们的基类就定义为strling.display.Sprite这个类,因为这个类是strling中最基本的现实对象对于我们实现程序来说是最好的选择,同时其他的类我们也需要,其目的就是为了节约资源。
我们先来编写一个PictureLoader类,这个类主要负责加载外部图片,通过资源引擎来对资源进行获取。
关键代码:
private function init(picurls:String):void
{
this.pic_loader = new Loader();
var picurl:URLRequest = new URLRequest(picurls);
this.pic_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,pic_load_complete);
this.pic_loader.load(picurl);
picurl = null;
}
private function pic_load_complete(evt:Event):void
{
this.pic_bit = this.pic_loader.content as Bitmap;
var loadcompleteevents:DataLoadEvents = new DataLoadEvents(DataLoadEvents.DATA_LOAD_COMPLETE);
this.dispatchEvent( loadcompleteevents );
loadcompleteevents = null;
}
通过这样的操作我们就可以通过这个类来加载一个image图片。
然后我们来编写一个GameButton组件,这个组件的一部分界面来自于PictureLoader类的支持。
关键代码:
ButtonUIManage类
public function ButtonUIManage(xml:XML,list:XMLList)
{
this._width = int(list[4]);
this._height = int(list[5]);
this._x = int(list[2]);
this._y = int(list[3]);
this.picloader = new Loader();
var picurl:URLRequest = new URLRequest( xml );
this.picloader.contentLoaderInfo.addEventListener(Event.COMPLETE,load_complete_header);
this.picloader.load(picurl);
picurl = null;
}
private function load_complete_header(evt:Event):void
{
this.picloader.contentLoaderInfo.removeEventListener(Event.COMPLETE,load_complete_header);
//
var upbit:BitmapData = new BitmapData(this._width,this._height);
upbit.copyPixels((this.picloader.content as Bitmap).bitmapData,new Rectangle(0,0,this._width,this._height),new Point(0,0) );
this._upbitmap = Image.fromBitmap(new Bitmap(upbit) );
var dbit:BitmapData = new BitmapData(this._width,this._height);
dbit.copyPixels((this.picloader.content as Bitmap).bitmapData,new Rectangle(this._width*2,0,this._width,this._height),new Point(0,0) );
this._downbitmap = Image.fromBitmap(new Bitmap(dbit) );
var obit:BitmapData = new BitmapData(this._width,this._height);
obit.copyPixels((this.picloader.content as Bitmap).bitmapData,new Rectangle(this._width,0,this._width,this._height),new Point(0,0) );
this._overbitmap = Image.fromBitmap(new Bitmap(obit) );
var unbit:BitmapData = new BitmapData(this._width,this._height);
unbit.copyPixels((this.picloader.content as Bitmap).bitmapData,new Rectangle(this._width*3,0,this._width,this._height),new Point(0,0) );
this._unbitmap = Image.fromBitmap(new Bitmap(unbit) );
var picloadevent:DataLoadEvents = new DataLoadEvents(DataLoadEvents.DATA_LOAD_COMPLETE);
this.dispatchEvent(picloadevent);
picloadevent = null;
}
GameButton类关键代码
private function ontouch(evt:TouchEvent):void
{
var t:Touch = evt.getTouch(this);
var event:StarlingMouseEvents;
if(t)
{
if(t.phase == TouchPhase.BEGAN && this._btn_state == true)
{
this._btn_state = false;
this.addChild( this._down );
event = new StarlingMouseEvents(StarlingMouseEvents.MOUSE_DOWNS);
}
if(t.phase == TouchPhase.ENDED && this._btn_state == false)
{
this._btn_state = true;
this.addChild( this._up );
event = new StarlingMouseEvents(StarlingMouseEvents.MOUSE_CLICK);
}
if(t.phase == TouchPhase.HOVER && this._btn_state == true && this._move.parent == null)
{
this.addChild( this._move );
event = new StarlingMouseEvents(StarlingMouseEvents.MOUSE_OVERS);
}
}
else
{
event = new StarlingMouseEvents(StarlingMouseEvents.MOUSE_OUTS);
this.addChild( this._up );
}
if(this.numChildren>1)
{
this.removeChildAt(0);
}
if(event)
{
this.dispatchEvent(event);
event = null;
}
}
public function get enable():Boolean
{
return this._Enable;
}
public function set enable(val:Boolean):void
{
var i:uint = this.numChildren;
while(i>0)
{
i–;
this.removeChildAt(0);
}
if(val)
{
this._Enable = val;
this.addChild( this._up );
this.addEventListener(TouchEvent.TOUCH,ontouch);
}
else
{
this._Enable = false;
this.addChild( this._un );
this.removeEventListener(TouchEvent.TOUCH,ontouch);
}
}
OK!关于组件部分先介绍到这里,下一篇文章中我将向大家介绍3D引擎的内容!