如何在 Actionscript 3.0 中使用静态函数?
我想要一些对象(例如 20 个),每次我将鼠标悬停在其中任何一个上时,它都会向上移动,每次我的鼠标离开时,它都会向下移动。
obj1.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj1.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj2.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj2.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj3.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj3.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj4.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj4.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj5.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj5.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
//and etc...
function moveMyself1(e:MouseEvent):void{
obj1.y -= 30;
}
function moveMyself2(e:MouseEvent):void{
obj1.y += 30;
}
我不想为每个对象添加一个事件侦听器,那么我将有 40 个方法!有没有办法编写静态方法以便我可以用于所有对象?
我意识到物体上下移动得太快了。如果你尝试将鼠标放在 obj 的底端,你会看到它快速地上下跳动。有什么方法可以控制 obj 的速度吗?
我想要一些对象,用户可以将鼠标悬停在其中并发现下面的宝藏。用户也可以点击宝藏。我从一个游戏中得到了这个想法。当用户将鼠标移开后,该对象将回落到同一位置。如果物体移动太快,用户就无法点击里面的宝藏。关于如何解决运动问题有什么想法吗?
更新
var elements : Array = new Array();
var elements2 : Array = new Array();
for (var i:int = 1; i <= 5; i++) {
elements[i] = this['obj' + i];
elements2[i] = this['tracking' + i];
}
for each(var element_1 : IEventDispatcher in elements){
element_1.addEventListener(MouseEvent.MOUSE_OVER, moveUp);
}
for each(var element_2 : IEventDispatcher in elements2){
element_2.addEventListener(MouseEvent.MOUSE_OUT, moveDown);
}
function moveUp(e:MouseEvent):void{
e.currentTarget.y -= 30;
}
function moveDown(e:MouseEvent):void{
elements[elements2.indexOf(e.currentTarget)].y += 30;
}
上面是我更新的代码,我尝试了理查德的建议,但似乎对象上下移动超出了我的控制范围。
I would like to have a few objects (e.g. 20 of them), each time I mouse over any one of them, it moves up and each time my mouse leaves, it moves down.
obj1.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj1.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj2.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj2.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj3.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj3.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj4.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj4.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj5.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj5.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
//and etc...
function moveMyself1(e:MouseEvent):void{
obj1.y -= 30;
}
function moveMyself2(e:MouseEvent):void{
obj1.y += 30;
}
I don't want to add an event listener for each of the objects, then I would have 40 methods! Is there any way to write a static method so I can use for all the objects?
And I realized the obj is moving up and down too fast. If you try to put your mouse at the bottom end of the obj, you will see it jumping up n down very fast. Is there any way I can control the speed of the obj?
I wanted to have a few objects in which the user can mouse over and discover treasure underneath. The user can click on the treasure as well. I got this idea from a game. The object will fall back to the same position after the user moves the mouse away. If the object moves too fast, the user can't get to click on the treasure inside. Any idea on how to solve the movement issue?
Updated
var elements : Array = new Array();
var elements2 : Array = new Array();
for (var i:int = 1; i <= 5; i++) {
elements[i] = this['obj' + i];
elements2[i] = this['tracking' + i];
}
for each(var element_1 : IEventDispatcher in elements){
element_1.addEventListener(MouseEvent.MOUSE_OVER, moveUp);
}
for each(var element_2 : IEventDispatcher in elements2){
element_2.addEventListener(MouseEvent.MOUSE_OUT, moveDown);
}
function moveUp(e:MouseEvent):void{
e.currentTarget.y -= 30;
}
function moveDown(e:MouseEvent):void{
elements[elements2.indexOf(e.currentTarget)].y += 30;
}
Above is my updated code, I tried Richard's suggestion, but it seemed like the objects are moving up and down out of my control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要为每个对象编写函数,因为您可以将正在侦听事件的对象引用为事件的“目标”,因此:
此外,您看到对象上下移动的原因确实如此快速是因为当您更改对象的位置时,鼠标不再位于对象内部,因此 MOUSE_OUT 事件触发,然后您再次将对象的位置更改为鼠标所在的位置并触发 MOUSE_OVER 事件,依此类推。轨迹将是:
对象位于 y=5(例如)。将鼠标移到上方(鼠标位于 y=5)。 MOUSE_OVER 事件触发 ->物体向上移动 (y=35) -> MOUSE_OUT 事件触发 ->物体向下移动 (y=5) ->由于鼠标仍位于 y=5,因此会触发 MOUSE_OVER 事件 ->冲洗并重复。
请记住,当您设置对象的 y 时,您并不是在创建运动动画,而是将其“远程传送”到该位置。
You don't need to code a function for each object, since you can refer to the object that is listening to the event as the 'target' of the event, so:
Also, the reason you see the object moving up&down really fast is because when you change the object's position, the mouse stops being inside the object so the MOUSE_OUT event fires, then you change the object's position again to where the mouse is and the MOUSE_OVER event fires and so on. The trace would be:
The object is at y=5 (for example). You move the mouse over (mouse is at y=5). MOUSE_OVER event fires -> the object moves up (y=35) -> MOUSE_OUT event fires -> the object moves down (y=5) -> since the mouse is still at y=5, MOUSE_OVER event fires -> rinse&repeat.
Please bear in mind that when you set the y of the object, you are not creating a movement animation, but kind of "teletransporting" it to that position.
您可以将对象放入数组中,并将对象从事件处理程序传递到应用逻辑的方法:
就移动速度而言,也许您可以触发动画?
You could put the objects into an array and pass the object from the event handler to your method that applies the logic:
As far as the movement speed, perhaps you could trigger an animation instead?
将您想要侦听的所有剪辑添加到容器中:
然后将事件侦听器添加到该容器中:
作为奖励:如果您有那么多按顺序命名的对象,您可以像这样:
this['obj' + i ]
将解析为 obj1、obj2 等。add all the clips you want to listen on to a container:
Then add an event listener to that container:
As a bonus: If you have that many objects named sequentially you can go like this:
this['obj' + i]
will resolve to obj1, obj2 and so on.