Discuss Scratch

PJFAUST
Scratcher
33 posts

How do I make a sprite not go through a wall?

I am still working on my tank game and thanks to help from fellow scratchers, I managed to make the tank shoot in the correct direction. But now I need help with walls. Simple wall scripts don't work because the tank can reverse and hit the walls with different sides so please help. Here is the link for the game I am talking about. If the link doesn't work, just click my name and click the tank battle project.

https://scratch.mit.edu/projects/156889896/#player
Copy this link in the url bar.
RokCoder
Scratcher
1000+ posts

How do I make a sprite not go through a wall?

In the player sprite you have two problems. You can collide with the wall by moving forwards or backwards so you need to reverse that movement if a collision occurs. But you can also collide with the wall by rotating left or right so you also need to reverse the rotation if a collision occurs -

when green flag clicked
forever
set [Speed v] to [0]
if <key [up arrow v] pressed?> then
change [Speed v] by (-3)
end
if <key [down arrow v] pressed?> then
change [Speed v] by (3)
end
move (Speed) steps
if <touching [Sprite4 v] ?> then
move ((0) - (Speed)) steps
end
set [Turn v] to [0]
if <key [right arrow v] pressed?> then
change [Turn v] by (3)
end
if <key [left arrow v] pressed?> then
change [Turn v] by (-3)
end
turn cw (Turn) degrees
if <touching [Sprite4 v] ?> then
turn ccw (Turn) degrees
end
if <<(Tank Health) = [0]> or <(Tank Health) < [0]>> then
broadcast [Player Dead v] and wait
end
end

In the player's turret you were rotating manually to match the player direction by checking for the left or right arrow keys. But now that we can stop the rotation if there's a collision, the turret would still rotate if the arrow keys were pressed. So we need to change the code so that the direction of the turret is stored as a delta angle to that of the tank -

when green flag clicked
forever
if <key [a v] pressed?> then
change [Gun Delta v] by (-3)
end
if <key [d v] pressed?> then
change [Gun Delta v] by (3)
end
point in direction (([direction v] of [Evader PL-1 (Player) v]) + (Gun Delta))
if <key [space v] pressed?> then
switch costume to [costume1 v]
repeat (4)
wait (0.03) secs
next costume
end
broadcast [Fire v]
wait (0.8) secs
end
end

You now have the issue of the turret jumping after firing a bullet if the player has continued turning. There are several ways to solve that but I'll leave that with you
Sprinternet
Scratcher
69 posts

How do I make a sprite not go through a wall?

The frame sprite (Sprite4 in project) is that who is making issue. You will need this script in Sprite4 sprite:
when green flag clicked
forever
if <<touching [tank hull v] ?> or <touching [tank turret v] ?>> then
broadcast [touching wall v]

end
I even think you should remove Sprite4 because it is used for nothing - you better draw it in Stage
Quiear12
Scratcher
1 post

How do I make a sprite not go through a wall?

Sprinternet wrote:

The frame sprite (Sprite4 in project) is that who is making issue. You will need this script in Sprite4 sprite:
when green flag clicked
forever
if <<touching [tank hull v] ?> or <touching [tank turret v] ?>> then
broadcast [touching wall v]

end
I even think you should remove Sprite4 because it is used for nothing - you better draw it in Stage
yes
musaj-tcsr
Scratcher
19 posts

How do I make a sprite not go through a wall?

This is a very simple way of making non-go through walls! check it out!

https://scratch.mit.edu/projects/1142284005/
beluga_me2official
Scratcher
0 posts

How do I make a sprite not go through a wall?

tmkayoutube
New Scratcher
1 post

How do I make a sprite not go through a wall?

67

Powered by DjangoBB