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

  发布时间:2014-06-13 09:22:15   作者:佚名   我要评论
本教程主要用Flash类来制作一个特效,要用到鼠标操作元件,放大、缩小、旋转、移动。花了一些时间,终于实现了,代码整理了一下,和大家分享一下。如有不足之处,希望大家提出意见
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本教程主要用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;
}
}
}

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

相关文章

  • flash如何制作形状补间动画?flash创建补间形状技巧

    flash一款功能强大的动画制作软件,制作动画很非常的方便,但动画过程使用的都是形状补间功能。今天小编就来给大家介绍一下flash形状补间动画制作方法,感兴趣的快来看看吧
    2022-05-11
  • flash cs6怎么做花朵成长的逐帧动画?

    flash cs6怎么做花朵成长的逐帧动画?flash中想要绘制一个花开的动画,该怎么画出各个图形然后做逐帧动画呢?下面我们就来看看详细的教程,需要的朋友可以参考下
    2021-03-09
  • Flash怎么将动画复制为ActionScript3.0并使用?

    Flash怎么将动画复制为ActionScript3.0并使用?Flash中制作的动画想要直接粘贴到另一个对象中,我们可以使用复制脚本代码的方式复制动画,下面我们就来看看详细的教程,需
    2017-12-08
  • flash怎么制作漫画人物行走的动画?

    flash怎么制作漫画人物行走的动画?flash中想要制作一段行走的动画,该怎么设计呢?下面我们就来看看详细的教程,很简单,需要的朋友可以参考下
    2017-04-18
  • flash逐帧动画制作全过程解析

    这篇教程是向程序员之家的朋友分享flash逐帧动画制作全过程,教程非常基础,很适合新手来学习,推荐过来,有兴趣的朋友可以过来学习
    2016-01-15
  • Flash正确的口型吻合动画技巧

    下面小编就为大家介绍利用Flash正确的口型吻合动画技巧,教程很不错,非常适合新手来学习,推荐到程序员之家,喜欢的朋友一起来学习吧
    2015-01-15
  • Flash动画制作技巧 图形元件相关知识介绍

    这篇教程是向程序员之家的朋友介绍Flash动画制作技巧:图形元件相关知识,教程很基础,但很实用,推荐到程序员之家,喜欢的朋友可以过来学习一下,希望能对大家有所帮助
    2015-01-05
  • Flash动画制作小黑人经典动画效果技巧介绍(图文)

    本教程是向大家介绍Flash动画制作小黑人经典动画效果技巧,教程很经典,介绍的非常详细,相信对学习Flash朋友有一定的帮助。转发过来,希望对大家有所帮助
    2014-08-05
  • Flash制作旋转的3D立体盒动画教程

    本教程是向大家介绍利用Flash制作旋转的3D立体盒动画教程,制作出来的效果非常好看,教程介绍的很详细,希望大家通过本篇教程能学习Flash制作3D动画的方法,这样以后就可以
    2014-07-22
  • Flash cs3制作人物行走动画技巧图文解析

    本教程向大家介绍了Flash cs3制作人物行走动画技巧,并做了详细的介绍,觉得教程很不错,转发过来,希望给朋友们带来帮助
    2014-07-15

最新评论

?


http://www.vxiaotou.com