Hey guys, so I have a ball and a direction set up depending on where you touch the screen but I don’t know how to set up the direction with the object on the screen. Here’s the code:
-- Lines1
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
held=false
directionx=0
directiony=0
speedx=WIDTH/2
speedy=HEIGHT/2
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(95, 112, 115, 255)
fill(255, 255, 255, 255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
ellipse(ballx,bally,40)
if held == true then
line(ballx,bally,CurrentTouch.x,CurrentTouch.y)
end
end
function touched(touch)
if touch.state == BEGAN then
held=true
direction=vec2(touch.prevX,touch.prevY)
end
if touch.state == ENDED then
held=false
directionx=touch.prevX
directiony=touch.prevY
end
end
I suggest you separate speed and direction, then you can calculate the direction from your current position to the touch like this
speed=10 --10 pixels per draw cycle
--i suggest you store current position in a vec2 like this
position=vec2(100,200) --how you set the initial position
direction=vec2(0,0) --no initial direction
--after a user touch, recalculate direction
direction=(vec2(touch.x,touch.y)-position):normalize()
--to move the user
position=position+direction*speed
and here is your amended program. If anything isn’t clear,ask.
-- Lines1
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
held=false
direction=vec2(0,0)
speed=3
position=vec2(WIDTH/2,HEIGHT/2)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(95, 112, 115, 255)
fill(255, 255, 255, 255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
position=position+direction*speed
ellipse(position.x,position.y,40)
if held == true then
line(position.x,position.y,CurrentTouch.x,CurrentTouch.y)
end
end
function touched(touch)
if touch.state == BEGAN then
held=true
end
if touch.state == ENDED then
held=false
direction=(vec2(touch.x,touch.y)-position):normalize()
end
end