JeffK Posted September 22, 2021 Share Posted September 22, 2021 if i wanted to move a sprite around a page but be restricted by boundaries is there a way to do it like you can with the draggable object. for example if you want to move the sprite around a simple maze - see codepen See the Pen QWgBrqN by jkeys (@jkeys) on CodePen Link to comment Share on other sites More sharing options...
elegantseagulls Posted September 23, 2021 Share Posted September 23, 2021 You'll have to create some logic that looks at current x and y positions and limits the next next move if the += goes over that. Something like: if (currentXPosition + xMov > xBoundry) { xMov = `+=${xBoundry - currentXPosition}` } else { xMove = `+=${moveUnit}` } or better yet, you could use the gsap clamp utility to keep your position within certain bounds: https://greensock.com/docs/v3/GSAP/UtilityMethods/clamp() gsap.to(el, { x: gsap.utils.clamp(0, 900, currentPosition + moveUnit) }) 4 Link to comment Share on other sites More sharing options...
JeffK Posted September 23, 2021 Author Share Posted September 23, 2021 Cool thanks. I didn’t know about clamp! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now