AS3-Dynamically Creating Classes From XML
Learned from http://www.insideria.com/2009/10/dynamically-creating-classes-f.html
转自:http://www.insideria.com/2009/10/dynamically-creating-classes-f.html
不翻译了,大伙儿自个儿翻译……
Step 1: Create A Test Class
The first thing we need to do is make a test class for us to use. Create the following class in your project in a com.flashartofwar.example package.
package com.flashartofwar.example {
import flash.display.Sprite;
/**
* @author jessefreeman
*/
public class RedBox extends Sprite {
protected var _width:Number = 0;
protected var _height:Number = 0;
/**
* We override the public setter for width so we can redraw
* the box when the width is changed.
*/
override public function set width(value : Number) : void {
_width = value;
trace("Width", super.width);
redraw();
}
override public function get width() : Number {
return _width;
}
/**
* We override the public setter for width so we can redraw
* the box when the width is changed.
*/
override public function set height(value : Number) : void {
_height = value;
redraw();
}
override public function get height() : Number {
return _height;
}
public function RedBox() {
width = 100;
height = 100;
redraw();
}
/**
* This simply clears the graphics and redraws the box based
* on the new width and height.
*/
public function redraw():void
{
graphics.clear();
graphics.beginFill(0xFF0000);
graphics.drawRect(0, 0, width, height);
graphics.endFill();
}
}
}
AStar(A星算法)-Grid Class
A星算法中需要用到网格类,至于网格类是干嘛的,只不过是保存信息的网格,没有实际意义。
Grid.as:
package {
/**
* Holds a two-dimensional array of Nodes methods to manipulate them, start node and end node for finding a path.
*/
public class Grid{
private var _startNode:Node;
private var _endNode:Node;
private var _nodes:Array;
private var _numCols:int;
private var _numRows:int;
/**
* Constructor.
*/
public function Grid(numCols:int,numRows:int) {
_numCols = numCols;
_numRows = numRows;
_nodes = new Array ;
for (var i:int = 0; i < _numCols; i++) {
_nodes[i] = new Array ;
for (var j:int = 0; j < _numRows; j++) {
_nodes[i][j] = new Node(i,j);
}
}
}
////////////////////////////////////////
// public methods
////////////////////////////////////////
/**
* Returns the node at the given coords.
* @param x The x coord.
* @param y The y coord.
*/
public function getNode(x:int,y:int):Node {
return _nodes[x][y] as Node;
}
/**
* Sets the node at the given coords as the end node.
* @param x The x coord.
* @param y The y coord.
*/
public function setEndNode(x:int,y:int):void {
_endNode = _nodes[x][y] as Node;
}
/**
* Sets the node at the given coords as the start node.
* @param x The x coord.
* @param y The y coord.
*/
public function setStartNode(x:int,y:int):void {
_startNode = _nodes[x][y] as Node;
}
/**
* Sets the node at the given coords as walkable or not.
* @param x The x coord.
* @param y The y coord.
*/
public function setWalkable(x:int,y:int,value:Boolean):void {
_nodes[x][y].walkable = value;
}
////////////////////////////////////////
// getters / setters
////////////////////////////////////////
/**
* Returns the end node.
*/
public function get endNode():Node {
return _endNode;
}
/**
* Returns the number of columns in the grid.
*/
public function get numCols():int {
return _numCols;
}
/**
* Returns the number of rows in the grid.
*/
public function get numRows():int {
return _numRows;
}
/**
* Returns the start node.
*/
public function get startNode():Node {
return _startNode;
}
}
}
代码简洁明了,传入行数和列数就可以构建一个网格,然后就是getters / setters。
AStar(A星算法)-Node Class
刚好在书上看到A星算法,今天先来讲下会用到的一些术语。
A星算法是一种静态路网中求解最短路径最有效的方法。恰当的使用,A星算法肯定能找出两点之间的最佳路径而且效率相当高。
节点(node):本质上就是方形网格里的某一个方格(yujjj注:为什么不把他们描述为方格?因为在一些时候划分的节点不一定是方形的,矩形、六角形、或其它任意形状,本书中只讨论方格)。由此可以看出,路径将会由起点节点,终点节点,还有从起点到终点经过的节点组成。
代价(cost):这是对节点优劣分级的值。代价小的节点肯定比代价大节点更好。代价由两部分组成:从起点到达当前点的代价和从这个点到终点的估计代价。代价一般由变量f,g和h,具体如下。
f:特定节点的全部代价。由g+h决定。
g:从起点到当前点的代价。它是确定的,因为你肯定知道从起点到这一点的实际路径。
h:从当前点到终点的估计代价。是用估价函数(heuristic function)计算的。它只能一个估算,因为你不知道具体的路线——你将会找出的那一条。
估价函数(heuristic):计算从当前点到终点估计代价的公式。通常有很多这样的公式,但他们的运算结果,速度等都有差异(yujjj注:估价公式计算的估计值越接近实际值,需要计算的节点越少;估价公式越简单,每个节点的计算速度越快)。
待考察表(open list):一组已经估价的节点。表里代价最小的节点将是下一次的计算的起点。
已考察表(closed list):从待考察表中取代价最小的节点作为起点,对它周围8个方向的节点进行估价,然后把它放入“已考察表”。
父节点(parent node):以一个点计算周围节点时,这个点就是其它节点的父节点。当我们到达终点节点,你可以一个一个找出父节点直到起点节点。因为父节点总是带考察表里的小代价节点,这样可以确保你找出最佳路线。
Queryloader-Preload your website in style

There is always a minor problem when it comes to preloading image on a website. Nobody really has a full solution for it. There are a lot of preloaders available, but most of the time they only display the words: “Loading page” or have an animated image that spins. Why can’t there be a nice loading bar of some kind?
I’ve gotten a few request on how to make a preloader, or people asking me how to get all the images of a web page and preload them (even the images in CSS).This preloader has it all. Loading bar, custom animations and getting all images included in the web page.
Download the source code here:
http://www.gayadesign.com/scripts/queryLoader/queryLoader.zip
View the example here:
http://www.gayadesign.com/scripts/queryLoader/
预加载还是比较有用的东西,有兴趣的可以试试看哈。
关于如何使用,还是看作者的详细介绍吧。
PHP-Securimage图片验证码工具
What is Securimage?
Securimage is an open-source free PHP CAPTCHA script for generating complex images and CAPTCHA codes to protect forms from spam and abuse. It can be easily added into existing forms on your website to provide protection from spam bots. It can run on most any webserver as long as you have PHP installed, and GD support within PHP. Securimage does everything from generate complicated CAPTCHA images to making sure the code the user entered was correct.
* Check out the quickstart guide to get going fast
* See live example sites using Securimage
* Download the latest version
Securimage是一个开源的PHP验证码脚本,它可以生成复杂的验证码图像,用来保护论坛免于垃圾留言的干扰。
我们可以轻松的把它加入到已存在的论坛中。它可以在大部分安装了PHP的web服务器上运行,只要你安装了GD库。
Securimage生成复杂的验证码,并且会确认用户输入的正确性。
Securimage的Ajax版本所用的是prototype库,呵呵,个人更喜欢jQuery。
jQuery Plugins-Lazy Load
Learned From http://www.webappers.com/2009/10/01/delays-loading-of-images-with-lazy-loader-jquery-plugin/
Lazy loader is a jQuery plugin written in JavaScript. It delays loading of images in (long) web pages. Images outside of viewport (visible part of web page) wont be loaded before user scrolls to them. This is opposite of image preloading.
Using Lazy loader on long web pages containing many large images makes the page load faster. Browser will be in ready state after loading visible images. In some cases it can also help to reduce server load.
There are options for control maniacs who need to fine-tune. You can set threshold on how close to the edge image should come before it is loaded. You can also set placeholder image and custom event to trigger loading. You can check out the demo with FadeIn Effect enabled.
Requirements: jQuery Framework
Demo: http://www.appelsiini.net/projects/lazyload/enabled_fadein.html
License: MIT License
Adobe Cookbook 2.0
From http://www.insideria.com/2009/09/the-adobe-developer-connection.html
Adobe has announced that the brand new Adobe Cookbooks application is now live! This new Adobe Cookbook application replaces the existing individual cookbook applications that existed for Flex, AIR and Mobile.
The new application still supports those technologies, and adds an additional dozen or so technologies, within a new single application.
The new application has an updated design and includes addition new features:
* Additional technologies supported (BlazeDS, Catalyst, PHP, etc)
* Improved navigation and search
* Recipe requests - for developers who can't find the code sample they are looking for
* Related recipes - that appear in a side bar of all recipe pages
* Cookbook Explorer - a visual display of top cookbook recipes and contributors
* New contributor recognition features - profile pics, links to blogs, links to community profiles
If you are a contributor, you should be sure to have your Adobe.com community profiles filled out before publishing any recipes as that is where the application will pull in a lot of the author recognition features. To fill out your community profile, login here and click "View your profile."
I have contributed recipes in the past and will continue to do so. Another important thing to note is that the O'Reilly relationship with the Adobe Cookbooks has allowed for many community contributed recipes to find their way into one of the published cookbooks. My experience in writing the AIR 1.5 cookbook included the selection of many recipes from the old AIR cookbook site for the printed book. This relationship continues with the new cookbook site, so not only will you now have additional recognition online when you submit a recipe, you may also wind up in a future printed O'Reilly cookbook.
Check out the new cookbook site at http://cookbooks.adobe.com.
LapOfLuxury Theme Released
designdisease每次出品的Theme都比较经典,这次是LapOfLuxury

Demo地址:http://www.lapofluxury.com/
AS3-SteeredVehicle.flock()群落行为分析
Demo:
先来看看什么是群落,来自wikipedia的解释是:
群落 (Biocoenosis)或称为“生物群落”。指的是生态学中,在一个群落生境内相互之间具有直接或间接关系的所有生物,或曰生物的总合。也有人作如下定义:生存在一起并与一定的生存条件相适应的动植物的总体。群落生境是群落生物生活的空间。一个生态系统则是群落和群落生境的系统性相互作用。
从定义可知,它是由在一个群落生境里生活的动物群落和植物群落,也称为生物系统组成的。生物地理群落是生物和地理因素,就是说无生命的环境的总和。这些关系的总和则是生态系统。这个概念1877年由卡尔·奥古斯都·莫比奥斯提出,他将一个牡蛎海岸上的所有生物称作“生物社区”或是“生物群落”。
一个群落的生物物种占据不同的小生境。一个群落的生物相互之间有着不同的关系。
Demo中的机车们就是一个群落。
AS3-SteeredVehicle.avoid()回避行为分析
Demo:
今天的内容是回避行为avoid(),先引用书中的文字来说明下回避行为:
对象回避主题的完整意义是指,在机车行走的路线中存在一些障碍物,机车必须绕开、防止触碰到它们。听上去和碰撞检测有关,然而这仅仅是发生在预测阶段,也就是:“以我当前的速度行驶下去,可能就会撞到它了。”
“回避行为”和“避开行为”、“躲避行为”都有所区别,“避开”和“躲避”都是针对单个目标的,而且机能方面“回避行为”更为复杂。
AS3-Vehicle机车类
因为前段时间博客流量过载,所以落下很多内容没更新,现在更新下。
所谓机车类,《AdvancED ActionScript 3.0 Animation》中说:
机车类是转向角色的基类,但它不提供任何转向行为,只处理与运动相关的基本内容,如位置,速度,质量以 及角色接触场景边缘后的反应(反弹还是穿越出现在另一边)。转向机车(SteeredVehicle)类继承机车类,并为之增加转向行为。使用这样的结构 其目的是为了让机车类可以用于仅需要移动而不需要转向行为的对象,同时也可以让转向机车类不考虑基本运动的细节而专心实现转向功能。
AS3-Vector2D类
OK,今天要讲的是2D向量,其实呢,Flash CS4中自带了Vector3D类,有兴趣的话可以打开Vector3D.as看一下。
不过在只处理2维的情况下,Vector3D反而让会事情变得麻烦,SO,Vector2D还是有存在的必要滴。
一个Vector2D向量需要的变量只有2个,一个x轴的长度_x,一个y轴的长度_y,然后就是向量的各种算法,add,subtract,multiply,divide。。
整个类的代码如下,版权属于Keith Peters的《AdvancED ActionScript 3.0 Animation》,在以后的Demo中会经常用到这个类。