欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-10-10 15:39:15
本教程主要用Flash类来制作一个特效,要用到鼠标操作元件,放大、缩小、旋转、移动。花了一些时间,终于实现了,代码整理了一下,和大家分享一下。如有不足之处,希望大家提出意见... 14-06-13...

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

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