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

#1 2007-07-02 18:47:34

scratchmoore
Scratcher
Registered: 2007-07-02
Posts: 5

Problem - only rarely escapes from a 'repeat until touching' loop

Any help on this one would be great.

For some reason, my alien is in denial and refuses to admit it's been hit.

I've got a 'Repeat until touching' set to the bullet but the loop is only occasionally exited from. As a test I included a seperate forever loop on the alien that contantly checks if it's touched by the bullet, and if it is, it says "ouch"

Well the alien says ouch so it's definately recognising the bullet all of the time - so why won't it exit out of my 'repeat until touching bullet' loop?

Btw, the reason the x and y position is being controllled from the stage is because I need this mechanism to control another 30 or so aliens. Any advice would be great. Thanks

http://www.scratch.mit.edu/projects/scratchmoore/18734

Offline

 

#2 2007-07-02 19:03:37

toontownmiser
Scratcher
Registered: 2007-06-28
Posts: 18

Re: Problem - only rarely escapes from a 'repeat until touching' loop

Another thing you might try is this:
in bullet sprite
repeat until | <<<xpos is greater than (alien x - range)> and <xpos is lesser than (alien x-range)> and <y pos = alien y>

Touch complex, but it should work.

Another thing: scratch won't check to see if the boolean is true or false until after the enclosed blocks are run through.

Hope it helps.

Last edited by toontownmiser (2007-07-02 19:05:17)


Algebra I Lesson for Today:
(cat)+(dog)=(fish)
Given: c=1, a=1, t=2  d=1,o=1, g=2 and f=1, i=1, s=2, s=2
True or False?

Offline

 

#3 2007-07-02 20:55:27

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Problem - only rarely escapes from a 'repeat until touching' loop

The problem is that you are only checking the contact twice a second.
You probably want a loop that is doing the detecting with a tiny wait (maybe 0.05 seconds) and a separate loop for the movement.  Your detection loop might look like

repeat until touching sprite1
    wait 0.05 sec
set dead to 1


and the other loop might look like

set dead to 0
repeat until dead > 0
    <movement code with waits>

Offline

 

#4 2007-07-03 16:39:39

scratchmoore
Scratcher
Registered: 2007-07-02
Posts: 5

Re: Problem - only rarely escapes from a 'repeat until touching' loop

Ahh - I'd made the incorrect assumption that it was constantly checking.

Thanks for both those answers. I'm back on track again.

Many thanks

smile

Offline

 

#5 2007-07-03 17:03:23

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Problem - only rarely escapes from a 'repeat until touching' loop

If your alien is detecting just fine, you might be able to influence you bullet with a broadcast or variable change.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#6 2007-07-03 19:57:55

DrJim
Scratcher
Registered: 2007-05-26
Posts: 100+

Re: Problem - only rarely escapes from a 'repeat until touching' loop

For Kevin's solution, I'd recommend "wait 0" in the test loop.  The move command seems essentially instaneous (true??) - if one object passes through another, the "collision" can be missed even for a wait as small as 0.05 - see http://scratch.mit.edu/projects/DrJim/19015 (download to edit project to see problem).

Offline

 

#7 2007-07-04 12:21:51

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Problem - only rarely escapes from a 'repeat until touching' loop

I was assuming that the movement code used either a glide or a loop of moves separated by waits, so that the bullet and target would be guaranteed to be in contact for a known minimum amount of time.  Without such a guarantee, even a forever loop that has no waits is not guaranteed to do the detection.

Offline

 

#8 2007-07-04 15:12:00

scratchmoore
Scratcher
Registered: 2007-07-02
Posts: 5

Re: Problem - only rarely escapes from a 'repeat until touching' loop

Yes the issue actually had to be solved in 2 places. The movement of the alien(s) had to be synchronised with the checking for contact, but also, I was hiding the bullet as soon as it contacted with anything green, so it needed to travel a little further sometimes to be registered.

It workes ok now and I've added another dozen aliens no problem, all of them die like troopers. Now to get them to shoot back and to build some shelters for the gun.

Thanks for your advice everyone.

Offline

 

#9 2007-07-09 11:14:10

scratchmoore
Scratcher
Registered: 2007-07-02
Posts: 5

Re: Problem - only rarely escapes from a 'repeat until touching' loop

Problem sorted now.

Here's the project if you're interested

http://scratch.mit.edu/projects/scratchmoore/20226

Offline

 

Board footer