Repeat a sprite

For some reason I am having an issue with repeating a sprite. I have an image I created that I would like to have as like a floor. I cannot for the life of me figure out how to make the image repeat itself to a certain point so I can cover a specific area with the sprite. Think of it as the floor of a dungeon and the sprite is a single tile on that floor. I am trying to repeat that tile in a specific area or even the whole screen.

Any thoughts?

Currently I am using the typical:

sprite(“Dropbox:Floor”, WIDTH/2, HEIGHT/2)

@jpwerner18 Not sure which way you want, so here’s 2 ways.

function setup()
    img=readImage("Planet Cute:Grass Block")
    imgW=img.width
end

function draw()
    background(0)
    for z=1,5 do
        sprite(img,20+imgW*z,100)
    end
end
displayMode(FULLSCREEN)

function setup()
    spriteMode(CORNER)
    img=readImage("Platformer Art:Block Brick")
    imgW=img.width
    imgH=img.height
    w=WIDTH/imgW
    h=HEIGHT/imgH
end

function draw()
    background(0)
    for x=0,w do
        for y=0,h do
            sprite(img,imgW*x,imgH*y)
        end
    end
end

Thank you that has helped!

Search the forum, or the coolcodea blog for “tiler shader”.

@jpwerner18 - if you are going to be drawing the same floor repeatedly, it’s more efficient to create an image in memory, draw all the tiles on to it, then you only have to sprite that one image after that.

--do this in setup
img=image(w,h) --whatever size you want
setContext(img) --tell Codea to draw on image
--<-- put code here to draw your tiles onto this image
setContext() --stop drawing on the image
--now you can just draw img instead of all the tiles

However, if you are tiling a very large area, the size may exceed Codea’s image limits. Also, if the tiled area is not rectangular or an exact multiple of the tile image, drawing can be messy. In this case, you can use a “shader” to efficiently tile the sprite (and you don’t need to understand shaders to do this, although they aren’t as scary as they look). See here -

https://coolcodea.wordpress.com/2013/06/06/78-shaders-tiling-images/

This post may assume a little knowledge about shaders, but if you’re planning to do anything more than basic graphics, they are well worth the effort, and as I said above, not scary. I have an ebook on them.

@jpwerner18 Here’s one of my above examples using @Ignatz suggestion.

displayMode(FULLSCREEN)

function setup()
    spriteMode(CORNER)
    img=readImage("Platformer Art:Block Brick")
    imgW=img.width
    imgH=img.height
    w=WIDTH/imgW
    h=HEIGHT/imgH
    scr=image(WIDTH,HEIGHT)
    setContext(scr)
    for x=0,w do
        for y=0,h do
            sprite(img,imgW*x,imgH*y)
        end
    end
    setContext()
end

function draw()
    background(0)
    sprite(scr,0,0)
end

@dave1707 - setting spriteMode to CORNER makes it easy to draw the tiled sprite but may throw off the drawing for the other sprites in the project. I’d prefer to forget about spriteMode and simply draw the tiled sprite with sprite(WIDTH/2,HEIGHT/2).

PS is it just me, or is the iOS9 spellchecker a lot more aggressive? Every time I write Sprite, it thinks I’m talking about a soft drink.

@Ignatz Using CORNER made it easier to code, but I wasn’t thinking about using other sprites. I haven’t use iOS 9 long enough to notice problems. For some reason, iOS 9 just dimmed my screen. Now I have to find where to brighten it. I tried the split screen thinking I could have 2 Codea programs showing at the same time, but only apps setup for multitasking are allowed.

to adjust lighting, pull up the control centre from bottom of the screen

I got the brightness setting back to how I want it. Just not sure why it dimmed.

The new Siri is very smart, she could see how bright you were already :smiley:

There seems to be a trend to getting software to anticipate everything you want, often with annoying results…