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

#1 2012-03-04 11:44:24

EmeraldDragon
Scratcher
Registered: 2011-11-20
Posts: 8

Help with collisions and movement...

Hi, fellow scratchers  smile  I'm working on a game, but I'm having trouble with collision scripts. I want to make it so the character will NOT be able to move in a certain direction when touching a collision object. I must add that the "map" that the character will be traveling on will be a top view, not a side view. What I keep ending up with is the character becoming completely immobile when touching the collision object. Anyone know a good script for collision? Thank you  smile

(p.s. I do not want my character to change direction or  "bounce", that becomes VERY annoying...)


The Emerald Dragon of awesomeness...

Offline

 

#2 2012-03-04 13:04:47

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

Re: Help with collisions and movement...

I don't know of any good way to do it without bouncing.  hmm
What I do is have two personal (for this sprite only) variables, called "last xpos" and "last ypos".
Then, in my moving script, I have this:

when gf clicked
forever
   set [last xpos v] to (x position)
   set [last ypos v] to (y position)
   moving script
   if <touching color [#000000]?> //use the color you don't want to go through
      go to x: (last xpos) y: (last ypos)
   end
end

Offline

 

#3 2012-03-04 14:28:34

EmeraldDragon
Scratcher
Registered: 2011-11-20
Posts: 8

Re: Help with collisions and movement...

Thank you so much scimonster! This really helped! The tiny  bit of bouncing is fine  smile


The Emerald Dragon of awesomeness...

Offline

 

#4 2012-03-04 14:53:00

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with collisions and movement...

I do something similar, but with the x-change and y-change separate, so if you, say, jump into a ceiling, your horizontal movement isn't affected.

when gf clicked
forever
set[old_x] to (x position)
set[old_y] to (y position)
x-movement-script
if<touching [wall/ceiling]?>
set x to (old_x)
end
y-movement-script
if<touching [wall/ceiling]?>
set y to (old_y)
end
end

Last edited by MoreGamesNow (2012-03-04 15:35:43)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2012-03-04 15:26:01

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

Re: Help with collisions and movement...

See the project mentioned in this thread.   smile

Offline

 

#6 2012-03-04 16:59:40

brettman98
Scratcher
Registered: 2010-10-17
Posts: 1000+

Re: Help with collisions and movement...

you could have a special collision sprite for the main character, so if one point of the collision box is touching the collision object the character will stop moving.


http://i40.tinypic.com/n1wbr7.jpg

Offline

 

Board footer