If you’ve seen my prototype “destructible terrain” program in another thread, you may have noticed that I’m using a sprite to sample pixels from an image, then slide the whole thing downward.
I’ve noticed something: the sprite method seems to actually draw one pixel up and to the right of the referenced location.
You can reproduce this by using image.copy to sample a part of an image, then using sprite to draw the sample back at the same location… like this:
spriteMode(CORNER)
noSmooth() -- make sure this isn't an anti-aliasing artifact
img2=img1:copy(1,1,img1.width,img1.height)
setContext(img1) -- draw to the image, rather than the screen
sprite(img2,1,1)
setContext()
sprite(img1,1,1) -- actually draw the image to the screen
If you put this in your draw() function, the contents of img1 will start walking up and to the right.
I think the cause here is the way Codea is referencing pixels inside of images. Codea seems to use (1,1) as the bottom-left pixel of the screen and of an image, whereas the underlying language used to implement the graphics library uses (0,0). In the runtime’s sprite method, the x and y coordinates just need to be decremented by one.
I’ve confirmed the img.copy method is working correctly. If you draw three different-colored vertical lines next to each other, then .copy the center line, you get the correct color.
Out of curiousity: why does the Codea runtime use 1 as the origin value for images? This is not consistent with any other graphics implementation I’m aware of, and if you’re using this as a learning tool, this teaches bad habits. (I can’t count the number of times when I TA’d in college that I’ve had to remind students to start indexing arrays with 0, not 1.)