i have several different sprites , they are in different positions of the screen .
can i just create ONE animator for all these sprites ? and the animator run in relative coordinates .
for example , all the sprites move from their current position to offset -10x pixels ?
This may help you:
And you can still choose to draw some things using the absolute screen coordinates. For example:
thank u Morgan , it work . but seem not so work . here is my code:
local square = playdate.graphics.image.new(20, 20, playdate.graphics.kColorBlack)
local square_small = playdate.graphics.image.new(10, 10, playdate.graphics.kColorBlack)
local animationDuration = 1000
local line = playdate.geometry.lineSegment.new(0, 0, 300, 0)
local animator_line = playdate.graphics.animator.new(animationDuration, line)
animator_line.repeatCount = 3
And in the playdate.update()
playdate.graphics.setDrawOffset(20,20)
square:draw(animator_line:currentValue())
playdate.graphics.setDrawOffset(20,50)
square_small:draw(animator_line:currentValue())
yes , all work . but ,
can i just create ONE animator_line ( that should make a left moving animation) , then image:draw with it . isnt that set the offset for every drawing a bit clumsy ? sorry for my noob question .
Also , if i used sprites , here is my codes:
local square = playdate.graphics.image.new(20, 20, playdate.graphics.kColorBlack)
local squareSprite = playdate.graphics.sprite.new(square)
squareSprite:moveTo(20, 90)
local line = playdate.geometry.lineSegment.new(0, 0, 300, 0)
local animator_line = playdate.graphics.animator.new(animationDuration, line)
animator_line.repeatCount = 3
squareSprite:setAnimator(animator_line)
this also will move the sprite from (0,0) to (300,0) , but all i need is move the square from (20,90) to (300,90). and seem in the sprites situation , the setDrawOffset is not work .
Rather than assigning the animator to the sprites directly, might it make more sense for you to just read the values from the animator directly, and apply those values frame-by-frame to offset your sprites in the way that you want?
thank u dan , i follow your guide.
at first , i try to pass the animator as a param for the sprite init() .some code like this:
function MySprite:init(x, y, animator)
MySprite.super.init(self)
local image = gfx.image.new(20, 20, gfx.kColorBlack)
self:setImage(image)
self.animator = animator
self:moveTo(x, y)
end
but seem the animator pass in will be a reference . so this is not right , if i need my sprites to share the same animator(in my scene , let the user to control which sprite should play the animation) , seem i need to put the animator.new in the init() of the subClass of the sprite . (or may be a class with the animator, and make all my sprite inherited from it ) , am i right ?
this is my last coe: ( it work , but i am not sure is the right way )
local gfx = playdate.graphics
class('MySprite').extends(gfx.sprite)
function MySprite:init(x, y)
MySprite.super.init(self)
local image = gfx.image.new(20, 20, gfx.kColorBlack)
self:setImage(image)
self.animationDuration = 1000
self.startX, self.endX = 0, 100
self.easingFunction = playdate.easingFunctions.inOutCubic
self.animator = gfx.animator.new(self.animationDuration, self.startX, self.endX, self.easingFunction)
self.original_x = x
self.original_y = y
self:moveTo(x, y)
end
function MySprite:update()
if self.animator:ended() ~= true then
self:moveTo(self.original_x + self.animator:currentValue(), self.original_y)
print(self.animator:currentValue())
end
end
local sprite_1 = MySprite(100, 100)
sprite_1:add()
local sprite_2 = MySprite(100, 200)
sprite_2:add()
function playdate.update()
gfx.sprite.update()
end
function playdate.AButtonDown()
sprite_1.animator:reset()
end