Howdy, Stranger!

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

Tile image function

edited January 2012 in Code Sharing Posts: 2,820

Since I've seen no one do this yet with the new sprite zsize function, it thought I might as well. It was extremely easy, but here is the code:

function setup()
    mx = 0
    my = 0

function tileImage(image,movex,movey,iscale)
    local oldScale = scale
    local currentScale = 1
    scale = function( d )
        if d == nil then
            return currentScale
            currentScale = currentScale * d 
    bsmode = spriteMode()
    currentScale = scale()
    bscale = currentScale
    ssx, ssy = spriteSize(image)
    mx = mx + movex
    my = my + movey
    for y = -1, HEIGHT/ssy/iscale do
        for x = -1, WIDTH/ssx/iscale do
            if mx >= ssx or mx <= -ssx then
                mx = 0
            elseif my >= ssy or my <= -ssy then
                my = 0
            sprite(image, x * ssx+mx, y * ssy+my)

That was easy. But it's still full of bugs and needs improvement. Here's what's on my list:
1. Fix the jolt when repeating the images when moving
2. Fix the problem when you tell it to move mor pixels than the image is
3. Fix the returning to the original scale and sprite mode
4. Madage to contain all the variables within the function
5. Speed up the process. It really slows down the program and even crashes Codea when you use some pixel art.
6. Add option to tell it if it's a block or not and it will automatically offset it correctly and draw the blocks right.


  • Posts: 2,820

    Oh. Is pushStyle() a way to save the style? How do you use it? It might be helpful.

  • Posts: 622

    Yes. Push/pull is like a stack of plastic cups. You push the current style down on the stack, then you can change whatever and when done, pop the style back up. Since it's a stack, you can push multiple times and they pop LIFO - last (most recent) in first out

  • Posts: 2,820

    Do you have a very simple example? Codea didn't really supply a really good, simple sample.

  • Posts: 622
    -- This sets the line thickness
        -- Do your drawing here
        line(100,100,100,200) --5
        pushStyle() --push 5
        line(110,100,110,200) --still 5
        strokeWidth(10) --changed to 10
        line(120,100,120,200) --10
        popStyle() --pop 5
        line(130,100,130,200) --5
  • Posts: 2,820

    Alright. Thanks. I'll include it as soon as possible.

  • SimeonSimeon Admin Mod
    Posts: 5,438

    Also the "Graphical Styles" example that ships with Codea is designed to show use of pushStyle() and popStyle().

  • Posts: 2,820

    Oh. Thanks. I'll check it out.

  • Posts: 2,820

    This really slows down the app. Any idea how to make it faster? Thanks.

  • Posts: 29

    I don't understand why you loop x (and y) from -1 to (for example) +4?

  • Posts: 2,820

    Are you talking about movex and movey? Those automatically move the background. In the loop, I start at -1 so when it is moving 1,1 pixels, I loop the position back to 0 (which there is a better method I'm about to add) to simulate continuous motion.

  • Posts: 29

    No, I meant:

        for x = -1, WIDTH/ssx/iscale do

    and then

                sprite(image, x * ssx+mx, y * ssy+my)
  • Posts: 2,820

    @Martin - mx means move x. Mabey I shouldve called it adjust x. I change it whatever amount the image should be offset to make the images move. Then when it's greater than the image size, I set it back to 0 to simulate an ongoing background.

Sign In or Register to comment.