【问题标题】:Moving object is not working but i have no errors移动对象不工作,但我没有错误
【发布时间】:2013-11-24 13:56:09
【问题描述】:

我编写了一些 actionscript 3 代码,用于在另一个对象靠近时移动一个对象。 我用的是flash cs5.5 问题是,当我添加一些代码仅移动该对象一次时,代码停止正常工作。也许问题不在于代码本身,而在于对象或对象实例中的某些内容,但我没有更改任何内容。 我得到了你可以在代码中看到的所有痕迹,但是当我将“磁铁”关闭到“钟摆”时,钟摆并没有像以前那样移动旋转。

import flash.events.MouseEvent;
import flash.events.Event;

var angle:Number = 0; //angle for rotation of object "pendulum"
var rotate:Number = 45; // value to multiple to calculate rotation

//global variable
var moved_once: int=0; //variable for checking the first move of the object 

function move_pendulum(e:Event):void{

    trace("hit"); //to check if enters the function
 pendulum1.rotation = Math.sin(angle) * rotate; //execute rotation
 angle +=0.3; //increase angle for next rotation
trace("hit2"); //to check if the function ends
}

magnet1.addEventListener(MouseEvent.MOUSE_DOWN, start_magnet); //for start dragging //object "magnet"
magnet1.addEventListener(MouseEvent.MOUSE_UP, stop_magnet); //for stop dragging //object "magnet"


function start_magnet(event:MouseEvent):void{

    magnet1.startDrag(); //start dragging of object "magnet"

    //check if the two objects are close enough
    if(magnet1.x >pendulum1.x && (magnet1.x-pendulum1.x)<100){
        trace("range detect");
    //check if it is the first time which the two objects are close enough
    if(moved_once==0){
        trace("called move");
        move_pendulum(event); //call function for rotating object "rotation"
        moved_once++; //increase counter for not rotating again
            }                                       
                                                            }

                                            }//end of function                                                  

function stop_magnet(event:MouseEvent):void{

    magnet1.stopDrag(); //stop dragging of object
    trace("stopped");
    }

我没有错误或其他任何错误。出了什么问题?我什么都看不到。

【问题讨论】:

  • 说代码停止正常工作有点含糊。怎么了 ?你有痕迹吗?如果你没有得到“范围检测”跟踪,你应该从那里开始。对吗?
  • @prototypical 是的,谢谢。我在描述中添加了这个信息。我得到了所有的痕迹,但我没有像以前那样移动。
  • 什么都没动?磁铁还是钟摆??根据您的代码,如果该条件为真并且不会再次开始移动,则钟摆将完全停止移动。 move_pendulum 函数是增加角度的地方。
  • 钟摆不动。我想要的是一旦磁铁靠近钟摆就移动钟摆
  • 如果钟摆有其他类型的运动不再发生,则此代码和您的描述未指定。

标签: actionscript-3 flash function object animation


【解决方案1】:

这是你的问题:

if(moved_once==0){
move_pendulum(event); //call function for rotating object "rotation"
moved_once++; //increase counter for not rotating again
}  

您正在检查全局变量moved_once 的值,如果它为0,那么您移动钟摆,然后增加moved_once。在此点之后,moved_once = 1 并且条件 (moved_once == 0) 不再为真。
再加上您调用 move_pendulum angle = 0(和 sin(0) = 0 和 0 * rotate = 0)的唯一一次,它不会去任何地方。
如果您总是希望钟摆移动,则将 if 完全删除。如果您只希望钟摆移动,例如,您正在拖动磁铁,那么您最好用 bool 替换moved_once。

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2020-11-10
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多