0

another swimming fish question ;-)

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.

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.

Thanks and greetz, Arno.

j08691
  • 204,283
  • 31
  • 260
  • 272
Dejansen
  • 13
  • 2

1 Answers1

0

You can always get the position of the element:

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

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

Something like:

  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.

Check this.

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

Also now there's the Mutation Observers is a replace for the Mutations Events, so you can write an observer for your DOM object, more here

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

Update

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.

Left or Rigth

Update 2

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

rscnt
  • 985
  • 9
  • 13
  • Wow, thanks for that great answer. The watch plugin is just what i need. Your math is based on a collision with another object. Can you also calculate just in which direction the sprite is going ? Thanks again. Arno. – Dejansen Jun 12 '13 at 12:10
  • That's not working, because when the bird is left of the center it can still fly to to right and vice versa. I did something like this: http://jsfiddle.net/eagleeyes007/dNKax/5/ But that's not working either. – Dejansen Jun 12 '13 at 17:52
  • Yes. It works for a split second and then it looks like spritely is just resetting it to spState 1 – Dejansen Jun 12 '13 at 18:20
  • It's prety dumb, but does exactly what i think you say you need... :) – rscnt Jun 12 '13 at 18:52
  • WOW amazing ! This is exactly what I needed. Thank you so much for your help. If I have the finished site online, I will post the link here so you can see with what you helped me. My answer to your request will be in the code too ;-) – Dejansen Jun 12 '13 at 19:52