#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# SpriteSheet Class with Frame Coord system

Mod
edited March 2013 Posts: 1,542

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

Tagged:

• Mod
Posts: 1,542

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"

--cat 2
coord.x = coord.x +inc
cat = "cat2"

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

• Mod
Posts: 5,396

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

• Posts: 887

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?

• Mod
Posts: 3,297

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

• Mod
edited March 2013 Posts: 3,297

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.

• Mod
Posts: 1,542

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

• Posts: 666

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.

• Mod
Posts: 1,542

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.

• Mod
Posts: 1,542

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

• Posts: 18