Hi.
Just started making my 1st game. Can you please help me make my character move from where it is now to a position on screen that has been touched? I want it to move to the new position smoothly and not too fast.
Thank you!
Your player might be in a object , a instance of a class.
For example,
- Main file
function setup()
player = Player(
{
position = vec2( WIDTH/2, HEIGHT/2)
}
)
end
function draw()
background( 40, 40, 50)
-- background grass :D
sprite("Planet Cute:Grass Block", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
-- draw player in its current position
player:draw()
end
function touched(touch)
player:touched(touch)
end
- Player class
Player = class()
function Player:init(args)
self.position = args.position
self.destiny = args.position
self.speed = vec2(0,0)
self.increment = 1.0 -- speed increment
end
function Player:updatePosition()
local p = vec2(math.floor(self.position.x), math.floor(self.position.y))
local d = vec2(math.floor(self.destiny.x), math.floor(self.destiny.y))
if p ~= d then
local s = self.increment
-- compute speed x component
if d.x<p.x then
self.speed.x = -s
elseif d.x > p.x then
self.speed.x = s
else
self.speed.x = 0
end
-- compute speed y component
if d.y<p.y then
self.speed.y = -s
elseif d.y > p.y then
self.speed.y = s
else
self.speed.y = 0
end
-- set the new position
self.position = self.position + self.speed
-- draw destiny point
sprite("Planet Cute:Selector", self.destiny.x, self.destiny.y)
end
end
function Player:draw()
self:updatePosition()
-- draw player in the calculated position
sprite("Planet Cute:Character Horn Girl", self.position.x, self.position.y)
end
function Player:touched(touch)
-- update the destiny with the current touch
self.destiny = vec2(touch.x, touch.y)
end
hope you like it