Page 1 of 1

Sprite Serialization Help

Posted: Sat Mar 12, 2016 5:50 am
by bumblberry
I'm working on a top-down game with grid-based movement and a ton of possible player sprites. All of my sprites are loaded using thishttp://www.buildandgun.com/2014/07/anim ... ove2d.html. After switching between animations for a short time, I get an error like this:

Code: Select all

[Error] AnimatedSprite.lua:100: bad argument #2 to 'draw' (Quad expected, got nil)
My main.lua looks like this:

Code: Select all

require "AnimatedSprite"
require "lovedebug"

love.graphics.setDefaultFilter("nearest", "nearest")

function testMap(x, y)
	if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
		return false
	end
	return true
end


function love.load()
  archer_f = GetInstance("unit_scripts/archer_f_sprite.lua")
  
  player = {
    grid_x = 256,
    grid_y = 256,
    actual_x = 200,
    actual_y = 200,
    sprite = archer_f,
    speed = 15,
    idle_anim = 1,
    horz_anim_l = 2,
    horz_anim_r = 3,
    down_anim = 4,
    up_anim = 5,
    select_anim = 6
  }
  
  map = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
		{ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
		{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
	}
end

function love.update(dt)
  player.actual_y = player.actual_y - ((player.actual_y - player.grid_y) * player.speed * dt)
  player.actual_x = player.actual_x - ((player.actual_x - player.grid_x) * player.speed * dt)

  UpdateInstance(player.sprite, dt)
end

function love.draw()
  DrawInstance(player.sprite, player.actual_x, player.actual_y)

  -- draws the wire map
  for y=1, #map do
		for x=1, #map[y] do
			if map[y][x] == 1 then
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
			end
		end
	end
end

function love.keypressed(key)
  -- after each movement need to run timer and then switch to idle animation
  if key == "up" then
    if testMap(0, -1) then
		  player.grid_y = player.grid_y - 32
      player.sprite.curr_anim = player.sprite.sprite.animations_names[player.up_anim]
    end
	elseif key == "down" then
    if testMap(0, 1) then
		  player.grid_y = player.grid_y + 32
      player.sprite.curr_anim = player.sprite.sprite.animations_names[player.down_anim]
    end
	elseif key == "left" then
    if testMap(-1, 0) then
		  player.grid_x = player.grid_x - 32
      player.sprite.curr_anim = player.sprite.sprite.animations_names[player.horz_anim_l]
    end
	elseif key == "right" then
    if testMap(1, 0) then
		  player.grid_x = player.grid_x + 32
      player.sprite.curr_anim = player.sprite.sprite.animations_names[player.horz_anim_r]
    end
  elseif key == "space" then
    player.sprite.curr_anim = player.sprite.sprite.animations_names[player.select_anim]
	end
end
And the error is referring to here in AnimatedSprite.lua:

Code: Select all

function DrawInstance(spr, x, y)
  love.graphics.draw(
    image_bank[spr.sprite.sprite_sheet],
    spr.sprite.animations[spr.curr_anim][spr.curr_frame], -- this argument is failing sometimes
    x,
    y,
    spr.rotation,
    spr.size_scale,
    spr.size_scale,
    flip_h,
    flip_v
  )
end -- DrawInstance
Why could my sprites be failing to load sometimes?

Re: Sprite Serialization Help

Posted: Sat Mar 12, 2016 9:41 am
by pgimeno
The problem is likely in archer_f_sprite.lua; can you show it?

Re: Sprite Serialization Help

Posted: Sat Mar 12, 2016 6:06 pm
by bumblberry
pgimeno wrote:The problem is likely in archer_f_sprite.lua; can you show it?
Yup

Code: Select all

print("archer_f_sprite.lua loaded")

local source_img = love.graphics.newImage("assets/character_sprites/map/archer_f.png")

local image_w = source_img:getWidth()
local image_h = source_img:getHeight()

return {
  serialization_version = 1.0,

  sprite_sheet = "assets/character_sprites/map/archer_f.png",
  sprite_name  = "archer_f",

  frame_duration = 0.2,

  animations_names = {
    "idle",
    "move_horz_l",
    "move_horz_r",
    "move_down",
    "move_up",
    "selecting"
  },

  animations = {
    idle = {
      love.graphics.newQuad(2, 33, 34, 34, image_w, image_h),
      love.graphics.newQuad(2, 66, 34, 34, image_w, image_h),
      love.graphics.newQuad(2, 99, 34, 34, image_w, image_h),
    },
    move_horz_l = {
      love.graphics.newQuad(34, 0, 34, 34, image_w, image_h),
      love.graphics.newQuad(34, 33, 34, 34, image_w, image_h),
      love.graphics.newQuad(34, 66, 34, 34, image_w, image_h),
      love.graphics.newQuad(34, 99, 34, 34, image_w, image_h),
    },
    move_horz_r = {
      love.graphics.newQuad(166, 0, 34, 34, image_w, image_h),
      love.graphics.newQuad(166, 33, 34, 34, image_w, image_h),
      love.graphics.newQuad(166, 66, 34, 34, image_w, image_h),
      love.graphics.newQuad(166, 99, 34, 34, image_w, image_h),
    },
    move_down = {
      love.graphics.newQuad(66, 0, 34, 34, image_w, image_h),
      love.graphics.newQuad(66, 33, 34, 34, image_w, image_h),
      love.graphics.newQuad(66, 66, 34, 34, image_w, image_h),
      love.graphics.newQuad(66, 99, 34, 34, image_w, image_h),
    },
    move_up = {
      love.graphics.newQuad(100, 0, 34, 34, image_w, image_h),
      love.graphics.newQuad(100, 33, 34, 34, image_w, image_h),
      love.graphics.newQuad(100, 66, 34, 34, image_w, image_h),
      love.graphics.newQuad(100, 99, 34, 34, image_w, image_h),
    },
    selecting = {
      love.graphics.newQuad(132, 33, 34, 34, image_w, image_h),
      love.graphics.newQuad(133, 66, 34, 34, image_w, image_h),
      love.graphics.newQuad(133, 99, 34, 34, image_w, image_h),
    },
  },
}


Re: Sprite Serialization Help

Posted: Sat Mar 12, 2016 7:22 pm
by zorg
The idle and selecting animations have 3 frames, and the rest have four, do you check whether the spr.curr_frame value is actually between 1 and the number of frames defined in the current animation (spr.curr_anim) anywhere?
That may be the issue.

Re: Sprite Serialization Help

Posted: Sun Mar 13, 2016 5:12 am
by bumblberry
zorg wrote:The idle and selecting animations have 3 frames, and the rest have four, do you check whether the spr.curr_frame value is actually between 1 and the number of frames defined in the current animation (spr.curr_anim) anywhere?
That may be the issue.

That does seem to be the issue. I copied a middle frame from selecting and placed it at the end of idle to test when idle has four frames. When switching from one of the directions to idle it keeps the frame number before updating. That's what was giving me the out-of-bounds error. So I guess I just need to reset the frames on input when moving from a directional animation to a non-directional one. Thanks for the help! ;)