#### Howdy, Stranger!

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

# How would I efficiently shift a 2048x2048 image over by a certian amount?

edited May 13 Posts: 26

Not like translating but editing the actual image so the contents shift to the left or to the right by a certain amount. I also know how to clear an image too.

• Mod
edited May 13 Posts: 5,896

if you want to shift an image, you could use image get and image set to shift it however you want. Read the original image with get, write a new image with set. Or image copy might be easier, but then you would have to fill the blank area in either case to keep the 2048x2048 size

• edited May 13 Posts: 26

I thought about doing image:set()/image:get() but that would be pixel by pixel and I need the function to keep doing this in real time (for side scrolling). Before asking this question, I had a function set up that would:

1. local var1 = image:copy(bigpicture) all of the image except for that column by setting x,y,w,h
2. local var2 = an image of the same size as the first
3. setContext(var2) draw the var1 at 0,0 and setContext()
4. bigpicture = var2
5. end

But the big picture didn't exhibit any change, and I couldn't find out what was wrong.

• Mod
Posts: 5,896

If you're using sprite to display the image, just change the x,y values for sprite. That will scroll the image in any direction any amount.

• Mod
Posts: 5,896

I don't have a 2048x2048 image, so I stretched the Cargo Bot image to 2048x2048. This lets you scroll the image in any direction.

displayMode(FULLSCREEN)

function setup()
x,y=WIDTH/2,HEIGHT/2
end

function draw()
background(40, 40, 50)
sprite(m,x,y,2048,2048)
end

function touched(t)
if t.state==MOVING then
x=x+t.deltaX
y=y+t.deltaY
end
end

• Posts: 26

Thank you for helping but I fixed my function. Here is what I did:

function Terrain:shift(dir)
local cs = (self.tiles.width/self.nChunks)
local blank = image(2048/self.density,(HEIGHT)/self.density)
local i = self.tiles:copy()
local sm = spriteMode()
spriteMode(CORNER)
if dir == "right" then
setContext(blank)
pushMatrix()
translate(cs,0)
sprite(i)
popMatrix()
setContext()
end
if dir == "left" then
setContext(blank)
pushMatrix()
translate(-cs,0)
sprite(i)
popMatrix()
setContext()
end
spriteMode(sm)
self.tiles = blank
end

• Posts: 26

Not fixed. Causes inexplicable crashes.

• Mod
Posts: 1,983

How often do you call terrain shift? Do you really need to create a new image each time? Create the image in setup, hold on to it, then use the same image each time you shift terrain. Images are immensely expensive resources, don't create more than you need to.