【问题标题】:Make an object snap to another ojbect, then follow its path with pure ActionScript?让一个对象捕捉到另一个对象,然后使用纯 ActionScript 跟随它的路径?
【发布时间】:2011-04-22 15:54:37
【问题描述】:

我仍在尝试掌握如何让一个对象捕捉到另一个对象,然后使用纯 ActionScript 跟随它的路径(将一个箭头对象捕捉到一个圆圈,然后当播放按钮时圆圈跟随箭头的方向打)。

有人可以帮我举一个小例子,这样我就可以理解它了,任何帮助都将不胜感激。我正在尝试创建一个针对此类的应用程序

   http://itunes.apple.com/us/app/basketball-coachs-clipboard/id317785081?mt=8

我的画线已经开始工作,但现在知道如何让对象跟随线,这是我在舞台上画线的方法。请你给我一个如何做到这一点的线索。

   function startPencilTool(e:MouseEvent):void
    {

    pencilDraw = new Shape(); 
    board.addChild(pencilDraw); 

    pencilDraw.graphics.moveTo(mouseX, mouseY); 
    pencilDraw.graphics.lineStyle(shapeSize.width);
   board.addEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); 
   }

 function drawPencilTool(e:MouseEvent):void
 {
 pencilDraw.graphics.lineTo(mouseX, mouseY); /
 }

 function stopPencilTool(e:MouseEvent):void
 {
 board.removeEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); 
 }

【问题讨论】:

    标签: actionscript-3 object flash-cs5 playback


    【解决方案1】:

    第一个

    如果您的意思是“跟随其路径”,即该对象跟随另一个对象,那么只需这样做

    obj2.x = obj1.x;
    obj2.y = obj1.y;
    

    遵循确切的坐标。如果你想拉开它们之间的距离,那么

    obj2.x = obj1.x + dx;
    obj2.y = obj1.y + dy;
    

    根据自己的意愿选择 dx 和 dy。

    第二次

    如果你想做一个应用程序,你可以“画一个箭头”或“画一个路径”,然后一个对象应该跟随它,那么你可以尝试存储鼠标的坐标,同时“画箭头” ",然后将您想要的对象捕捉到这些坐标。

    var coordinates:Array = [];
    
    stage.addEventListener("mouseDown", md);
    
    function md(evt:*):void
    {
        //empty the coordinates
        coordinates = [];
    
        //add listener when mouse is released
        stage.addEventListener("mouseUp", mu);
    
        //add a listener for enterframe to record the mouse's motion
        addEventListener("enterFrame", recordMouse);
    }
    
    function mu(evt:*):void
    {
        stage.removeEventListener("mouseUp", mu);
        removeEventListener("enterFrame", recordMouse);
    
        //snap the object to the drawn line and play it
        addEventListener("enterFrame", playRecording);
    }
    
    function recordMouse(evt:*):void
    {
        coordinates.push(new Point(stage.mouseX, stage.mouseY));
    }
    
    function playRecording(evt:*):void
    {
        //snap object to the recorded coordinates
        myObject.x = coordinates[0].x;
        myObject.y = coordinates[0].y;
    
        //delete first element of array
        coordinates.splice(0, 1);
    
        //stop playing if there are no more points
        if(coordinates.length == 0) removeEventListener("enterFrame", playRecording);
    }
    

    在舞台上放置一个影片剪辑并将其命名为 myObject。然后添加代码并编译 swf。

    此外,在“记录”坐标的同时,您还可以绘制一些线条。

    把 md 函数改成这样:

    function md(evt:*):void
    {
        //empty the coordinates
        coordinates = [];
    
        //add listener when mouse is released
        stage.addEventListener("mouseUp", mu);
    
        //add a listener for enterframe to record the mouse's motion
        addEventListener("enterFrame", recordMouse);
    
        //clear graphics, and initialize line
        with(graphics) clear(), lineStyle(1, 0xff0000), moveTo(stage.mouseX, stage.mouseY);
    }
    

    并将鼠标记录到此。

    function recordMouse(evt:*):void
    {
        coordinates.push(new Point(stage.mouseX, stage.mouseY));
    
        //draw the line
        with(graphics) lineTo(stage.mouseX, stage.mouseY);
    }
    

    第三

    如果您想遵循预先绘制的路线,那么您有多种选择,具体取决于您的任务。但一切都取决于您究竟想如何“捕捉”您的对象。

    【讨论】:

    • 谢谢 anemgyenge。这有助于我了解我需要做的很多事情。我更喜欢使用您提供的第二个选项,但我不知道如何将绘图工具添加到舞台??
    • 好吧,在舞台上创建一个影片剪辑。给这个实例一个名字(属性标签,名字):myObject.然后点击第一帧并粘贴到我写的actionscript代码部分(Windows中的F9功能键)。我猜你正在使用类似 Adob​​e Flash 开发软件的东西。希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多