Flash类的运用:可放大、缩小、旋转的鼠标操作类

网站建设 2021-07-03 14:34www.dzhlxh.cn网站建设

本教程主要用Flash类来制作一个特效,要用到鼠标操作元件,放大、缩小、旋转、移动。花了一些时间,终于实现了,代码整理了一下,和大家分享一下。希望大家提出意见,修改。废话少说,先看效果:

操作类:

复制代码
代码如下:
package com.mygamemylove{
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
public class opObject {
private var spOp:Sprite;
private var currentOp:Sprite;
private var arrPoint:Array;
//
private var isDrogOp:Boolean;
private var isDrog:Boolean;
private var pointType:uint;
private var spPoint:Sprite;
//
private var sizeType:Number;
private var minW:Number;
private var minH:Number;
private var drawC:uint;
private var spParent:Sprite;
//旋转
private var isR:Boolean
public var nowRotation:Number;
public var pCent:Point;
public var spRotationPoint:Sprite;
public var spCenterPoint:Sprite;
public var numOpPointWH:uint;

/**
* 操作一个对象,可以放大缩小,旋转。
*原创flash代码,尽在自娱自乐 www.MyGameMyLove.com
*smallerbird smallerbird@gmail.com 2009-9-12
*
*
* @spParent:Sprite 操作对象的父对象
* @currentOp:Sprite 其中操作的一个对象
* @sizeType:Number 调整尺寸的显示模式。
* @minW:Number 缩小最小尺寸宽
* @minH:Number 缩小最小尺寸高
* @drawC:Number 操作柄的色
* @numOpPointWH:Number 操作柄的宽高
* */
public function opObject(spParent:Sprite,currentOp:Sprite, sizeType:Number=1,
minW:Number=10, minH:Number=10, drawC:Number=0xff0000, numOpPointWH:uint=10){

this.numOpPointWH=numOpPointWH;
this.currentOp=currentOp
this.spParent=spParent;
this.spParent.mouseEnabled=false;
this.sizeType=sizeType;
this.minW=minW;
this.minH=minH;
isDrogOp=false;
isDrog=false;
pointType=0;
arrPoint=new Array();
nowRotation=0;
isR=true
}
源代码下载地址:http://www.mygamemylove.com/bbs/viewthread.php?tid=48
//改变注册点
public static function RegPoint($obj:Sprite, $point:Point):void {
var tmp_point:Point=$obj.parent.globalToLocal($obj.localToGlobal($point));
var len:int=$obj.numChildren;
while (len--) {
var tmp_obj:DisplayObject=$obj.getChildAt(len);
tmp_obj.x-=$point.x;
tmp_obj.y-=$point.y;
}
$obj.x=tmp_point.x;
$obj.y=tmp_point.y;
}
private function drawR(g:Graphics, c:uint, x:Number, y:Number, w:Number, h:Number):void {
g.beginFill(c, 0.5);
g.drawRoundRect(x, y, w, h, 5);
g.endFill();
}
private function drawOpPont(sp:Sprite, x:Number, y:Number, c:uint, w:Number, h:Number):void {
var spTem:Sprite=new Sprite();
spTem.x=x;
spTem.y=y;
drawR(spTem.graphics, c, -w / 2, -h / 2, w, h);
sp.addChild(spTem);
arrPoint.push(spTem);
}
//
private function clrPointSize():void {
if (arrPoint.length != 0) {
for (var i:uint=0; i < arrPoint.length; i++) {
arrPoint[i].removeEventListener(MouseEvent.MOUSE_DOWN, fun_point_down);
arrPoint[i].removeEventListener(MouseEvent.MOUSE_UP, fun_point_up);
spParent.removeChild(arrPoint[i]);
}
arrPoint=new Array();
}
spParent.graphics.clear();
}
//
private function clrPoint():void {
clrPointSize();
if (spCenterPoint != null) {
spParent.removeChild(spCenterPoint);
spCenterPoint=null;
spParent.removeChild(spRotationPoint);
}
}
//
private function showOp4point(sp:Sprite):void {
//
clrPoint();
var r:Rectangle=sp.getRect(spParent);
//
var x1:Number=r.x;
var y1:Number=r.y;
var w1:Number=r.width;
var h1:Number=r.height;
var w2:Number=w1 / 2;
var h2:Number=h1 / 2;
//
var c:uint=drawC;
var p_tem:Sprite=spParent;
//
pCent=new Point(x1 + w2, y1 + h2);
//
drawOpPont(p_tem, x1, y1, c, numOpPointWH, numOpPointWH);
drawOpPont(p_tem, x1 + w2, y1, c, numOpPointWH, numOpPointWH);
drawOpPont(p_tem, x1 + w1, y1, c, numOpPointWH, numOpPointWH);
//
drawOpPont(p_tem, x1 + w1, y1 + h2, c, numOpPointWH, numOpPointWH);
drawOpPont(p_tem, x1 + w1, y1 + h1, c, numOpPointWH, numOpPointWH);
//
drawOpPont(p_tem, x1 + w2, y1 + h1, c, numOpPointWH, numOpPointWH);
drawOpPont(p_tem, x1, y1 + h1, c, numOpPointWH, numOpPointWH);
drawOpPont(p_tem, x1, y1 + h2, c, numOpPointWH, numOpPointWH);
for (var i:uint=0; i < arrPoint.length; i++) {
arrPoint[i].addEventListener(MouseEvent.MOUSE_DOWN, fun_point_down);
arrPoint[i].addEventListener(MouseEvent.MOUSE_UP, fun_point_up);
}
//
//画连线
var gTem:Graphics=p_tem.graphics;
gTem.clear();
gTem.lineStyle(1, c, 0.5);
gTem.moveTo(arrPoint[0].x, arrPoint[0].y);
for (i=1; i < arrPoint.length; i++) {
gTem.lineTo(arrPoint[i].x, arrPoint[i].y);
}
gTem.lineTo(arrPoint[0].x, arrPoint[0].y);
gTem.lineTo(arrPoint[4].x, arrPoint[4].y);
gTem.moveTo(arrPoint[6].x, arrPoint[6].y);
gTem.lineTo(arrPoint[2].x, arrPoint[2].y);
//
gTem.moveTo(arrPoint[1].x, arrPoint[1].y);
gTem.lineTo(arrPoint[5].x, arrPoint[5].y);
//
gTem.moveTo(arrPoint[7].x, arrPoint[7].y);
gTem.lineTo(arrPoint[3].x, arrPoint[3].y);
//画旋转的点
//中心点
if(isR){
spCenterPoint=new Sprite();
spCenterPoint.mouseEnabled=false;
spCenterPoint.graphics.beginFill(0xff0000, 0.5);
spCenterPoint.graphics.drawCircle(0, 0, numOpPointWH / 2);
spCenterPoint.graphics.endFill();
spCenterPoint.x=pCent.x;
spCenterPoint.y=pCent.y;
var pTem:Point=currentOp.globalToLocal(pCent);
spParent.addChild(spCenterPoint);
//旋转控制点
spRotationPoint=new Sprite();
spRotationPoint.graphics.beginFill(0xff0000, 0.5);
spRotationPoint.graphics.drawCircle(0, 0, numOpPointWH / 2);
spRotationPoint.graphics.endFill();
spRotationPoint.x=x1 - numOpPointWH;
spRotationPoint.y=y1 - numOpPointWH;
spParent.addChild(spRotationPoint);
spRotationPoint.addEventListener(MouseEvent.MOUSE_DOWN, fun_point_down);
spRotationPoint.addEventListener(MouseEvent.MOUSE_UP, fun_point_up);
}
//
}
public function fun_opUp(e:MouseEvent):void {
isDrogOp=false;
currentOp.stopDrag();
showOp4point(currentOp);
}
//如果isR=false 不可以进行旋转操作
public function fun_opDown(e:MouseEvent,isR:Boolean=true):void {
this.isR=isR
currentOp=e.target as Sprite;
showOp4point(currentOp);
//
currentOp.startDrag();
isDrogOp=true;
}
public function fun_over(e:MouseEvent):void {
var spTem:Sprite=e.target as Sprite;
}
//
//不能越过边界
public function noMoveBorder(rBorder:Rectangle):uint {
var r:Rectangle = currentOp.getBounds(spParent)
var numOffsetTem:Number=10
if(rBorder.width-numOffsetTem<r.width){
currentOp.width=rBorder.width-numOffsetTem
return 0
}
if(rBorder.height-numOffsetTem<r.height){
currentOp.height=rBorder.height-numOffsetTem
return 0
}
//trace(currentOp);
if (r.x<rBorder.x) {
opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent));
no();
currentOp.x=rBorder.x+r.width/2;
}
if (r.y<rBorder.y) {
opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent));
no();
currentOp.y=rBorder.y+r.height/2;
}
if (r.x+r.width>rBorder.x+rBorder.width) {
opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent));
no();
currentOp.x=rBorder.x+rBorder.width-r.width/2;
}
if (r.y+r.height>rBorder.y+rBorder.height) {
opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent));
no();
currentOp.y=rBorder.y+rBorder.height-r.height/2;
}
return 0
}
//取消的所有动作
public function no():void {
isDrog=false;
clrPoint();
if (spPoint) {
spPoint.stopDrag();
}
currentOp.stopDrag();
}
//重新设置注册点
private function seCentXY():void {
var pTem:Point;
switch (pointType) {
case 1 :
pTem=currentOp.globalToLocal(new Point(arrPoint[4].x, arrPoint[4].y));
break;
case 2 :
pTem=currentOp.globalToLocal(new Point(arrPoint[5].x, arrPoint[5].y));
break;
case 3 :
pTem=currentOp.globalToLocal(new Point(arrPoint[6].x, arrPoint[6].y));
break;
case 4 :
pTem=currentOp.globalToLocal(new Point(arrPoint[7].x, arrPoint[7].y));
break;
case 5 :
pTem=currentOp.globalToLocal(new Point(arrPoint[0].x, arrPoint[0].y));
break;
case 6 :
pTem=currentOp.globalToLocal(new Point(arrPoint[1].x, arrPoint[1].y));
break;
case 7 :
pTem=currentOp.globalToLocal(new Point(arrPoint[2].x, arrPoint[2].y));
break;
case 8 :
pTem=currentOp.globalToLocal(new Point(arrPoint[3].x, arrPoint[3].y));
break;
}
opObject.RegPoint(currentOp, pTem);
}
private function fun_point_down(e:MouseEvent):void {
//
var spTem:Sprite=e.target as Sprite;
spPoint=spTem;
if (spRotationPoint == spPoint) {
var dx:Number=currentOp.parent.mouseX - pCent.x;
var dy:Number=currentOp.parent.mouseY - pCent.y;
nowRotation-=(Math.atan2(dy, dx) * 180 / Math.PI);
var pTem:Point=currentOp.globalToLocal(pCent);
opObject.RegPoint(currentOp, pTem);
spTem.alpha=0;
clrPointSize();
} else {
pointType=0;
for (var i:uint=0; i < arrPoint.length; i++) {
if (arrPoint[i] == spTem) {
pointType=i + 1;
break;
}
}
seCentXY();
}
isDrog=true;
spTem.startDrag(true);
}
private function fun_point_up(e:MouseEvent):void {
nowRotation=currentOp.rotation;
clrPoint();
}
private function isUpObj(sp:Sprite):Boolean {
var isRe:Boolean=false;
if (currentOp == sp) {
isRe=true;
} else {
for (var i:uint=0; i < arrPoint.length; i++) {
if (arrPoint[i] == sp) {
isRe=true;
break;
}
}
//
if (spRotationPoint == sp) {
isRe=true;
}
}
return isRe;
}
public function fun_Mouse_up(e:MouseEvent):void {
isDrog=false;
if (!isUpObj(e.target as Sprite)) {
clrPoint();
} else {
if (spPoint) {
spPoint.stopDrag();
}
}
}

public function fun_onEnterFrame(e:Event):Boolean {
if (isDrogOp) {
showOp4point(currentOp);
}
if (!isDrog) {
return false;
}
var spTem:Sprite=spPoint;
var dx:Number, dy:Number;
//旋转
if (spRotationPoint == spPoint) {
dx=currentOp.parent.mouseX - pCent.x;
dy=currentOp.parent.mouseY - pCent.y;
currentOp.rotation=(Math.atan2(dy, dx) * 180 / Math.PI) + nowRotation;
return true;
}
//放大///////////////
switch (pointType) {
case 1 :
dx=arrPoint[4].x - spTem.x;
dy=arrPoint[4].y - spTem.y;
break;
case 2 :
dx=0;
dy=arrPoint[5].y - spTem.y;
break;
case 3 :
dx=spTem.x - arrPoint[6].x;
dy=arrPoint[6].y - spTem.y;
break;
case 4 :
dx=spTem.x - arrPoint[7].x;
dy=0;
break;
case 5 :
dx=spTem.x - arrPoint[0].x;
dy=spTem.y - arrPoint[0].y;
break;
case 6 :
dx=0;
dy=spTem.y - arrPoint[1].y;
break;
case 7 :
dx=arrPoint[2].x - spTem.x;
dy=spTem.y - arrPoint[2].y;
break;
case 8 :
dx=arrPoint[3].x - spTem.x;
dy=0;
break;
}
if (dx > minW) {
currentOp.width=dx;
}
if (dy > minH) {
currentOp.height=dy;
}
showOp4point(currentOp);
return true;
}
}
}

以上就是可放大、缩小、旋转的鼠标操作类代码的整理,希望对大家有一定的帮助!

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板