This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-21 16:21:46

jose6a
New Scratcher
Registered: 2011-08-08
Posts: 23

detecting change direction (right<->left)

Hi scratchers, first of all
i'm a frenchy guy, so please be indulgent with my so poor english ....


well one of my very first script :
http://scratch.mit.edu/projects/jose6a/2469404
is about a fish who follow my mouse ... OK ... it works fine,
but, I have a pb with the changing costume.

If you download the script, you will see that event
when i don't change direction, the script change the costume...
I would like to optimise that script by changing costume
juste once time ! only when i change direction.

So, how can i detect that changing detection ?
i have "current direction"
i have "left direction" = -90
i have "right direction" = 90

Is it possible to change costume when i change direction ?

thanks for helping !
José

Offline

 

#2 2012-04-21 16:48:01

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: detecting change direction (right<->left)

jose6a wrote:

Hi scratchers, first of all
i'm a frenchy guy, so please be indulgent with my so poor english ....


well one of my very first script :
http://scratch.mit.edu/projects/jose6a/2469404
is about a fish who follow my mouse ... OK ... it works fine,
but, I have a pb with the changing costume.

If you download the script, you will see that event
when i don't change direction, the script change the costume...
I would like to optimise that script by changing costume
juste once time ! only when i change direction.

So, how can i detect that changing detection ?
i have "current direction"
i have "left direction" = -90
i have "right direction" = 90

Is it possible to change costume when i change direction ?

thanks for helping !
José

The easiest way to do this would to have costumes named:
fish1
fish-1

Then you add the script:

if <not <(direction) = (0)> >
  switch to costume (join [fish] (([abs v] of (direction))/(direction)))
end
If it ends up being backwards, just switch the costume names around  smile 

Hope that helped!

Last edited by AtomicBawm3 (2012-04-21 16:49:58)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#3 2012-04-21 17:08:51

jose6a
New Scratcher
Registered: 2011-08-08
Posts: 23

Re: detecting change direction (right<->left)

Hi atomicBawm3 !
wellthankx a lot for your help, I have success with
my solution, but yours is nicer and better !

but first, i'll try to understand your solution, and how it works !

please don't hesitate to comment my own solution, i'm afraid, it's
a préhistoric one !

thanks again !
José from france (so, i supose my english is préhistoric too !)

Offline

 

#4 2012-04-21 18:35:42

jose6a
New Scratcher
Registered: 2011-08-08
Posts: 23

Re: detecting change direction (right<->left)

well ... that works fine, but, i have one question :

is it not timeprocess consuming switching allways the costume, even if we don't
change direction with the mouse ?
the new : http://scratch.mit.edu/projects/jose6a/2486309
thanks a lot !
José

Offline

 

#5 2012-04-21 19:10:04

iTweak0r
Scratcher
Registered: 2011-07-30
Posts: 100+

Re: detecting change direction (right<->left)

Some tip:

Abs tells the positive of a number, so -3 becomes 3.


Make it in Scratch! because it's cooler when it's made in scratch
http://i.imgur.com/D4iqPHR.png

Offline

 

#6 2012-04-21 19:36:19

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: detecting change direction (right<->left)

jose6a wrote:

well ... that works fine, but, i have one question :

is it not timeprocess consuming switching allways the costume, even if we don't
change direction with the mouse ?
the new : http://scratch.mit.edu/projects/jose6a/2486309
thanks a lot !
José

Since this is only happening for 1 sprite and it's a very simple change, no it isn't time consuming...also, this is in Scratch, so any method is going to delay almost just as much as this one...however, if you only want it to change when necessary:

//first create a variable named "old" and another named "new"
When gf clicked
set [old v] to (round(([abs v] of (direction))/((direction)+(.001))))
forever
  point towards [mouse-pointer v]
  move (3) steps
  set [new v] to (round(([abs v] of (direction))/((direction)+(.001))))
  if <not<(new) = (old)>>
    switch to costume (join [fish] (new))
    set [old v] to (new)
  end
A few things:
--the new formula is simply so that a direction of zero won't cause an error
--the reason the formula works is because it simply gets the sign (+ or -) from the direction...in this case, it will also get 0 if direction = 0
--keep the same costumes as before.

Last edited by AtomicBawm3 (2012-04-21 20:08:27)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#7 2012-04-21 19:44:57

chanmanpartyman
Scratcher
Registered: 2011-05-30
Posts: 500+

Re: detecting change direction (right<->left)

AtomicBawm3 wrote:

jose6a wrote:

well ... that works fine, but, i have one question :

is it not timeprocess consuming switching allways the costume, even if we don't
change direction with the mouse ?
the new : http://scratch.mit.edu/projects/jose6a/2486309
thanks a lot !
José

Since this is only happening for 1 sprite and it's a very simple change, no it isn't time consuming...also, this is in Scratch, so any method is going to delay almost just as much as this one...however, if you only want it to change when necessary:

//first create a variable named "old" and another named "new"
When gf clicked
set [old v] to (round(([abs v] of (direction))/((direction)+(.001))))
forever
  point towards [mouse-pointer v]
  move (3) steps
  set [new v] to (round(([abs v] of (direction))/((direction)+(.001))))
  if <not<(new) = (old)>>
    switch to costume (join [fish] (new))
    set [old v] to (new)
  end
A few things:
--the new formula is simply so that a direction of zero won't cause an error
--the reason the formula works is because it simply gets the sign (+ or -) from the direction...in this case, it will also get 0 if direction = 0
--keep the same costumes as before.

Fixed.

Offline

 

#8 2012-04-24 18:35:59

jose6a
New Scratcher
Registered: 2011-08-08
Posts: 23

Re: detecting change direction (right<->left)

Hi AtomicBawm3 and  chanmanpartyman !
thanks a lot for your help and suggestions !
I'll gone à trye that.
José

Offline

 

Board footer