Difference between revisions of "How to point a sprite at a target"
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
== Example == | == Example == | ||
+ | This example loads an arrow sprite, and makes it continuously point at the mouse. Press Control-C to exit. | ||
<ms>clear | <ms>clear |
Revision as of 16:35, 6 March 2020
To point a sprite at a target, use the atan function to calculate the angle in radians, then multiply by 180/pi to convert to degrees, and assign the result to the sprite's .rotation property.
See also How to move a sprite towards a target.
Example
This example loads an arrow sprite, and makes it continuously point at the mouse. Press Control-C to exit.
clear
sp = new Sprite
sp.image = file.loadImage("/sys/pics/arrows/arrow2.png")
sp.x = 480
sp.y = 320
display(4).sprites.push sp
while true // (press Control-C to exit)
sp.rotation = atan(mouse.y - sp.y, mouse.x - sp.x) * 180/pi
yield
end while