How do I make it so my sword rotates towards my mouse cursor, but stays on the player.
You have to publish the game first if you would like someone to check your code.
That link is needed :)
Just don't tick the 'List in explorer ' box while you still work on it.
On the phone, so can't provide much more of an example, but try this:
init = function()
player = object end
player.x = 0
player.y = 0
player.speed = 5
player.sword = "player_sword"
player.angle = 0 // angle for the sword
end
update = function()
// direction check, short form without loops (just maths...)
player.y += (keyboard.W-keyboard.S)*player.speed
player.x += (keyboard.D-keyboard.A)*player.speed
// calculating the angle from the player to the mouse position
// +30 is the offset for the sword position
player.angle = getAngle(player.x+30,player.y,mouse.x,mouse.y)-45 // the sword is drawn under 45 degree
end
draw = function()
screen.clear()
screen.drawSprite("player", player.x, player.y, 40, 40 )
screen.setDrawRotation(player.angle) // change the drawing angle for the sword
screen.drawSprite("player_sword", player.x+30, player.y, 40, 40)
screen.setDrawRotation(0) // reset
end
// helper function
// finds the angle in degrees from point 1 to point 2
getAngle = function(x1, y1, x2, y2)
return atan2d(y2 - y1, x2 - x1)
end
I added an extra parameter for the angle of the sword and a helper function that calculates the angle between 2 points.
In this case that will be the player position and the mouse position.
I hope that gives you some initial ideas.
Live Long and Tinker
https://microstudio.io/i/Loginus/actoratan2/ - my version of how to do it to rotate the sword.
tysm guys that really helped :D
wait how did you check my code anyways?