Flash / Flex / ActionScript/Development/Drag Drop — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 09:19, 26 мая 2010
Drag and drop a Sprite
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width=550, height=400)]
public class Main extends Sprite {
private var _canvas:Sprite = new Sprite();
public function Main() {
_canvas
_canvas.graphics.beginFill(0xF0F0F0);
_canvas.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
_canvas.graphics.endFill();
_canvas.graphics.lineStyle(2, 0x000000);
addChild(_canvas);
_canvas.addEventListener(MouseEvent.MOUSE_DOWN, onCanvasMouseDown);
_canvas.addEventListener(MouseEvent.MOUSE_UP, onCanvasMouseUp);
}
private function onCanvasMouseDown(event:MouseEvent):void {
_canvas.graphics.moveTo(event.localX, event.localY);
_canvas.addEventListener(MouseEvent.MOUSE_MOVE, onCanvasMouseMove);
}
private function onCanvasMouseMove(event:MouseEvent):void {
_canvas.graphics.lineTo(event.localX, event.localY);
event.updateAfterEvent();
}
private function onCanvasMouseUp(event:MouseEvent):void {
_canvas.graphics.lineTo(event.localX, event.localY);
_canvas.removeEventListener(MouseEvent.MOUSE_MOVE, onCanvasMouseMove);
}
}
}
Dragging and Dropping
package{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var draggableSprite:Sprite = new Sprite();
private var droppableSprite:Sprite = new Sprite();
private var nonDroppableSprite:Sprite = new Sprite();
public function Main ()
{
draggableSprite.graphics.beginFill(0xff0000, 1);
draggableSprite.graphics.drawCircle(0, 0, 10);
draggableSprite.graphics.endFill();
droppableSprite.graphics.beginFill(0x0000ff, 1);
droppableSprite.graphics.drawRect(0, 0, 100, 100);
droppableSprite.graphics.endFill();
nonDroppableSprite.graphics.beginFill(0xff0000, 1);
nonDroppableSprite.graphics.drawRect(0, 0, 100, 100);
nonDroppableSprite.graphics.endFill();
addChild(nonDroppableSprite);
addChild(droppableSprite);
addChild(draggableSprite);
nonDroppableSprite.x = 200;
droppableSprite.x = 500;
nonDroppableSprite.y = 100;
droppableSprite.y = 100;
draggableSprite.y = 100;
draggableSprite.addEventListener(MouseEvent.MOUSE_DOWN,
startDragSprite);
draggableSprite.addEventListener(MouseEvent.MOUSE_UP,
stopDragSprite);
}
private function startDragSprite(mouseEvent:MouseEvent):void
{
(mouseEvent.target as Sprite).startDrag();
}
private function stopDragSprite(mouseEvent:MouseEvent):void
{
var target:Sprite = mouseEvent.target as Sprite;
if (target.dropTarget == droppableSprite)
{
target.stopDrag();
} else {
target.x = 0;
}
}
}
}
Dragging and Dropping Objects with the Mouse
package {
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.filters.DropShadowFilter;
public class Main extends Sprite {
private var _red:Sprite;
private var _green:Sprite;
private var _blue:Sprite;
private var _white:Sprite;
private var startingLocation:Point;
public function Main( ) {
_red = new Sprite( );
_red.graphics.beginFill( 0xFF0000 );
_red.graphics.drawRect( 0, 10, 10, 10 );
_red.graphics.endFill( );
_green = new Sprite( )
_green.graphics.beginFill( 0x00FF00 );
_green.graphics.drawRect( 0, 30, 10, 10 );
_green.graphics.endFill( );
_blue = new Sprite( );
_blue.graphics.beginFill( 0x0000FF );
_blue.graphics.drawRect( 0, 50, 10, 10 );
_blue.graphics.endFill( );
_white = new Sprite( );
_white.graphics.beginFill( 0xFFFFFF );
_white.graphics.drawRect( 20, 10, 50, 50 );
_white.graphics.endFill( );
addChild( _red );
addChild( _green );
addChild( _blue );
addChild( _white );
_red.addEventListener( MouseEvent.MOUSE_DOWN, pickup );
_red.addEventListener( MouseEvent.MOUSE_UP, place );
_green.addEventListener( MouseEvent.MOUSE_DOWN, pickup );
_green.addEventListener( MouseEvent.MOUSE_UP, place );
_blue.addEventListener( MouseEvent.MOUSE_DOWN, pickup );
_blue.addEventListener( MouseEvent.MOUSE_UP, place );
}
public function pickup( event:MouseEvent ):void {
startingLocation = new Point( );
startingLocation.x = event.target.x;
startingLocation.y = event.target.y;
event.target.startDrag( );
event.target.filters = [ new DropShadowFilter( ) ];
setChildIndex( DisplayObject( event.target ), numChildren - 1 );
}
public function place( event:MouseEvent ):void {
event.target.stopDrag( );
event.target.filters = null;
if ( event.target.dropTarget == _white ) {
var color:uint;
switch ( event.target ) {
case _red: color = 0xFF0000; break;
case _green: color = 0x00FF00; break;
case _blue: color = 0x0000FF; break;
}
_white.graphics.clear( );
_white.graphics.beginFill( color );
_white.graphics.drawRect( 20, 10, 50, 50 );
_white.graphics.endFill( );
}
event.target.x = startingLocation.x;
event.target.y = startingLocation.y;
}
}
}