Spawn sprites

Hi all,
for one of my projects i want to ba able to press a button which makes a sprite spawn in a specified area.
Is there any way to do this??

@Klupa56 Since I dont know where you want the sprite to appear, this example shows a sprite at a random location each time the screen is touched. You just need to add a button and the x,y position where you want the sprite to appear.


displayMode(FULLSCREEN)

function setup()
    x=0
    y=0
end

function draw()
    background(0, 0, 0)
    fill(255)
    text("Tap screen for sprite",WIDTH/2,HEIGHT/2)
    if x+y>0 then
        sprite("Planet Cute:Character Boy",x,y)
    end
end

function touched(t)
    if t.state==BEGAN then
        x=math.random(WIDTH)
        y=math.random(HEIGHT)
    end
end

Thx a lot

What about multiple sprites??
Is there a way to keep multiple sprites on the screen without writing each one up individually

@Klupa56 For multiple sprites, you’ll need tables, which can get a bit complicated. Here’s an example, though:

-- Sprite Spawn

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    sprites = {}
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    for k, v in ipairs(sprites) do
        sprite("Planet Cute:Character Boy", v.x, v.y)
    end
end

function touched(touch)
    if touch.state == BEGAN then
        table.insert(sprites, {x = math.random(1, WIDTH), y = math.random(1, HEIGHT)})
    end
end


You can read up more on tables here: https://coolcodea.wordpress.com/2014/10/01/169-why-tables-and-classes-are-so-useful/