Difference between revisions of "How to point a sprite at a target"
Jump to navigation
Jump to search
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | 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 [[Sprite.rotation|.rotation]] property. | + | To point a [[Sprite|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 [[Sprite.rotation|.rotation]] property. |
See also [[How to move a sprite towards a target]]. | See also [[How to move a sprite towards a target]]. | ||
== 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 |
Latest revision as of 20:03, 20 November 2021
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