CodeWalrus

Development => Calculators => Calculator News, Coding, Help & Talk => Topic started by: Strontium on April 22, 2015, 11:06:39 PM

Title: TI Nspire Lua things
Post by: Strontium on April 22, 2015, 11:06:39 PM
I've been messing around with Lua on my TI Nspire, and made a thing that allows you to move a square around and draw a line when you press 6. The code, so far, is as follows:
player_pos = {100, 100}
atacking = false

function attackLeft()
    if attacking == false then
        attacking = true
    else
        attacking = false
    end
end

function on.arrowUp()
    player_pos[1] = player_pos[1] - 1
end

function on.arrowDown()
    player_pos[1] = player_pos[1] + 1
end

function on.arrowLeft()
    player_pos[2] = player_pos[2] - 1
end

function on.arrowRight()
    player_pos[2] = player_pos[2] + 1
end

function on.charIn(char)
    if char == "6" then
        attackLeft()
    end
end

function on.timer()
    platform.window:invalidate()
end

timer.start(0.1)

function on.paint(gc)
    gc:drawRect(player_pos[2], player_pos[1], 20, 20)
    if attacking == true then
        gc:drawLine(player_pos[2] + 20, player_pos[1] + 10, player_pos[2] + 75, player_pos[1] + 10)
    end
end


Its just a prototype I hacked together. Some things in it dont even make sense (for example, how I dealt with the player position). However, there is something I have a question about:

When I hold down any of the direction arrows, then press 6 to draw the line, the player stops moving in x direction. I know why this is, though, its because when I press 6 the program stops receiving input from the arrows. However, I want to be able to continue moving even after I press 6. How would I do this?

Edit: Also, is there a function that prevents the calc asking you if you want to save when you close out the application?
Title: Re: TI Nspire Lua things
Post by: alexgt on April 22, 2015, 11:14:55 PM
Can't give you help as I don't know about Lua but I wish you luck, sorry :(
Title: Re: TI Nspire Lua things
Post by: Jonius7 on May 07, 2015, 11:25:26 PM
Perhaps this is worth a try:

Use on.arrowKey(key) and then call it in your on.charIn(char)


function on.arrowKey(key)
    if key == "up" then
        player_pos[1] = player_pos[1] - 1
    end

    if key == "down" then
        player_pos[1] = player_pos[1] + 1
    end

    if key == "left" then
        player_pos[2] = player_pos[2] - 1
    end

    if key == "right" then
        player_pos[2] = player_pos[2] + 1
    end
end

function on.charIn(char)
    if char == "6" then
        attackLeft()
        on.arrowKey(key) -- actually this might not work
    end
end
Title: Re: TI Nspire Lua things
Post by: Strontium on May 07, 2015, 11:35:54 PM
You'd have to call on.charIn(char) for that. But besides that, on.arrowKey is something that is called whenever you press the arrow keys on the Nspire. Furthermore, since this still using on.charIn pretty much the same way as before, I don't think it will work.
Title: Re: TI Nspire Lua things
Post by: Jonius7 on May 08, 2015, 12:15:37 AM
Perhaps pass the last arrow key you pressed to another variable, and then check the value of that variable when you press "6", so you can continue the moving code accordingly.