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

#1 2009-01-07 19:26:21

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

How can I keep a sprite from moving into certain areas?

How can I color (or another way) the backgrounds, so I can keep my sprite from going in those areas. I already have the backgrounds made, but I could put lines on it to block that area. I am using scrolling with X and Y. When I have it stop "scroll X" and "scroll Y", my sprite just stops and gets stuck. How can I keep it from moving, like have it bounce back (slightly). Remember I am using scrolling, and I already have background, but I could put lines down. If you know of any sample projects that would be nice. If you know a way and can just type it out, that is fine too. All help appreciated, thanks.

Offline

 

#2 2009-01-07 19:39:46

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

OK,  first make 2 variables oldScrollX and oldScrollY

First make sure all the places you don't want to go into is one colour.

Put this script in

when flag clicked
forever
if touching colour (specify colour)
set scrollX to oldScrollX
set scrollY to oldScrollY
end
set oldScrollX to scrollX
set oldScrollY to scrollY
end

This will make it so that the sprite will never end up inside the specified colour.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#3 2009-01-07 20:55:34

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

What do you mean by end? Do you mean stop script or what? Also will this work if I just draw lines? I want the player to be able to see into the distance of where they can't go.

Offline

 

#4 2009-01-07 21:00:51

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

Purplewalrus wrote:

What do you mean by end? Do you mean stop script or what? Also will this work if I just draw lines? I want the player to be able to see into the distance of where they can't go.

By end I mean this part of the block

<end>

This will work if you draw lines.

Last edited by archmage (2009-01-07 21:01:30)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#5 2009-01-07 21:13:59

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

This is what I'm doing, and it isn't working, the sprite just gets stuck on the line.


http://scratch.mit.edu/forums/post.php?tid=13311#req_message
http://scratch.mit.edu/forums/post.php?tid=13311#req_message
http://scratch.mit.edu/forums/post.php?tid=13311#req_message http://scratch.mit.edu/forums/post.php?tid=13311#req_message
http://scratch.mit.edu/forums/post.php?tid=13311#req_message Scroll X => old scroll X
http://scratch.mit.edu/forums/post.php?tid=13311#req_message Scroll Y => old scroll Y
http://scratch.mit.edu/forums/post.php?tid=13311#req_message
http://scratch.mit.edu/forums/post.php?tid=13311#req_message old scroll X => Scroll X
http://scratch.mit.edu/forums/post.php?tid=13311#req_message old scroll Y => Scroll Y
http://scratch.mit.edu/forums/post.php?tid=13311#req_message

Or you can look at the picture of it here:
http://i303.photobucket.com/albums/nn160/purplewalruscontact/Script.jpg

Offline

 

#6 2009-01-07 21:15:33

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

I just tried to make the pictures with those blocks that the forums lets you use, and it didn't work as you can see with my previous post. it just made links, i guess you'll have to use the picture.

Offline

 

#7 2009-01-07 21:28:10

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

I got it to work this time, this is what it should look like.

<when green flag clicked>
<forever>
<if><touching color[
<set{ Scroll X }to( old scroll X
<set{ Scroll Y }to( old scroll Y
<end>
<set{ old scroll X }to( Scroll X
<set{ old scroll Y }to( Scroll Y
<end>

Offline

 

#8 2009-01-07 21:32:01

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

When I said, "I got it to work this time." I meant I got the forum script to work, not the actual script

Offline

 

#9 2009-01-07 21:36:01

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

I will post a demo. The code in the picture is correct.

The code I game you should work, another one of your codes must be the problem.
http://scratch.mit.edu/projects/archtest/374293  <-- I made a project of it

Or if the case is that you just don't want the scroll away from all of the terrain sprites there is an easier solution. But I am assuming that there will be certain parts in your terrain sprites that you don't want the player to go into.

Last edited by archmage (2009-01-07 21:38:37)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#10 2009-01-07 21:52:22

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

Now I am really confused. When I looked at your project online it looked fine, it worked. When I downloaded it, it didn't work. Then when I uploaded mine to this website, thinking it would work like yours, it didn't work. Do you have any idea whats going on?

Offline

 

#11 2009-01-07 21:58:23

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

I think I know the problem. I tried having scroll X and Scroll Y change by 1 and -1, and it worked. I think it was because I am going to fast, and move into the black, not next to it. Do you know how to fix this problem?

Offline

 

#12 2009-01-07 21:59:11

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

Oh yeah, I downloaded it and got the same error.

Let me check it out, I know it has something to do with the lag on the terrain sprites.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#13 2009-01-07 22:02:11

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

Alright, thanks a lot!

Offline

 

#14 2009-01-07 22:04:00

Hellbender
Scratcher
Registered: 2009-01-07
Posts: 3

Re: How can I keep a sprite from moving into certain areas?

I wish they would allow direct scripting

function newBlock(b,pos)
{
var pudding = pos
}
return pudding
end

>.>
To tell the truth I dont actually script Much  Java, just a lil, but I know alot of lua, lua would be easier than java.

function SaySomething(Something)
print(Something," has been said because I wanted It to say that")
end
SaySomething("Lol")

Last edited by Hellbender (2009-01-07 22:07:02)


Images hack us...no wait...imageshack.us
thats better  wink .

Offline

 

#15 2009-01-07 22:05:54

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

Hellbender wrote:

I wish they would allow direct scripting

function newBlock(b,pos)
{
var pudding = pos
}
return pudding
end

Kinda off topic. Try making your own thread instead of using an unrelated one.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#16 2009-01-07 22:28:19

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

This script is giving me a rough time :S

Let me explain why this should work and why this is the scratch program's fault.

lets assume that the user has the down arrow key down, and one more movement down will push the sprite into green space.

This is my script (ignore the get out broadcast, I was testing stuff)


http://img249.imageshack.us/img249/9039/exampleog3.gif


So the code should run through as follows.


1.the variable scrollY is 95 now, one more increase and the sprites moves into green zone. The oldScrollY variable is set to 95.

2. The down arrow key is down so we change the scrollY variable by 5

3. Since we are now in the green zone we have a script to move out of the green zone. The script will set the scrollY variable which is set to 100 to the oldScrollY variable which is set to 95.

4. We are out of the green area and life is good  smile

However THIS DOES NOT HAPPEN  mad

Seriously, this defys all logic.

Last edited by archmage (2009-01-07 22:30:55)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#17 2009-01-07 22:33:24

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

Wait...so your saying it can't be done?

Offline

 

#18 2009-01-07 22:36:10

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

Purplewalrus wrote:

Wait...so your saying it can't be done?

No, there is a way, but it involves using sensor sprites. I am just saying that my logic is perfect and that in any other language this would have worked but scratch is stupid sometimes.

Wait a bit and I'll upload another solution.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#19 2009-01-07 22:38:03

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

Alright, thanks.

Offline

 

#20 2009-01-07 22:46:50

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

Here, it uses sprites to detect its surroundings
http://scratch.mit.edu/projects/archtest/374326
Download it,  look at the code.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#21 2009-01-07 22:58:39

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

Ok, I figured out the problem in the first script too. You see, because scratch is slower to moves sprites offline the If Touching Green part was skipped because the terrain sprites hadn't moved yet. By the time they moved it is too late because the oldScroll variables were saved to a position inside the green part making the whole script to get out useless.

This new project uses the same script as the first one I gave you, but only records variables if you are not in a green spot.

Use this one instead.
http://scratch.mit.edu/projects/archtest/374330


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#22 2009-01-07 23:12:55

Purplewalrus
Scratcher
Registered: 2008-12-30
Posts: 20

Re: How can I keep a sprite from moving into certain areas?

OMG!!! Thank you sooo much! It finally works now!!! :-)

Offline

 

#23 2009-01-07 23:16:03

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

Amazing how I was able to figure all those things out eh?

I guess making all those program flowcharts and tests in computer science paid off  smile

The key to solving problems to to first see where the script is going wrong then to isolate the faulty code and fix it. Remember that  wink

Last edited by archmage (2009-01-07 23:16:25)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#24 2009-01-08 13:43:17

yambanshee
Scratcher
Registered: 2007-11-06
Posts: 500+

Re: How can I keep a sprite from moving into certain areas?

if only i saw this earlier. This is a very helpfull detection coding method as the sprite doesint apear to bounce, i use it all the time and its the perfect soltion, even to slow old scratch. (also arch, simply moving the colour hitTest before the variable reset should solve it aswell, although ive never tried it like that.

Offline

 

#25 2009-01-08 15:52:46

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: How can I keep a sprite from moving into certain areas?

yambanshee wrote:

(also arch, simply moving the colour hitTest before the variable reset should solve it aswell, although ive never tried it like that.

Tried it, didn't work.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

Board footer