Author Topic: Question on TI-Lua programming  (Read 4456 times)

0 Members and 1 Guest are viewing this topic.

Offline alta

  • LV0 Newcomer (Next: 5)
  • Posts: 2
  • Rating: +0/-0
    • View Profile
Question on TI-Lua programming
« on: December 31, 2012, 06:38:33 am »
I'm learning Lua on Ti-Nspire. My new calculator is a Ti Nspire CX CAS with the OS 3.2.3.1233.

The problem is that I made a small program with TI Nspire CAS Student software that works well on my PC and dosen't work on my calculator.

The error message is: "bad argument #2 to 'copy' (value must be > 0)". However, as you can see below in the first listing, the second argument is expected to be > 0 if the first lines of the script are taken into account.

I determine the changes to make in the script and it works well now on my calculator (second listing). The instructions that define h and w must be local to the on.paint(gc) event and the same thing must be done to initialize the variables px, py, wim and him

In fact, it seems that when the instruction h = platform.window:height() is define at the begining of the script out of the function on.paint(gc) to define the global variable h, the on.paint(gc) event is called before the initialization of the variable h.
Then, I tried to use the event on.construction() to define the variables h, w, px, py, wim and him, but it doesn't work on the calculator.

Here is the script that works well with the software:
The str bariable was cut to decrease the size of the string.



str = "4\002\000\000t\001\000\000\000\000\000\000h\004\000\000\016\000\001\000\254\251\\159\239\159\239\159\ ... "

local h = platform.window:height()
local w = platform.window:width()
px = w/2
py = h/2
wim = w/2
him = h/2

function on.construction()
    monImage = image.new(str)
end

function on.arrowKey(key)
    if key == "up" then
        wim = 1.1*wim
        him = 1.1*him
    elseif key == "down" then
        wim = 0.9*wim
        him = 0.9*him
    end
    platform.window:invalidate()
end

function on.paint(gc)
    monImage = image.copy(monImage, wim, him)
    gc:drawImage(monImage, px - wim/2, py - him/2)
end


Here is the script modified to work on the calculator:



str = "4\002\000\000t\001\000\000\000\000\000\000h\004\000\000\016\000\001\000\254\251\\159\239\159\239\159\ ... "

function on.construction()
    monImage = image.new(str)
    init = 0
end

function on.arrowKey(key)
    if key == "up" then
        wim = 1.1*wim
        him = 1.1*him
    elseif key == "down" then
        wim = 0.9*wim
        him = 0.9*him
    end
    platform.window:invalidate()
end

function on.paint(gc)
    if init == 0 then
        local h = platform.window:height()
        local w = platform.window:width()
        px = w/2
        py = h/2
        wim = w/2
        him = h/2
        init = 1
    end
    monImage = image.copy(monImage, wim, him)
    gc:drawImage(monImage, px - wim/2, py - him/2)
end


Could you have an explanation for this curious behavior ? I'm certainly doing a beginner mistake.
Yhanks fr your solutions.  :)

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Question on TI-Lua programming
« Reply #1 on: December 31, 2012, 06:58:49 am »
Well, it's not a beginners mistake, don't worry ;)

I'm not yet going to explain what is happening, but could you add
Code: [Select]
function on.resize(ww, hh)
 w, h = ww, hh
end

See if that helps :)
I'll explain later ^^

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Question on TI-Lua programming
« Reply #2 on: December 31, 2012, 07:01:53 am »
Does this has to do with the fact that the screenwidth, -height etc isn't set up at the start of the script? (like why my stuff wasn't working?)
« Last Edit: December 31, 2012, 07:02:37 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Question on TI-Lua programming
« Reply #3 on: December 31, 2012, 07:10:27 am »
Yes, indeed.

When just loading the script, the height and width of it are still 0, because the window is still loading. On your computer the window is already loaded.
The best is to use on.resize to set your width and height, as it will get called as soon as the window has a proper size.

Also, don't use image.copy in on.paint. The window doesn't resize every time you redraw it ^^
Rather do something like this:
Code: [Select]
str = "4\002\000\000t\001\000\000\000\000\000\000h\004\000\000\016\000\001\000\254\251\\159\239\159\239\159\ ... "

function on.construction()
    monImage = image.new(str)
end

function on.resize(width, height)
    h = heighy
    w = width
    px = w/2
    py = h/2
    wim = w/2
    him = h/2
    monImage = image.copy(monImage, wim, him)
end


function on.arrowKey(key)
    if key == "up" then
        wim = 1.1*wim
        him = 1.1*him
    elseif key == "down" then
        wim = 0.9*wim
        him = 0.9*him
    end

    platform.window:invalidate()
end

function on.paint(gc)
    gc:drawImage(monImage, px - wim/2, py - him/2)
end

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Question on TI-Lua programming
« Reply #4 on: December 31, 2012, 07:21:24 am »
Small tip for future posts: to make you post more readable use code tags like jim did above me :) click the little # icon when writing a message or use [code]code here[/code]
« Last Edit: December 31, 2012, 07:24:18 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline alta

  • LV0 Newcomer (Next: 5)
  • Posts: 2
  • Rating: +0/-0
    • View Profile
Re: Question on TI-Lua programming
« Reply #5 on: December 31, 2012, 09:44:00 am »
Thank you for your tips. It works well now. :)