当spRandom中的方向更改时更改spState [英] Change spState when direction change in spRandom

查看:90
本文介绍了当spRandom中的方向更改时更改spState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个游泳鱼的问题;-)

another swimming fish question ;-)

是否可以检测spRandom当前移动的方向?这样我就可以将sprite的spState从1更改为2.

Is there a way to detect the direction in which spRandom is currently moving ? So that I can change the spState of my sprite from 1 to 2.

如果direction = left spState(1)->鱼面朝左 如果direction = right spState(2)->鱼面朝右

if direction=left spState(1) -> Fish faces left if direction=right spState(2) -> Fish faces right

希望您能理解我的问题.我还没有发布任何示例代码.希望任何人都能给我一个通用的示例.

Hope you understand my question. I did not post any example code yet. Hope anyone can give me a generic example of how to do that.

感谢和格里茨,亚诺.

推荐答案

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

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.

现在还有 这是一个示例 ,我希望它可以帮助您提出一个想法.

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)

这篇关于当spRandom中的方向更改时更改spState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆