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

#1 2012-03-22 14:10:47

jpsrolls
New Scratcher
Registered: 2010-12-09
Posts: 17

Student needs help

The program has a wizard and a dragon.  Every time the two sprites touch one of them gets a point.  The student has two variable scores, one for the dragon and one for the wizard.  The score is not working.  I am wondering if they are on the same layer.  They don't seem to be, and I think it may be a problem.  Can anyone help?    In the script, each sprite has an if  loop saying that if one is touching the other the score increases by one.

Offline

 

#2 2012-03-22 14:12:10

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Student needs help

Could you specify exactly what is not working?
(They do not have to be on the same layer)

Try using this:

when gf clicked
set [wizardscore v] to (0)
forever
if <touching [dragon v]?> //or wizard
change [wizardscore v] by (1)
wait until <not <touching [dragon v]?> > //or wizard
end

Last edited by trinary (2012-03-22 14:20:43)


http://trinary.tk/images/signature_.php

Offline

 

#3 2012-03-22 15:32:55

Indiego
New Scratcher
Registered: 2012-02-27
Posts: 61

Re: Student needs help

You could try putting these into the dragon script:

when gf clicked
forever
wait until <touching [wizard v]?>
change [scoredragon v] by [1]
wait until <not <touching [wizard v]?>>

and put this into the wizard:

when gf clicked
forever
wait until <touching [dragon v]?>
change [scorewizard v] by [1]
wait until <not <touching [dragon v]?>>

Offline

 

#4 2012-03-23 06:09:15

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Student needs help

You probably need to have a forever block around the if.  smile

Offline

 

#5 2012-03-23 08:47:00

Mokat
Scratcher
Registered: 2011-12-08
Posts: 1000+

Re: Student needs help

scimonster wrote:

You probably need to have a forever block around the if.  smile

Or use the

 Forever If <> 


http://www.eggcave.com/egg/977371.pnghttp://www.eggcave.com/egg/977376.pnghttp://www.eggcave.com/egg/1005291.pnghttp://www.eggcave.com/egg/996745.png

Offline

 

#6 2012-03-23 14:41:56

LiquidMetal
Scratcher
Registered: 2011-06-15
Posts: 500+

Re: Student needs help

Mokat wrote:

scimonster wrote:

You probably need to have a forever block around the if.  smile

Or use the

 Forever If <> 

Do NOT use the forever if.

I learned this the hard way:

Forever if <>
end
Checks if the boolean operation is true, and if it is, executes forever.  It only checks true or false ONCE.  This is something I hope they will fix in Scratch 2.0, as it really confuses new scratchers.

Offline

 

#7 2012-03-26 13:14:16

jpsrolls
New Scratcher
Registered: 2010-12-09
Posts: 17

Re: Student needs help

Hi-the students say it is working, except that when the sprites touch BOTH get the point.  How can we fix this?  Thanks again.

Offline

 

#8 2012-03-26 13:42:53

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Student needs help

So you want it that, whoever was the one to impact gets the point. That can get tricky.
Could your student upload the project? I might be able to play around with it and get it working then.  smile

Offline

 

#9 2012-03-26 14:25:35

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Student needs help

LiquidMetal wrote:

Mokat wrote:

scimonster wrote:

You probably need to have a forever block around the if.  smile

Or use the

 Forever If <> 

Do NOT use the forever if.

I learned this the hard way:

Forever if <>
end
Checks if the boolean operation is true, and if it is, executes forever.  It only checks true or false ONCE.  This is something I hope they will fix in Scratch 2.0, as it really confuses new scratchers.

Uh, no it doesn't.  This can be proven with a simple script:

When gf clicked
forever if <key [space  v] pressed?>
change [color  v] effect by (10)
You'll notice that the color only changes when the space key is pressed.



Back to the topic:  It sounds like you'll need to calculate the speed of both characters.  I believe this can be done effectively by waiting a VERY small amount of time (such as a hundredth of a second), calculating the distance between the old position and new position using the distance formula, and using the speed formula (distance / time) to calculate the speed of each character.  Then, update it constantly, and compare the speeds when they touch.

Hopefully, there's an easier way.   tongue

Offline

 

#10 2012-03-27 13:29:54

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: Student needs help

You can keep speed variables, and change the speed variable if you want to change the speed.

Wizard:

when gf clicked
forever
move (wizard speed) steps

when gf clicked
forever
if <touching [dragon v]?>
if <(dragon speed) < (wizard speed)>
change [wizard score v] by (1)
end
end
Dragon:
when gf clicked
forever
move (dragon speed) steps

when gf clicked
forever
if <touching [wizard v]?>
if <(wizard speed) < (dragon speed)>
change [dragon score v] by (1)
end
end

Offline

 

#11 2012-03-27 13:59:55

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: Student needs help

LiquidMetal wrote:

Mokat wrote:

scimonster wrote:

You probably need to have a forever block around the if.  smile

Or use the

 Forever If <> 

Do NOT use the forever if.

I learned this the hard way:

Forever if <>
end
Checks if the boolean operation is true, and if it is, executes forever.  It only checks true or false ONCE.  This is something I hope they will fix in Scratch 2.0, as it really confuses new scratchers.

no, you have it the wrong way round. Most new scratchers THINK it checks the condition once then loops it forever, but it is the other way round: whenever the condition is true it runs the loop, then checks the condition again and waits until it us true before running the loop again, and it does this for ever. It is equivalent to

forever
if<something>
  do stuff
end
or
forever
wait until <something>
do stuff


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#12 2012-03-27 16:23:52

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

Re: Student needs help

The velocity idea is clever, but not quite right...you could possibly get it to work if you used the absolute value of the velocity but I think I have a simpler way:

I'm assuming the sprites are controlled one with wasd and the other with the arrow keys, correct?

Well, in the controls of each sprite, in every if key pressed statement have a variable that changes between -1 and 1 based off either sprite that's being controlled.  This will tell you the last sprite that has been moved (you can change the values to a string like "wizard" and "dragon" if you want).

My method would look something like this: in ONLY ONE of the sprites, put an if block for touching the other (I'll use the dragon):

if <touching [wizard v]?>
  change [dragon_score v] by (((last)+(1))/(2))
  change [wizard_score v] by (((last)-(1))/(-2))
end
In this case, the variable "last" is set to 1 when the dragon moves and -1 when the wizard moves.  Using those two equations, the correct variable will be changed by 1 and the other by 0.


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

Offline

 

#13 2012-03-28 06:57:07

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Student needs help

But what if both headbang into each other?  tongue

What you should do in my opinion is the velocity method. Or you could add a new feature where you press and hold a key (different for each player) to switch to an "attack mode". This lasts for say 1 second, and when you are in attack mode and hit the other player, your score increases. Otherwise, if you you touch the other player outside attack mode, your score decreases. And attack mode needs a hefty reload period.

Last edited by Hardmath123 (2012-03-28 06:58:21)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

Board footer