且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当spRandom中的方向更改时更改spState

更新时间:2023-02-13 19:32:51

您始终可以获取元素的位置:

You can always get the position of the element:

   $('#bird').position().top // left, right, bottom.

然后,您可以获取该精灵与对象之间的距离,并设置正确的spState.

Then you can get the distance of that this sprite have with the object and set the correct spState.

类似的东西:

  distance = Math.sqrt((
             Math.pow(
                  (object.position().left - spirte.position().left), 2) +
             Math.pow(
                  (object.position().top - spirte.position().left), 2) 
           ));

因此,您可以检查这些东西是否需要改变多远.

So you can check with an if how much distance those things need to have to changue.

检查.

要观看精灵,您需要类似,是使用 文档.

And for watch the sprite you need something like this, is a jQuery plugin that uses Mutations Events for watch the elements, the documentation.

现在还有突变事件 ,因此您可以为DOM对象 这是一个示例 ,我希望它可以帮助您提出一个想法.

Here's an example i hope it helps you make an idea.

更新

您需要做的就是获取父级的中心,然后检查当前位置 精灵的大小,看看它是否大于或小于中心.

All you need to do is get the center of the parent, then check for the current position of your sprite and see if it's more or less than the center.

左或右

更新2

它似乎在jsfiddle之外起作用. 在此处进行测试.(jsbin)

It seems to work outside jsfiddle. Test it here. (jsbin)