The best way to get out of a trap is to not get into one in the first place. To avoid traps, keep your head up and keep it on a swivel. If you see someone coming in for the trap, remember to keep your dribble alive and look for the open man. When your teammate sees the trap coming, they should immediately cut for the basket. Look for them and hit them in stride.
Search