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

#1 2013-04-05 17:54:20

dusty22
Scratcher
Registered: 2012-09-28
Posts: 49

Almost exact floor/landing detection for a platformer?

So I have a platformer. And it has good floor detection. I never fall through the floor. But landings are always a little unpredictable. I never land exactly at the same height on a level surface. I might land at the very tip top. Or I might touch down slightly lower before it registers. Anyway I can make it more exact/uniform?

I use a sensor sprite at the bottom of the player's sprite to find the floor. Basically this is what it looks like:

forever
 if <<touching [ground_l]> or <touching [ground_r]>>
  if <jumping = 0>
   set [grounded] to 1
    if <grounded = 1>
     set [y velocity] to 0
    end
   end
 end
end
forever
 set x position to <[x position] of [playersprite]>
 set y position to <<[y position] of [playersprite]> - 16>
end
Y velocity is like the gravity. If grounded = 0 (which means the sprite isn't on the floor) it's set to around -5/6, just to clarify.

Last edited by dusty22 (2013-04-05 17:56:11)

Offline

 

#2 2013-04-05 18:04:04

xlk
Scratcher
Registered: 2013-03-18
Posts: 57

Re: Almost exact floor/landing detection for a platformer?

You could make it so as soon as the sprite touches ground, make a bunch of "if" s that move it up one pixel:

forever
 if <touching [ground]?>
  change y by [1]
 end
 if <touching [ground]?>
  change y by [1]
 end
 if <touching [ground]?>
  change y by [1]
 end
 if <touching [ground]?>
  change y by [1]
 end
 if <touching [ground]?>
  change y by [1]
 end
 if <touching [ground]?>
  change y by [1]
 end
 if <touching [ground]?>
  change y by [1]
 end
 if <touching [ground]?>
  change y by [1]
 end
end

Offline

 

#3 2013-04-05 18:16:54

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Almost exact floor/landing detection for a platformer?

xlk wrote:

You could make it so as soon as the sprite touches ground, make a bunch of "if" s that move it up one pixel:

forever
 if <touching [ground v]?>
  change y by [1]
 end
 if <touching [ground v]?>
  change y by [1]
 end
 if <touching [ground v]?>
  change y by [1]
 end
 if <touching [ground v]?>
  change y by [1]
 end
 if <touching [ground v]?>
  change y by [1]
 end
 if <touching [ground v]?>
  change y by [1]
 end
 if <touching [ground v]?>
  change y by [1]
 end
 if <touching [ground v]?>
  change y by [1]
 end
end

Fixed, you just needed some "v"s after the "grounds".

Regards,

CAA14

Offline

 

#4 2013-04-05 19:04:45

dusty22
Scratcher
Registered: 2012-09-28
Posts: 49

Re: Almost exact floor/landing detection for a platformer?

At first I didn't understand, but I get it now. It keeps moving up until it's on top of the ground rather than in it. Thanks!

Offline

 

#5 2013-04-05 19:25:49

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Almost exact floor/landing detection for a platformer?

My only comment is that you shouldn't need more than one if statement if it's in a forever loop...

Regards,

CAA14

Offline

 

#6 2013-04-06 06:35:33

xlk
Scratcher
Registered: 2013-03-18
Posts: 57

Re: Almost exact floor/landing detection for a platformer?

but his problem is the sprite appearing in the ground, having many of them will make it move out of the ground, if it can in 8 one pixel moves, instantaneously, so the player won't see the sprite sink temporarily in the ground.
BTW, remember to have as many of those as the fastest downwards speed (EG, if the max. falling speed is -9, have 9 of them, as it can end up up to 9 pixels into the ground). And, make sure it doesn’t happen when hitting a ceiling(!).

Offline

 

Board footer