SpriteSheet Class with Frame Coord system

This is still a work in progress. I am new to this and would like feedback

This will take a sprite sheet and create individual sprites from it. You can then create animations by selecting what frames to display using the bottom left sprite as 1

Sprite Used in Demo - http://goo.gl/IWXj4
code - https://gist.github.com/briarfox/44731d2f8fcdec0bd0ab

if anyone wants to help me out I had an issue in the main.lua

--example
coord = vec2(100,100)
size = vec2(100,100)
inc = 100
cat = "cat1"
a:addSprite(cat,coord,size)
a:addAnimation(cat,"right",{24,23,22})
a:addAnimation(cat,"left",{36,35,34})

--cat 2
coord.x = coord.x +inc
cat = "cat2"
a:addSprite(cat,coord,size)
a:addAnimation(cat,"right",{21,20,19})
a:addAnimation(cat,"left",{33,32,31})

This would not work I had to inc coord.x by
coord = vec2(coord.x+inc,coord.y) why?

If you want the cats to be next to each other and not on top of each other, then you need to increment the x value for each new cat

Thank you Jmv38, that was driving me crazy, perfect example.

I think what you are asking is:
Why does

coord=vec2(coord.x+inc,coord.y)

work, but incrementing coord.x outside the vec2 doesn’t

coord.x=coord.x+inc

In other words, why can’t you reference coord.x directly?

Unfortunately, I don’t know the answer - I’m guessing a vec2 is a special case?

The problem must be somewhere else in your program. This works exactly as expected:

-- test vec2

function setup()
    v = vec2(100,200)
    print(v)
    v.x = v.x + 10
    print(v)
end

function draw()
end

Ok i’ve got it. Line 157 you write.
Coord = screenCoord
So you think you have copied screen Coord to coord… But no, you have copied the adress of screenCoords to coord: the two are pointing to the same memory location now. A class is a complex object, so the = asignment does not pass the value but the adress. if you want a real copy of the values, you must copy all fields as for variable b in below example:


-- test vec2

function setup()
    v = vec2(100,0)
    a = v
    b = vec2() ; b.x = v.x ; b.y = v.y
    print("v = "..tostring(v))
    print("a = "..tostring(a))
    print("b = "..tostring(b))
    v.x = v.x + 100
    print("v = "..tostring(v))
    print("a = "..tostring(a))
    print("b = "..tostring(b))
end

function draw()
end

Check the lua manual for types that are passed by value (numbers,booleans,strings?) everything else is passed as adress. Strings are not really passed as value, the adress is passed, but since in lua you never modify a string, you just create a new one, the result is the same.

You need to write a deepcopy() routine. I have one somewhere…i found it online. It handles tables. I don,t recall if it handles userdata, which is vec2, color, etc.

Thanks aciolino, I had no idea what a deep copy was. Looks like I’ve been doing shallow copies. if you happen to run across you code I would like to see it.

Updated to allow easier movement of mesh and uses rotation and translation now.

Briarfox : the previous links (above) are dead on purpose ?

I dont believe I deleted it on purpose. Its a library that has become very messy. I used it in a small game. I have plans on cleaning it up and will repost it.