热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在轴(x和y)中拖动对象,但不在as3中对角拖动-Howtodragobjectsinaxis(bothxandy)butnotdiagonallyinas3

IfthemouseisdraggedinxaxisthenobjectshouldmoveinxaxisIfthemouseisdraggedinyaxi

If the mouse is dragged in x axis then object should move in x axis If the mouse is dragged in y axis then object should move in y axis Object should not move diagonally

如果在x轴上拖动鼠标,则对象应在x轴上移动如果在y轴上拖动鼠标,则对象应在y轴上移动对象不应在对角线上移动

2 个解决方案

#1


1  

It's impossible to do it. When your mouse is going to move diagonally, what will you do? An alternative could be something like that, where your object moves along the speediest axis:

这是不可能的。当你的鼠标要对角移动时,你会怎么做?替代方案可能是这样的,您的对象沿着最快的轴移动:

var oldx:int;
var oldy:int;
var sx:int;
var sy:int;

function run(e:Event):void
{
    sx = Math.abs(mouseX - oldx);
    sy = Math.abs(mouseY - oldy);

    if (sx > sy) {
        c.x = mouseX;
    } else {
        c.y = mouseY;
    }

    oldx = mouseX;
    oldy = mouseY;
}

stage.addEventListener(Event.ENTER_FRAME, run);

Other solution

Another alternative consists in calculating the angle of your mouse:

另一种方法是计算鼠标的角度:

var oldx:int;
var oldy:int;
var rd:Number;
var l1:Number = Math.PI / 4;
var l2:Number = 3 * Math.PI / 4;

function run2(e:Event):void
{
    rd = Math.atan2(mouseX - oldx, mouseY - oldy);

    if ((rd > l1 && rd  -l2)) {
        c.x = mouseX;
    } else {
        c.y = mouseY;
    }

    oldx = mouseX;
    oldy = mouseY;

}
stage.addEventListener(Event.ENTER_FRAME, run2);

#2


0  

import flash.geom.Rectangle;
import flash.events.MouseEvent;
var tempx:Number=0;
var tempy:Number=0;
var count:int=0;
stage.addEventListener(MouseEvent.MOUSE_UP,remov)
bl.addEventListener(MouseEvent.MOUSE_DOWN,func)
function func  (f:MouseEvent)
{
    tempx=Math.abs(stage.mouseX);
    tempy=Math.abs(stage.mouseY);
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mov)
}
function mov(h:MouseEvent)
{
    count+=1;
    if (count==4)
    {
    count=0;
    var Xval:Number=tempx-stage.mouseX;
    var Yval:Number=tempy-stage.mouseY;
    if(Math.abs(Xval)>Math.abs(Yval))
    {
    bl.startDrag(false,new Rectangle(0,bl.y,stage.stageWidth,0))
    trace ("X dragg")
    trace(Xval+""+""+Yval)
    }
    else
    {

        trace ("YY drag")
        trace(Xval+""+""+Yval)
        bl.startDrag(false,new Rectangle(bl.x,0,0,stage.stageHeight))
    }
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,mov)
    }
}
function remov(y:MouseEvent)
{
    bl.stopDrag();
}

bl is the mc name this works fine for me....Moving either in x or y axis alone....

bl是mc名称这对我来说很好....单独在x或y轴移动....


推荐阅读
author-avatar
小破皮2011_292
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有