使用 removeChild 方法,从显示列表中删除一个对象,并以该对象的名字作为参数。第一,删除一个对象,不是去毁灭它,对象依然保持原样,只是暂时被移除,当再次被加入到显示列表中,对象仍保持原来的状态。换句话讲,如果显示对象里面绘制了图形,或是已加载了一些外部信息,那么将它重新加入显示列表后,就不必再去重绘或重载这些信息。第二,把该对象重新加入显示列表后,还可以为它指定处在显示列表中的位置,这就是我们所熟知的重定父级。
从一个影片剪辑中删除一个对象,再把它加载到另一个影片中剪辑中,并保持刚刚被删除时的状态,在以前是不可能完成的。事实上,有时并不需要去删除影片,因为,一个子对象只能有一个父级,把它加入到另一个父级中,就会自动从原来的父级中删除。请看下面示例:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Reparenting extends Sprite {
private var parent1:Sprite;
private var parent2:Sprite;
private var ball:Sprite;
public function Reparenting() {
init();
}
private function init():void {
parent1 = new Sprite();
addChild(parent1);
parent1.graphics.lineStyle(1, 0);
parent1.graphics.drawRect(-50, -50, 100, 100);
parent1.x = 60;
parent1.y = 60;
parent2 = new Sprite();
addChild(parent2);
parent2.graphics.lineStyle(1, 0);
parent2.graphics.drawRect(-50, -50, 100, 100);
parent2.x = 170;
parent2.y = 60;
ball = new Sprite();
parent1.addChild(ball);
ball.graphics.beginFill(0xff0000);
ball.graphics.drawCircle(0, 0, 40);
ball.graphics.endFill();
ball.addEventListener(MouseEvent.CLICK, onBallClick);
}
public function onBallClick(event:MouseEvent):void {
parent2.addChild(ball);
}
}
}
该类中有三个 Sprite 对象:parent1, parent2, ball。 parent1, parent2 影片直接加入显示列表,并在影片中绘制了正方形。 Ball 影片被加入到 parent1,就相当于加入了显示列表并可见。当小球被点击时,它将被加入 parent2 影片中。请注意,没有改变 Ball 的 x,y 坐标的代码,之所以产生移动是因为 Ball 被加载到了不同位置的 Sprite 影片中。 Sprite 影片被删除后再被加入显示列表,Ball 仍会出现。
2.设置和监听键盘焦点
stage.focus = sprite;//设置
监听只需要对stage进行侦听就可以了。
3.三角函数
计算方法:
弧度(radians) = 角度(degrees) * Math.PI /180
角度(degrees) = 弧度(radians) * 180 / Math.PI
Math.atan2(y,x),这是 Flash 的另一个反正切函数,它比 Math.atan(ratio)要有用得多。实事上,只需要学会这个函数的用法就可以了,函数中包括两个参数:对边长度与邻边长度。有时常会误写成 x,y,请注意应该是 y,x。请看如下示例,输入 Math.atan2(1,2),然后记住这个结果:
trace(Math.atan2(1, 2) * 180 / Math.PI);
输出结果为 26.565051177078,这正是角B的度数。下面再输入-1/-2(角D),再来试试:
trace(Math.atan2(-1, -2) * 180 / Math.PI);
//使箭头方向跟随鼠标方向
var dx:Number = mouseX - arrow.x;
var dy:Number = mouseY - arrow.y;
var radians:Number = Math.atan2(dy, dx);//Math.atan2()函数发挥有效的作用,用Math.atan()方法就会出现问题
arrow.rotation = radians * 180 / Math.PI;
//Math.sin()运用,使物体运动 原理是一样的,centerScale表示100%的缩放比, range 表示范围, speed 表示速度。不仅如此,正弦波还在 alpha,rotation 等属性中应用。
Ball.as
Bobbing.as
//使物体以圆形运动
//本章基本公式
基本三角函数的计算:
角的正弦值 = 对边 / 斜边
角的余弦值 = 邻边 / 斜边
角的正切值 = 对边 / 邻边
角度制与弧度制的相互转换:
弧度 = 角度 * Math.PI / 180
角度 = 弧度 * 180 / Math.PI
向鼠标旋转(或向某点旋转):
// substitute mouseX, mouseY with the x, y point to rotate to
dx = mouseX - sprite.x;
dy = mouseY - sprite.y;
sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
创建波形:
// assign value to x, y or other property of sprite or movie clip,
// use as drawing coordinates, etc.
public function onEnterFrame(event:Event){
value = center + Math.sin(angle) * range;
angle += speed;
}
创建圆形:
// assign position to x and y of sprite or movie clip,
// use as drawing coordinates, etc.
public function onEnterFrame(event:Event){
xposition = centerX + Math.cos(angle) * radius;
yposition = centerY + Math.sin(angle) * radius;
angle += speed;
}
创建椭圆:
// assign position to x and y of sprite or movie clip,
// use as drawing coordinates, etc.
public function onEnterFrame(event:Event){
xposition = centerX + Math.cos(angle) * radiusX;
yposition = centerY + Math.sin(angle) * radiusY;
angle += speed;
}
计算两点间距离:
// points are x1, y1 and x2, y2
// can be sprite / movie clip positions, mouse coordinates, etc.
dx = x2 – x1;
dy = y2 – y1;
dist = Math.sqrt(dx*dx + dy*dy);
4.改变颜色colorTransform
package {
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.geom.ColorTransform;
public class TransformColor extends Sprite {
[Embed(source="picture.jpg")];
public var Picture:Class;
public function TransformColor() {
init();
}
private function init():void {
var pic:Bitmap=new Picture ;
addChild(pic);
pic.transform.colorTransform=new ColorTransform(-1,-1,1,1,
255,255,255,0);
}
}
}
测试这个影片时,请改变这行代码
[Embed(source="picture.jpg")]
请匹配这个路径为所使用图片的路径,如果在 Flash IDE 中编辑,只需要在库中导入这张图片,为 ActionScript 导出,并输入类名为 Picture。重要的一句是 ColorTransform 的设置。
//关于嵌入资源
虽然在有些情况下,在运行时读取资源很合适,但是在有些情况下有一些外部图形只想加载到 SWF 自里面。这时,如果使用 Flash IDE,可以简单地导入这个对象到库中并设置为“为 ActionScript 导出”。但在使用 Flex Builder 2 或 Flex 2 SDK 命令编译器时,没有库,那么如何在 SWF 中加载外部资源呢?
答案是使用[Embed]元数据(metadata)标签嵌入资源,元数据标签是指加到 ActionScript 文件中的非正式 ActionScript 语句。另外,它们指示编译器在编译过程中去做某种事情,[Embed]标签告诉编译器在最终的 SWF 文件中加载一个特殊的外部资源,资源可以是位图或外部 SWF 文件。告诉编译器要嵌入的资源所在的 source 路径的属性,如下:
[Embed(source="picture.jpg")]
在元数据语句的后面,直接声明一个 Class 类型的变量,如下:
[Embed(source="picture.jpg")]
private var Image:Class;
现在可以使用这个变量创建一个新的资源实例,如下:
var img:Bitmap = new Image();
注意创建的这个对象是 Bitmap 类型的。如果嵌入一个外部 SWF 文件,创建的这个对象应该是 Sprite 类型的,如下:
[Embed(source="animation.swf")]
private var Anim:Class;
var anim:Sprite = new Anim();
这里是一个在 SWF 中嵌入外部 JPEG 的例子:
package {
import flash.display.Sprite;
import flash.display.Bitmap;
public class EmbedAsset extends Sprite {
[Embed(source="picture.jpg")];
private var Image:Class;
public function EmbedAsset() {
init();
}
private function init():void {
var img:Bitmap = new Image();
addChild(img);
}
}
}
如果我们使用 Flash IDE ,只要将对象导入到库中并“为 ActionScript 导出”给出一个类名就可以了。不需要使用 [Embed] 元数据标签及类变量,事实上,Flash IDE 编译器甚至不支持 [Embed] 元数据标签。这里只作一个简单的介绍,因为在本书后面的内容中不会用到这个技术,但是很显然这是个非常有用的方法。
//相关公式
转换为十进制:
trace(hexValue);
十进制转换为十六进制:
trace(decimalValue.toString(16));
颜色合成:
color24 = red << 16 | green << 8 | blue;
color32 = alpha << 24 | red << 16 | green << 8 | blue;
颜色提取:
red = color24 >> 16;
green = color24 >> 8 & 0xFF;
blue = color24 & 0xFF;
alpha = color32 >> 24;
red = color32 >> 16 & 0xFF;
green = color32 >> 8 & 0xFF;
blue = color232 & 0xFF;
5.物体加速度
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Acceleration3 extends Sprite {
private var ball:Ball;
private var vx:Number=0;
private var vy:Number=0;
private var ax:Number=0;
private var ay:Number=0;
private var gravity:Number = 0.1; //重力加速度
public function Acceleration3() {
init();
}
private function init():void {
ball=new Ball ;
addChild(ball);
ball.x=stage.stageWidth / 2;
ball.y=stage.stageHeight / 2;
addEventListener(Event.ENTER_FRAME,onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
}
private function onKeyDown(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.LEFT :
ax=-0.2;
break;
case Keyboard.RIGHT :
ax=0.2;
break;
case Keyboard.UP :
ay=-0.2;
break;
case Keyboard.DOWN :
ay=0.2;
break;
default :
break;
}
}
private function onKeyUp(event:KeyboardEvent):void {
ax=0;
ay=0;
}
private function onEnterFrame(event:Event):void {
vx+= ax;
vy+= ay;
vy += gravity;
ball.x+= vx;
ball.y+= vy;
}
}
}
//相关公式
角速度转换为 x,y 速度向量:
vx = speed * Math.cos(angle);
vy = speed * Math.sin(angle);
角加速度(作用于物体上的 force )转换为 x,y 加速度:
ax = force * Math.cos(angle);
ay = force * Math.sin(angle);
将加速度加入速度向量:
vx += ax;
vy += ay;
将速度向量加入坐标:
movieclip._x += vx;
sprite.y += vy;
6.边界与摩擦力
移除对象
重置对象
屏幕环绕
浙公网安备 33010602011771号