CodeWalrus

Development => Calculators => Calculator News, Coding, Help & Talk => Topic started by: Strontium on April 27, 2015, 05:32:12 AM

Title: Code runs fine on TI Nspire emulator, but wont work on real hardware
Post by: Strontium on April 27, 2015, 05:32:12 AM
I am working on a roguelike for my TI Nspire CX. So far, everything works fine in the emulator, but on the calculator, I cannot use the arrow keys to move. Here is the code:

-- character info
playerx = 5
playery = 5
playerhp = 100

-- MONSTER CLASS THINGY
-- monster constructor
MonsterGen = {}
function MonsterGen:new(x, y, icon)
  o = {x = x, y = y, icon = icon}
  setmetatable(o, self)
  self.__index = self
  return o
end

-- move monster
function MonsterGen:move()
  -- temporary movement
  -- I will soon make this an algorythm to move towards the player
  self.x = self.x + 1
  self.y = self.y + 2
end

-- end monster class thingy

-- event handlers
function on.paint(gc)
  -- draw player stats
  gc:drawLine(260, 0, 260, 240)
  gc:drawString("HP", 265, 15)
  gc:drawString(tostring(playerhp) .. "/100", 265, 30)

  -- draw all the monsters
  for mono = 1, 2 do
    gc:drawString(monster[mono].icon, monster[mono].x * 20, monster[mono].y * 20)
  end
  -- draw the player
  gc:drawString('@', playerx * 20, playery * 20)
end

function on.arrowLeft()
  -- move player left
  playerx = playerx - 1
  turnTaken()
end

function on.arrowRight()
  -- move player right
  playerx = playerx + 1
  turnTaken()
end

function on.arrowUp()
  -- move player up
  playery = playery - 1
  turnTaken()
end

function on.arrowDown()
  -- move player down
  playery = playery + 1
  turnTaken()
end

-- perform various processes after player turn
function turnTaken()
  -- perform monster-ly processes
  for mono = 1, 2 do
    monster[mono]:move()
  end
end

monster = {}
monster[1] = MonsterGen:new(1, 1, 'k')
monster[2] = MonsterGen:new(3, 3, 'g')


I have no idea what is causing the movement to work on the emulator but not the calculator. At all. And I need help fixing it.

Edit: Ha! I figured it out. I forgot to call platform.window:invalidate() after each turn.

Woops.
Title: Re: Code runs fine on TI Nspire emulator, but wont work on real hardware
Post by: Dream of Omnimaga on April 27, 2015, 05:49:41 AM
Glad you figured it out. :) I am curious about why the code would behave differently in the emulator than on the calculator otherwise, though... this screams like the emulator could use some fixes.
Title: Re: Code runs fine on TI Nspire emulator, but wont work on real hardware
Post by: Strontium on April 27, 2015, 05:52:47 AM
Yeah, I thought it was a bit weird when the emulator would update the graphics context without me explicitly telling it to. The last time I tried to do something in the emulator it didn't act that way.