Hello, I have been an unexperienced self learner of programming and I am stuck trying to rotate a sprite (or anything) on the same spot of the screen. I tried learning about the rotate command and it seems to be rotating the coordinate the sprite is drawn instead and it ends up showing my sprite moving while rotating.
If anyone have the time and understand my confusion, please kindly post up how I can do that. Thank you very much >.<
The rotate() command rotates the entire screen around the origin (0, 0). Or whatever the origin is set to using translate() and scale(). So to rotate a sprite about its center, you can do the following:
function draw()
background(0)
-- The position of your sprite
x = 300
y = 200
translate( x , y )
rotate( angle ) -- Rotate by some angle
sprite( "Your Sprite:Here", 0, 0 ) -- Sprite is drawn at 0, 0
end
To be more versatile, you can wrap it up in a class like this:
Sprite = class()
function Sprite:init( name )
self.name = name
self.position = vec2(0,0)
self.angle = 0
end
function Sprite:draw()
pushMatrix()
translate( self.position.x, self.position.y )
rotate( self.angle )
sprite( self.name, 0, 0 )
popMatrix()
end
Then you can use it like this:
function setup()
mySprite = Sprite( "Planet Cute:Character Boy" )
mySprite.position = vec2( 300, 200 )
mySprite.angle = 45
end
function draw()
background(0)
mySprite:draw()
end
@Iruizlopez137 .Experts in this forum to learn and share codes implement new features every day, if you want a game that you like I invite you to search applestore made codea games (two lifes lefts)