Actionscript 3 移动对象不工作,但我没有错误
我编写了一些ActionScript3代码,用于在另一个对象靠近时移动对象。 我使用flash cs5.5 问题是,当我添加了一些只移动该对象一次的代码时,代码就不能正常工作了。也许问题不在于代码本身,而在于对象或对象实例中的某些东西,但我没有改变任何东西。 我得到了你能在代码中看到的所有轨迹,但是当我把磁铁靠近钟摆时,钟摆并没有像它那样旋转Actionscript 3 移动对象不工作,但我没有错误,actionscript-3,flash,function,object,animation,Actionscript 3,Flash,Function,Object,Animation,我编写了一些ActionScript3代码,用于在另一个对象靠近时移动对象。 我使用flash cs5.5 问题是,当我添加了一些只移动该对象一次的代码时,代码就不能正常工作了。也许问题不在于代码本身,而在于对象或对象实例中的某些东西,但我没有改变任何东西。 我得到了你能在代码中看到的所有轨迹,但是当我把磁铁靠近钟摆时,钟摆并没有像它那样旋转 import flash.events.MouseEvent; import flash.events.Event; var angle:Number
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");
}
我没有任何错误或其他事情。出什么问题了?我什么也看不见。这是你的问题:
if(moved_once==0){
move_pendulum(event); //call function for rotating object "rotation"
moved_once++; //increase counter for not rotating again
}
检查全局变量moved_的值一次,如果为0,则移动钟摆,然后增加moved_一次。在此点之后,moved_once=1,条件moved_once==0不再为真。
再加上一个事实,你称之为move_摆角=0,sin0=0,0*rotate=0的唯一一次,它不会去任何地方。
如果您总是想让钟摆移动,则将If完全移除。如果你只想让钟摆移动,例如,你正在拖动磁铁,那么无论如何,你最好用一个bool替换moved_一次。说代码停止正常工作有点含糊不清。发生了什么事?你找到痕迹了吗?如果你没有得到范围检测跟踪,你应该从那里开始。是吗?@原型是的,谢谢。我在描述中添加了这个信息。我得到了所有的痕迹,但我没有像以前那样移动。什么也没有移动?磁铁还是钟摆??根据您的代码,如果该条件为真,钟摆将完全停止移动,并且不会再次开始移动。移动摆函数是角度增加的地方。摆不移动。我想要的是,一旦磁铁靠近钟摆,就移动钟摆。如果钟摆不再发生其他类型的移动,这个代码和你的描述没有具体说明。