如何使用as3用鼠标创建精灵的上/下移动

发布于 2024-08-24 22:13:18 字数 45 浏览 8 评论 0原文

我只需要在鼠标移动时垂直移动精灵。如何用as3实现它?

谢谢

I need to move a sprite only vertically on mouse move. How do I implement it with as3?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和影子一齐双人舞 2024-08-31 22:13:18

Flash 版本

var s:Sprite = new Sprite();
s.x = 20;
s.graphics.beginFill(0xFF0000);
s.graphics.drawRect(0,0,20,20);
addChild(s);

stage.addEventListener(MouseEvent.MOUSE_MOVE,moveSprite);

function moveSprite(e:MouseEvent):void
{
    s.y = e.localY;
}

Flex 版本

<mx:Canvas width="100" height="100">
            <mx:mouseMove>
                    <![CDATA[
                        s.y = event.localY;
                    ]]>
                </mx:mouseMove>
            <mx:Canvas id="s" backgroundColor="#ff0000" width="20" height="20"/>
        </mx:Canvas>

这些中的每一个您都可以粘贴并按照您所说的进行操作。它将创建一个 20x20 的红色框,其垂直方向与鼠标相同,但水平方向固定。您的鼠标的 Flex 版本必须位于包含的画布内。

Flash version

var s:Sprite = new Sprite();
s.x = 20;
s.graphics.beginFill(0xFF0000);
s.graphics.drawRect(0,0,20,20);
addChild(s);

stage.addEventListener(MouseEvent.MOUSE_MOVE,moveSprite);

function moveSprite(e:MouseEvent):void
{
    s.y = e.localY;
}

Flex version

<mx:Canvas width="100" height="100">
            <mx:mouseMove>
                    <![CDATA[
                        s.y = event.localY;
                    ]]>
                </mx:mouseMove>
            <mx:Canvas id="s" backgroundColor="#ff0000" width="20" height="20"/>
        </mx:Canvas>

Each of these you can paste in and will do what you said. it will create a 20x20 red box that is vertically the same as the mouse but fixed horizontally. The flex version your mouse has to be within the containing Canvas.

蝶舞 2024-08-31 22:13:18
addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(e:MouseEvent):void{
    mySprite.y += amount;
}
addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(e:MouseEvent):void{
    mySprite.y += amount;
}
耀眼的星火 2024-08-31 22:13:18

好吧,拖动有点复杂。您需要为拖动的边界定义一个矩形。如果您只想沿一个轴拖动,则可以使矩形的宽度为 0。在本示例中,我将滚动量和向下滚动量限制为不同的数字,您可以在下面更改这些数字。

import flash.events.MouseEvent;
import flash.geom.Rectangle;

mySprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void{
    stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    var scrollUpAmount:int = 10;
    var scrollDownAmount:int = 200;
    var boundsRect:Rectangle = new Rectangle(mySprite.x,mySprite.y-scrollUpAmount,0,mySprite.y+scrollDownAmount);
    mySprite.startDrag(false, boundsRect);
}

function mouseUpHandler(event:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    mySprite.stopDrag();
}

Ok, dragging is a little more complicated. You need to define a rectangle for the bounds of the dragging. If you want to just drag along one axis then you make the rectangle have a width of 0. In this example I've restricted the amount of scrolling and and down to different numbers that you can change below.

import flash.events.MouseEvent;
import flash.geom.Rectangle;

mySprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void{
    stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    var scrollUpAmount:int = 10;
    var scrollDownAmount:int = 200;
    var boundsRect:Rectangle = new Rectangle(mySprite.x,mySprite.y-scrollUpAmount,0,mySprite.y+scrollDownAmount);
    mySprite.startDrag(false, boundsRect);
}

function mouseUpHandler(event:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    mySprite.stopDrag();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文