Discuss Scratch

-EndlessSpace-
New Scratcher
13 posts

How to stop a sprite after collision?

Hey, I'm working on my first platformer, and I was wondering how to stop a sprite after it collides with another sprite. I'm using variables like X Velocity and Y Velocity, so I don't want those to be broken after a collision. Does anyone know how?
enderbrine5858
Scratcher
13 posts

How to stop a sprite after collision?

Well, since you have a collisioncheck with the ground, just add the same collision script to the ground that you do the other sprite. If you want it to bounce the other direction, then change your Xvelocity (or Y!) to a negative number instead of 0. So, if your ground script is (to put it simply) When I touch the ground, stop my movement on the Y axis downwards, basically saying (if jumping sets your Yvelocity to 12, for instance, and you have a forever block always making it go down by .1, persay) Then when you want your character sprite to jump back instead of just stopping, you would want to change your Xvelocity (if it's coming from the side) to, let's say, -4. This should bounce it back far enough to be effective, without it being annoying.

You might also want to post a link to your project in order for people to see what the problem is.

Last edited by enderbrine5858 (March 20, 2020 15:09:53)

StrangeMagic32
Scratcher
1000+ posts

How to stop a sprite after collision?

Here's a project that may help
https://scratch.mit.edu/projects/378101950/

Last edited by StrangeMagic32 (March 20, 2020 15:22:29)

-EndlessSpace-
New Scratcher
13 posts

How to stop a sprite after collision?

@StrangeMagic32 - The link doesn't seem to work.
@enderbrine5858 - Would it help if I sent the script? I'm still having some trouble.
Y Vel Script -
when green flag clicked
if <touching color [#c06e77] ?> then
if <key [ up arrow] pressed?> then
set [YVel] to [12]



else
set [YVel] to [0]
end

else
change [YVel] by (-1)

end

XVel Script -
when green flag clicked
if <<touching [ Block1] ?> or <touching [ Block2] ?>> then
set [XVel] to [-4
repeat until <[XVel] = [0]>
change [ XVel] by (1)
end
end

Sorry if my blocks are inefficient or inaccurate. Making blocks in the forum messages is kinda hard.

Last edited by -EndlessSpace- (March 20, 2020 17:00:17)

-EndlessSpace-
New Scratcher
13 posts

How to stop a sprite after collision?

There's also forever blocks around those scripts.
PersRufaro
Scratcher
100+ posts

How to stop a sprite after collision?

Maybe try this script:
when gf clicked
wait until<touching [(Sprite for stopping script with v] ?>
stop [other scripts in sprite v]

Last edited by PersRufaro (March 20, 2020 17:10:47)

-EndlessSpace-
New Scratcher
13 posts

How to stop a sprite after collision?

Actually, I just want collision detection so it doesn't go past the block, but can still move.
BroOn4tn1t
Scratcher
39 posts

How to stop a sprite after collision?

if you are trying to do the collision with two sprites and make one of the sprites disappear, try this. Hope it works!

when green flag clicked
forever
if <touching [random sprite v] > then
hide
end
end

Last edited by BroOn4tn1t (March 22, 2020 16:14:48)

StrangeMagic32
Scratcher
1000+ posts

How to stop a sprite after collision?

-EndlessSpace- wrote:

@StrangeMagic32 - The link doesn't seem to work.
woops, it's shared now

https://scratch.mit.edu/projects/378101950/

Last edited by StrangeMagic32 (March 20, 2020 17:24:20)

enderbrine5858
Scratcher
13 posts

How to stop a sprite after collision?

Could you post a link to the project here? If you give us the project, we'll be able to help you a lot more. I'll even be willing to remix it and fix it up for you, so you could look at mine to see what I did.

Last edited by enderbrine5858 (March 20, 2020 19:38:54)

Tallented-Code-bot
Scratcher
100+ posts

How to stop a sprite after collision?

Here is a script that will work, this is for gravity and ground detection (for the player sprite).
Remember this script is for the player sprite.


when green flag clicked
set [ Y vel] to [0]
forever
change y by (Y vel)
change [Y vel] by (-1)// This makes gravity.
if <touching [ Ground ] ?> then
set [ Y vel] to [ 0]
change y by (1)// This makes the player sprite be one pixel above the Ground
end // sprite so you are not always touching the ground.
end




define Touch Ground
repeat until <not <touching [ Ground] ?>>
set [ Y vel] to [0]
change y by (1)
end

Last edited by Tallented-Code-bot (March 20, 2020 20:21:57)

-EndlessSpace-
New Scratcher
13 posts

How to stop a sprite after collision?

Alright, here's the project.The collision is on level 4. Also, the controls are little touchy, and if it helps, I made the variables visible.
https://scratch.mit.edu/projects/377475239/
enderbrine5858
Scratcher
13 posts

How to stop a sprite after collision?

From what I can see from your code, there should be no problem with integrating Block1 and Block2 into your backdrops.

You say that you want to be able to move after you touch the block, are you saying a walljump feature?

I'll remix this and fix up your code.

Last edited by enderbrine5858 (March 21, 2020 15:01:32)

-EndlessSpace-
New Scratcher
13 posts

How to stop a sprite after collision?

Walljump would be nice, but I mean the sprite moving without it being all jerky. Does that make sense?
enderbrine5858
Scratcher
13 posts

How to stop a sprite after collision?

Yes, I'm working on it now. You can see the project so far by clicking here.

The game's all done. You can remix it or copy it.

Last edited by enderbrine5858 (March 21, 2020 16:09:37)

Tesla0511
Scratcher
22 posts

How to stop a sprite after collision?

nvm

Last edited by Tesla0511 (Oct. 5, 2022 09:52:47)

Powered by DjangoBB