5D艺术网首页
商城
|
资讯
|
作品
|
博客
|
教程
|
论坛
登录
注册
加为好友
发短消息
来自:
性别:秘密
最后登录:2010-03-02
http://wyongedu.5d.cn/
Love is like a butterfly. It goes where it pleases and it pleases where it goes
首页
|
新闻
|
话题
|
博客
|
相册
|
艺术作品
|
社交关系
|
留言板
|
社交圈
2005/11/03 | Actionscript 3 - First Steps XI
类别(失落的学习)
|
评论
(0)
|
阅读(43)
|
发表于 12:46
Actionscript 2 wasn't that bad after all, but a few things drove some developers crazy. For example if you initialized a member variable with an Object, this member magically behaved like a static classmember, due to the storage of the member in the prototype. In ActionScript 3 this works just like expected, as you can see here.
package {
import flash.util.trace;
import flash.display.MovieClip;
public class Test1 extends MovieClip {
public function Test1() {
var t1:Test2 = new Test2();
var t2:Test2 = new Test2();
}
}
private class Test2 {
private var a:Array = new Array();
public function Test2() {
a.push( a.length);
trace(a);
}
}
}
Output
0
0
Actionscript 3 - First Steps XII
Now, that we know how to set the framerate and the size of the stage, we would like to move something across the stage. In the old days we could simply assign a function to the onEnterFrame-handler of a MovieClip instance. As you might expect already, things are slightly different in AS3. If you have seen, how we activated the mouseEvents you know already, how it works in AS3 - addEventListener. All we need to know now, is the exact name of the event. Fortunately we don't have to remember all the names, but just find out, who knows it. As it turns out, EventType has a static member ENTER_FRAME, which is the one we need.
Out little example creates ten circles and lets them move across the stage.
package {
// use
// -default-size 800 800 -default-frame-rate 31
// as the additional compiler arguments
import flash.events.Event;
import flash.events.EventType;
import flash.display.Sprite;
public class Test extends Sprite {
public function Test() {
var minSize:Number = 10;
var maxSize:Number = 50;
var minSpeed:Number = 20;
var maxSpeed:Number = 50;
for( var i:int=0; i<10; i++){
var mc:MovingCircle = new MovingCircle(
getRandom( minSize, maxSize),
getRandom( minSpeed, maxSpeed),
getRandom( minSpeed, maxSpeed));
addChild( mc);
}
}
private function getRandom( minVal:Number, maxVal:Number):Number{
return minVal + Math.random() * (maxVal - minVal);
}
}
private class MovingCircle extends Sprite
{
private var xSpeed:Number;
private var ySpeed:Number;
public function MovingCircle( size:Number, xSpeed:Number, ySpeed:Number){
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
createUI( size);
addEventListener(EventType.ENTER_FRAME, onEnterFrame);
}
private function createUI( size:Number){
graphics.beginFill(Math.floor(Math.random() * 0xffffff));
graphics.drawCircle(-size/2, -size/2, size);
graphics.endFill();
}
private function onEnterFrame( evt:Event):Void{
x += xSpeed;
y += ySpeed;
if( x < 0){
x = Math.abs(x);
xSpeed *= -1;
} else if( x > stage.stageWidth){
x = stage.stageWidth - (x - stage.stageWidth);
xSpeed *= -1;
}
if( y < 0){
y = Math.abs(y);
ySpeed *= -1;
} else if( y > stage.stageHeight){
y = stage.stageHeight - (y - stage.stageHeight);
ySpeed *= -1;
}
}
}
}
Since we don't want to move them off the stage, we limit the movement to the size of the stage. Isn't it nice, we can get the stage's dimensions from the stage property of the current circle, instead of having to use constants.
ActionScript 3 - First Steps XIII
Ok, now for something real - XML. At the labs site you can read about E4X, the integration of XML in Actionscript. Gordon Smith gives a nice overview of the new possibilities. I liked this part most: "Compared with the current XML support in the Flash Player, E4X allows you to write less code and execute it faster because more processing can be done at the native speed of C++." Wohoo, the native speed of C++, that's definitely a nice way to go. So let's do a speed test.
package {
import flash.display.Sprite;
import flash.util.trace;
import flash.util.getTimer;
import flash.xml.XMLNode;
import flash.xml.XMLDocument;
public class Test extends Sprite {
public function Test() {
var employees:XML =
<employees>
<employee ssn="123-123-1234">
<name first="John" last="Doe"/>
<address>
<street>11 Main St.</street>
<city>San Francisco</city>
<state>CA</state>
<zip>98765</zip>
</address>
</employee>
<employee ssn="789-789-7890">
<name first="Mary" last="Roe"/>
<address>
<street>99 Broad St.</street>
<city>Newton</city>
<state>MA</state>
<zip>01234</zip>
</address>
</employee>
</employees>;
var employees2:XMLDocument = new XMLDocument('<employees><employee
ssn="123-123-1234"><name first="John"
last="Doe"/><address><street>11 Main
St.</street><city>San
Francisco</city><state>CA</state><
zip>98765</zip></address></employee><employee
ssn="789-789-7890"><name first="Mary"
last="Roe"/><address><street>99 Broad St.</street><city>Newton</city><
state>MA</state><zip>01234</zip>
</address></employee></employees>');
var t:int = getTimer();
for( var i:int=0; i<10000;i++){
var x:XMLList = employees.employee.(@ssn == "789-789-7890");
}
trace( getTimer() - t);
var t:int = getTimer();
for( var i:int=0; i<10000;i++){
var n:XMLNode = employees2.firstChild.firstChild;
var nFound:Array = new Array();
while( n != null){
if( n.attributes.ssn == "789-789-7890"){
nFound.push( n);
}
n = n.nextSibling;
}
}
trace( getTimer() - t);
}
}
}
0
评论
Comments
日志分类
首页
[192]
失落的感情
[27]
失落的学习
[30]
失落的时间
[5]
失落的网络
[85]
失落的自己
[42]
失落的朋友
[3]