New, need help with simple stuff

Hey guys, 27 male, wanted to do something different in my life besides managing networks. So i decided to start coding and I found codea. Its a pretty awesome LUA tool. I have been struggling with moving this stupid sprite.
All ive figured out so far is to hold the screen and itll move around. But i just wanna do a click and it goes to my click.
So far ive got

Function draw()

Sprite("example",spx,spy,HEIGHT/2,WIDTH/2) --typing this from memory could be wrong haha


Function touched(touch)

If touch.state == Began then

  If spx > touch.x then


  Elseif spx<touch.x then




^A quick example.
This will move the character as long you keep tapping. But i just wanna do one tap, and watch the sprite move across the screen to touch.x

I havent programmed since i was a young kid in VB. so no timers and not understanding a loop system.
Any advice is appreciated. And guys. You linked a complete RPG to a 12 yr old, i looked over it, and closed it. Right…im just saying it was funny. Please dont link an entire game and expect me to understand the touch portions when theyve used 50 variables declared across 100s of line code. !D

Also what is the vec and vec2 functions all about?
Ill keep reading, brand new coder here, remember.
One thing ive noticed about LUA is how simple it is, compared to things in the past. Ive way over complicated it. It took me a day to figure out the stuff. Im slow, call me charlie kelly(Day)!!

Charlie - greetings from a fellow VB user. Lua is very like VB, but simpler (and better, I reckon).

To make the sprite move smoothly across the screen from x1,y1 to x2,y2, you have to decide the speed you want it to go, remembering the screen redraws 60 times per second. Then you use (fairly) simple trigonometry to figure out where it’s go to each time Codea redraws.

If the angle you are moving at is a [unknown], then tan(a) =(y2-y1)/(x2-x1), so we can solve for
a = math.atan((y2-y1)/(x2-x1))

Then if you want to move at a speed of p pixels per redraw, the x and y changes are (again by trig)

until you reach x2,y2

If you want to learn fastest, I suggest you do what I did (I started out the same as you, a few months ago). Read the Lua docs (see wiki link at top), then do all the tutorials you can find. I have tried to help by writing a heap more tutorials, including a Lua ebook - see link below.

Ive been reading all your stuff man, and i appreciate it alot, but can you write it as a function so i can understand better sir? Thank you very much

@Ignatz I’d say angles are overkill here (I’m a firm believer that angles are overused in general so it’s nothing personal!). To move from (x1,y1) to (x2,y2) use the formula:

t |-> (1 - t) * (x1,y1) + t * (x2,y2)

That will go from (x1,y1) at t = 0 to (x2,y2) at t = 1. To scale the time so a specific pixels-per-second or pixels-per-frame then you just need to know the distance between the two points, which is math.sqrt((x2 - x1)^2 + (y2 - y1)^2) or can be computed using the dist method of the vec2 object.

Lol okay if this could be written as a function so i understand, im just about to give up i think.

Function touched(touch)
Ya nvm i give up, thanks.

@Andrew_Stacey - it works either way. But I must admit I also prefer to work with x and y values rather than angles.

@Nabbie - the changes in x and y need to be calculated in the draw function, so a separate function isn’t going to be much help.

@Ignatz I know it works either way. The point is that using angles is an unnecessary extra. It is, I’ll admit, small. But, for example, in your pseudo-code then you are computing cos(a) afresh every frame. Now, you could (and should) cache it whereupon the saving is the difference between computing cos(tan^{-1}(y/x)) and x/sqrt(x^2 + y^2). I would expect the latter to be faster and more accurate. It’s not that either is likely to be significant, but it might be if there are a lot of these being done (say with vertices of a very large mesh).

@Nabbie this is a very simple example that will use the tween animation function to move a circle to wherever you tap on the screen. Hopefully it helps.

--# Main
-- TweenTo

function setup()
    -- Don't need the sidebar
    displayMode( FULLSCREEN )
    -- Global position of the circle
    pos = vec2(0,0)    

-- This function gets called once every frame
function draw()

    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness

    -- Draw a red ellipse at 'pos'
    fill( 255, 0, 0 )
    ellipse( pos.x, pos.y, 100 )

function touched(touch)
    -- If the user taps the screen once
    if touch.tapCount == 1 and
       touch.state == ENDED then
        -- Animate 'pos' to the touch position
        tween( 0.5, pos, {x = touch.x, y = touch.y}, tween.easing.backOut )



I’d also like to add my $0.02 to @Nabbie - but to say have a look again at the examples provided with Codea, they do cover pretty much all the things you need to know (that and a good intro to Lua).

Remember - the way Codea is written, it just provides an empty framework so if you just create a new project and run it - nothing happens (or at least nothing you can see), behind the scenes a LOT is happening (and happening repeatedly), it’s just that you haven’t added your own $0.02 the party yet!

The Codea framework will call the setup() function ONCE when your lua script starts - you’ll use this function to setup / initialise all your stuff for your app (you can have stuff be executed outside of this in the main script but we’ll leave that for now).

Then once “per frame” (think of a frame like an individual frame in a movie - as each still image frame passes over the projector lens it’s shown on the screen, by changing what the still image frame displays a little bit each time the illusion of movement is created), the Codea framework will call the “draw()” function to create a “still image”, this is where you get to work your magic, by drawing objects in slightly different positions every time the draw() function is called (ie use variables to hold screen positions) you also create the illusion of movement. The only difference is that film usually show’s 24 frames per second and Codea will attempt to show 60.

As for input, there are two ways to handle this - the easiest way is to look at the contents of the “currentTouch” table in your “draw()” function, currentTouch holds the details of the last touch event that occurred so you can see if any touch has occurred since the last time the draw() function was called - this is fine for simple examples and to use a single touch.

To handle “multi-touch”, the Codea framework provides the “touched()” function which is called EVERY time a touch event occurs - if you touch the screen in five places at the same time, this function will be called five times, each touch event has an ID associated with it so you can track the movement of each individual point (a finger) as it touches / moves and is lifted from the screen. I’d leave off multitouch to start with and have a look at the multitouch example when you feel ready!