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

#1 2013-01-21 16:57:57

Fyrox
New Scratcher
Registered: 2013-01-21
Posts: 17

First movement script (with velocity and gravity)

Hello, I just started with Scratch and I'm planning on doing a platform game. I decided to script the sprite's movement first and here's what I got:

when gf clicked
set [y velocity] to [0]
set [x velocity] to [0]
forever
if <touching color [ground color]?>
  if <key [up arrow v] pressed?>
    set [y velocity v] to (10)
  else
    set [y velocity v] to (0)
  end
 else
   change [y velocity v] by (-1)
end
if <key [right arrow v] pressed?>
   set [x velocity v] to (6)
end
if <key [left arrow v] pressed?>
   set [x velocity v] to (-6)
end
if  <&lt;not &lt;key [left arrow v] pressed?&gt;&gt; and &lt;not &lt;key [right arrow v] pressed?&gt;&gt;>
set [x velocity] to ( (x velocity) * (0.92) )
end
change y by (y velocity)
change x by (x velocity)
 
My question is: are there easiest/better ways to do this? As far as I've tested, it works correctly, but I don't know if I'm doing it the right way.

Offline

 

#2 2013-01-21 17:57:46

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

Re: First movement script (with velocity and gravity)

It depends on what you want the script to do.  There are more advanced scripts that can do wall detection, double jumping, etc, but if you only want ground detection and movement with acceleration, your script looks like the easiest way to me.

I'd like to point out that, if you're only using "friction" if the left and right keys aren't pressed, there is no limit on the speed the user can get to.


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

Offline

 

#3 2013-01-21 18:24:26

Fyrox
New Scratcher
Registered: 2013-01-21
Posts: 17

Re: First movement script (with velocity and gravity)

MoreGamesNow wrote:

I'd like to point out that, if you're only using "friction" if the left and right keys aren't pressed, there is no limit on the speed the user can get to.

What do you mean by that? I didn't understand that very well to be honest.

Offline

 

#4 2013-01-21 19:59:49

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

Re: First movement script (with velocity and gravity)

Fyrox wrote:

MoreGamesNow wrote:

I'd like to point out that, if you're only using "friction" if the left and right keys aren't pressed, there is no limit on the speed the user can get to.

What do you mean by that? I didn't understand that very well to be honest.

If I just hold down the right arrow, the x velocity will continue to increase without a limit.


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

Offline

 

#5 2013-01-21 20:46:50

AgentCNF
Scratcher
Registered: 2012-08-27
Posts: 21

Re: First movement script (with velocity and gravity)

MoreGamesNow wrote:

Fyrox wrote:

MoreGamesNow wrote:

I'd like to point out that, if you're only using "friction" if the left and right keys aren't pressed, there is no limit on the speed the user can get to.

What do you mean by that? I didn't understand that very well to be honest.

If I just hold down the right arrow, the x velocity will continue to increase without a limit.

Yep. The speed will keep increasing until you stop holding the arrow key.


Go St. Louis Cardinals!!! I Hate The Cubs!

Offline

 

#6 2013-01-22 12:37:18

Fyrox
New Scratcher
Registered: 2013-01-21
Posts: 17

Re: First movement script (with velocity and gravity)

I've tested it and x velocity doesn't increase. Maybe I misunderstood something, but I'm using "set" to modify the x velocity value and not "change by".

Offline

 

#7 2013-01-22 13:51:30

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

Re: First movement script (with velocity and gravity)

Ah.  I missed that.  My bad  smile


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

Offline

 

#8 2013-01-22 14:36:08

Fyrox
New Scratcher
Registered: 2013-01-21
Posts: 17

Re: First movement script (with velocity and gravity)

No problem  tongue  Alright then, everything is fine now but I got one last question: To animate the sprite, I should place the animation script in another "when GreenFlag clicked"?

Offline

 

#9 2013-01-22 17:06:28

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

Re: First movement script (with velocity and gravity)

Yes, that would probably be easiest.  I look forward to seeing what this is all going towards  smile


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

Offline

 

#10 2013-01-22 17:28:19

Fyrox
New Scratcher
Registered: 2013-01-21
Posts: 17

Re: First movement script (with velocity and gravity)

Well I basically want to make a cool simple platform game just to learn a bit of Scratch since I just started with it, nothing really special. I've almost finished the first part of the game (movement, animation and a little point system). Later I'll have to deal with wall detection and stuff.

Offline

 

Board footer