1
Lua / 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.
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.