Flash / Flex / ActionScript/Development/Sprite
Содержание
- 1 addChild( ) method doesn"t guarantee that a display object is added to the display list.
- 2 Adding an Item to the Display List
- 3 Add TextField to Sprite
- 4 Advanced Masks
- 5 Applies a black color transformation to group, causing all children to be colored solid black
- 6 Building an FLV Playback Application
- 7 Change child index
- 8 Containment Events
- 9 Depth test
- 10 Manipulating Objects in Containers Collectively
- 11 Masks
- 12 Moving Objects Forward and Backward
- 13 Removing an Item from the Display List
- 14 Reparenting Display Objects
- 15 Swapping the Depths of Children
- 16 Using hitTestPoint
- 17 Using the buttonMode of the Sprite
- 18 Using the hitArea
addChild( ) method doesn"t guarantee that a display object is added to the display list.
package {
import flash.display.*;
public class CircleExample extends Sprite {
public function CircleExample( ) {
var red:Shape = createCircle( 0xFF0000, 10 );
red.x = 10;
red.y = 20;
var green:Shape = createCircle( 0x00FF00, 10 );
green.x = 15;
green.y = 25;
var blue:Shape = createCircle( 0x0000FF, 10 );
blue.x = 20;
blue.y = 20;
addChild( red );
addChild( blue );
addChildAt( green, 1 );
}
public function createCircle( color:uint, radius:Number ):Shape {
var shape:Shape = new Shape( );
shape.graphics.beginFill( color );
shape.graphics.drawCircle( 0, 0, radius );
shape.graphics.endFill( );
return shape;
}
}
}
Adding an Item to the Display List
package {
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.text.TextField;
public class DisplayListExample extends Sprite {
public function DisplayListExample( ) {
var hello:TextField = new TextField( );
hello.text = "hello";
addChild( hello );
}
}
}
Add TextField to Sprite
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite {
public function Main( ) {
var field:TextField = new TextField( );
addChild(field);
}
}
}
Advanced Masks
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var maskingSprite:Sprite = new Sprite();
private var maskedSprite:Sprite = new Sprite();
private var maskHolder:Sprite = new Sprite();
public function Main()
{
stage.scaleMode = "noScale";
stage.align = "TL";
maskedSprite.graphics.beginFill(0xFFCC00, 1);
maskedSprite.graphics.drawRect(0, 0, 1000, 600);
maskedSprite.graphics.lineStyle(20, 0x000000);
maskedSprite.graphics.lineTo(1000, 600);
maskedSprite.graphics.moveTo(1000, 0);
maskedSprite.graphics.lineTo(0, 600);
addChild(maskedSprite);
maskHolder.graphics.beginFill(0x000000, 1);
maskHolder.graphics.drawRect(0, 0, 120, 120);
maskingSprite.graphics.beginFill(0x000000, 1);
maskingSprite.graphics.drawRect(0, 0, 100, 100);
maskingSprite.graphics.endFill();
addChild(maskingSprite);
maskedSprite.addEventListener(MouseEvent.MOUSE_DOWN, dragMask);
maskedSprite.addEventListener(MouseEvent.MOUSE_UP, stopDragMask);
maskedSprite.mask = maskingSprite;
}
private function dragMask(mouseEvent:MouseEvent):void
{
trace(" drag ");
maskingSprite.startDrag();
}
private function stopDragMask(mouseEvent:MouseEvent):void
{
maskingSprite.stopDrag();
}
}
}
Applies a black color transformation to group, causing all children to be colored solid black
package {
import flash.display.GradientType;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
public class Main extends Sprite {
public function Main() {
var rect1:Sprite = new Sprite( );
rect1.graphics.lineStyle(1);
rect1.graphics.beginFill(0x0000FF, 1);
rect1.graphics.drawRect(0, 0, 75, 50);
var rect2:Sprite = new Sprite( );
rect2.graphics.lineStyle(1);
rect2.graphics.beginFill(0xFF0000, 1);
rect2.graphics.drawRect(0, 0, 75, 50);
rect2.x = 50;
rect2.y = 75;
// Create the container
var group:Sprite = new Sprite( );
// Add the rectangles to the container
group.addChild(rect1);
group.addChild(rect2);
// Add the container to the main application
addChild(group);
group.x = 40;
group.scaleY = .15;
group.rotation = 15;
var blackTransform:ColorTransform = new ColorTransform( );
blackTransform.color = 0x0000FF;
group.transform.colorTransform = blackTransform;
}
}
}
Building an FLV Playback Application
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class Main extends Sprite
{
public static const SEEKING:String = "seeking";
public static const FINISHED_SEEKING:String = "finishedSeeking";
public var playhead:Sprite;
public function Main()
{
this.graphics.beginFill(0xCCCCCC, 1);
this.graphics.drawRect(0, 0, 100, 20);
playhead = new Sprite();
playhead.graphics.beginFill(0x0000ff, 1);
playhead.graphics.drawRect(0, 0, 20, 20);
addChild(playhead);
playhead.x = -10;
playhead.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
}
private function beginDrag(mouseEvent:MouseEvent):void
{
playhead.startDrag(false, new Rectangle(0, 0, width, height));
playhead.addEventListener(MouseEvent.MOUSE_UP, stopDragPlayhead);
var event:Event = new Event("seeking");
dispatchEvent(event);
}
private function stopDragPlayhead(mouseEvent:MouseEvent):void
{
playhead.stopDrag();
var event:Event = new Event("finishedSeeking");
dispatchEvent(event);
}
public function updatePlayhead(number:Number):void
{
playhead.x = number;
}
}
}
Change child index
package {
import flash.display.*;
public class Main extends Sprite {
public function Main( ) {
var green:Shape = createCircle( 0x00FF00, 10 );
green.x = 25;
green.y = 25;
var blue:Shape = createCircle( 0x0000FF, 20 );
blue.x = 25;
blue.y = 25;
addChild( green );
addChild( blue );
setChildIndex( blue, getChildIndex( green ) );
}
public function createCircle( color:uint, radius:Number ):Shape {
var shape:Shape = new Shape( );
shape.graphics.beginFill( color );
shape.graphics.drawCircle( 0, 0, radius );
shape.graphics.endFill( );
return shape;
}
}
}
Containment Events
package {
import flash.display.*;
import flash.events.*;
public class Main extends Sprite {
public function Main() {
var container:Sprite = new Sprite( );
var child:Sprite = new Sprite( );
var grandchild:Sprite = new Sprite( );
container.addEventListener(Event.ADDED, addedListener);
container.addEventListener(Event.REMOVED, removedListener);
container.addChild(child);
child.addChild(grandchild);
stage.addChild(container);
child.removeChild(grandchild);
stage.removeChild(container);
}
private function addedListener (e:Event):void {
if (e.eventPhase != EventPhase.AT_TARGET) {
trace("container has a new descendant: " + e.target);
} else {
trace("container was added to a new parent: "
+ DisplayObject(e.target).parent);
}
}
private function removedListener (e:Event):void {
if (e.eventPhase != EventPhase.AT_TARGET) {
trace("a descendant was removed from container: " + e.target);
} else {
trace("container was removed from its parent: "
+ DisplayObject(e.target).parent);
}
}
}
}
Depth test
package {
import flash.display.BlendMode;
import flash.display.Sprite;
[SWF(width=550, height=400)]
public class Main extends Sprite {
public function Main() {
var square:Square = new Square();
addChild(square);
square.x = 10;
square.y = 10;
var square2:Square = new Square();
addChild(square2);
square2.x = 43;
square2.y = 66;
var square3:Square = new Square();
addChild(square3);
square3.x = 93;
square3.y = 31;
trace("square: " + getChildIndex(square));
trace("square2: " + getChildIndex(square2));
trace("square3: " + getChildIndex(square3));
setChildIndex(square, numChildren-1);
trace("square: " + getChildIndex(square));
trace("square2: " + getChildIndex(square2));
trace("square3: " + getChildIndex(square3));
swapChildren(square2, square3);
swapChildrenAt(0, 2);
trace("square: " + getChildIndex(square));
trace("square2: " + getChildIndex(square2));
trace("square3: " + getChildIndex(square3));
square3.blendMode = BlendMode.INVERT;
}
}
}
class Square extends flash.display.Sprite {
public function Square() {
graphics.lineStyle(5);
graphics.beginFill(0xFF);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
}
}
Manipulating Objects in Containers Collectively
package {
import flash.display.GradientType;
import flash.display.Sprite;
import flash.geom.Matrix;
public class Main extends Sprite {
public function Main() {
var rect1:Sprite = new Sprite( );
rect1.graphics.lineStyle(1);
rect1.graphics.beginFill(0x0000FF, 1);
rect1.graphics.drawRect(0, 0, 75, 50);
var rect2:Sprite = new Sprite( );
rect2.graphics.lineStyle(1);
rect2.graphics.beginFill(0xFF0000, 1);
rect2.graphics.drawRect(0, 0, 75, 50);
rect2.x = 50;
rect2.y = 75;
var group:Sprite = new Sprite( );
group.addChild(rect1);
group.addChild(rect2);
addChild(group);
group.x = 40;
group.scaleY = .15;
group.rotation = 15;
}
}
}
Masks
package
{
import flash.display.*;
import flash.events.Event;
public class Main extends Sprite
{
private var circle:Shape;
private var vBox:Shape;
private var up:Boolean = false;
public function Main()
{
super();
stage.scaleMode = "noScale";
circle = new Shape();
circle.graphics.beginFill(0xFF6600, 1);
circle.graphics.drawCircle(250, 250, 250);
vBox = new Shape();
vBox.graphics.beginFill(0x000000, 1);
vBox.graphics.drawRect(0, 0, 1000, 20);
circle.mask = vBox;
addChild(vBox);
addChild(circle);
addEventListener(Event.ENTER_FRAME, scrollVertBox);
}
private function scrollVertBox(event:Event):void
{
if(up)
{
vBox.y -= 2;
} else {
vBox.y += 2;
}
if(vBox.y > 520)
{
up = true;
}
if(vBox.y < 0)
{
up = true;
}
}
}
}
Moving Objects Forward and Backward
package {
import flash.display.*;
public class SetChildIndexExample extends Sprite {
public function SetChildIndexExample( ) {
var red:Shape = createCircle( 0xFF0000, 10 );
red.x = 10;
red.y = 20;
var green:Shape = createCircle( 0x00FF00, 10 );
green.x = 15;
green.y = 25;
var blue:Shape = createCircle( 0x0000FF, 10 );
blue.x = 20;
blue.y = 20;
addChild( red );
addChild( green );
addChild( blue );
setChildIndex( blue, 0 );
}
public function createCircle( color:uint, radius:Number ):Shape {
var shape:Shape = new Shape( );
shape.graphics.beginFill( color );
shape.graphics.drawCircle( 0, 0, radius );
shape.graphics.endFill( );
return shape;
}
}
}
Removing an Item from the Display List
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class RemoveChildExample extends Sprite {
private var _label:TextField;
public function RemoveChildExample( ) {
_label = new TextField( );
_label.text = "Some Text";
addChild( _label );
stage.addEventListener( MouseEvent.CLICK, removeLabel );
}
public function removeLabel( event:MouseEvent ):void {
removeChild( _label );
}
}
}
Reparenting Display Objects
package {
import flash.display.*;
import flash.events.*;
public class Main extends Sprite
{
var squareOne:Sprite = new Sprite();
var squareTwo:Sprite = new Sprite();
var shapeInst:Shape = new Shape();
public function Main()
{
squareOne.graphics.beginFill(0x00ff00, 1);
squareOne.graphics.drawRect(0, 0, 200, 200);
squareOne.graphics.endFill();
squareTwo.graphics.beginFill(0x00ff00, 1);
squareTwo.graphics.drawRect(0, 0, 200, 200);
squareTwo.graphics.endFill();
addChild(squareOne);
addChild(squareTwo);
squareTwo.x = 300;
squareOne.addEventListener(MouseEvent.MOUSE_DOWN, addShape);
squareTwo.addEventListener(MouseEvent.MOUSE_DOWN, addShape);
}
private function addShape(event:Event):void
{
event.target.addChild(shapeInst);
}
}
}
Swapping the Depths of Children
package
{
import flash.display.Sprite;
import flash.events.*;
public class Main extends Sprite
{
public function Main()
{
var aSpr:Sprite = new Sprite();
var bSpr:Sprite = new Sprite();
var cSpr:Sprite = new Sprite();
var dSpr:Sprite = new Sprite();
aSpr.addEventListener(MouseEvent.MOUSE_DOWN, swapSprites);
bSpr.addEventListener(MouseEvent.MOUSE_DOWN, swapSprites);
cSpr.addEventListener(MouseEvent.MOUSE_DOWN, swapSprites);
dSpr.addEventListener(MouseEvent.MOUSE_DOWN, swapSprites);
addChild(aSpr);
addChild(bSpr);
addChild(cSpr);
addChild(dSpr);
}
private function swapSprites(evt:Event):void
{
swapChildren((evt.target as Sprite), getChildAt(numChildren - 1));
}
}
}
Using hitTestPoint
package{
import flash.display.*;
import flash.geom.*;
public class Main extends Sprite{
public function Main(){
var pt:Point = new Point(20, 30);
var temp:Sprite = new Sprite();
temp.graphics.beginFill(0x00ff00, 1.0);
temp.graphics.drawRect(0, 0, 30, 30);
temp.graphics.endFill();
addChild(temp);
if (temp.hitTestPoint(pt.x, pt.y))
{
trace("Point within bounds");
}
}
}
}
Using the buttonMode of the Sprite
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var foo:Sprite = new Sprite();
foo.graphics.beginFill(0xff0000, 1);
foo.graphics.drawRect(0, 0, 100, 100);
foo.graphics.endFill();
foo.buttonMode = true;
addChild(foo);
}
}
}
Using the hitArea
package
{
import flash.display.*;
import flash.events.*;
public class Main extends Sprite
{
public function Main()
{
var notHitArea:Sprite = new Sprite();
notHitArea.graphics.beginFill(0x00FF00, 1.0);
notHitArea.graphics.drawRect(0, 0, 30, 30);
notHitArea.graphics.endFill();
addChild(notHitArea);
notHitArea.x = 100;
notHitArea.y = 200;
var hitAreaSprite:Sprite = new Sprite();
hitAreaSprite.graphics.beginFill(0x0000FF, 1.0);
hitAreaSprite.graphics.drawRect(0, 0, 30, 30);
hitAreaSprite.graphics.endFill();
addChild(hitAreaSprite);
notHitArea.hitArea = hitAreaSprite;
hitAreaSprite.mouseEnabled = false;
notHitArea.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
}
private function clickHandler(mouseEvent:MouseEvent):void
{
trace(" clickHandler ");
}
}
}