Arrows

In Ignatz book Codea For Beginners he gave balloon pop code in chapter six I was wondering how I could make new arrows spawn when I tap the screen rather then the arrow disappear and create a new one?

To have multiple arrows on the screen at the same time, you need to use a table. Each time you tap the screen, you’ll add an arrow to the table.

I have put the arrows into a table yet the result is the same as when it is outside of a table. How would I make so every time I touch a new arrow will be created? I would like multiple arrows on the screen at one time. Would you look at my code and tell what I am doing wrong. Thank you very much would appreciate any help.

-- Final Project

function setup()
    A={}
    LaunchArrow()
end

function LaunchArrow()
    a={}
    a.w=40
    a.sx=50
    a.x=-1
    a.y=HEIGHT/2
    a.s=10
    table.insert(A,a)
end

function draw()
    background(255, 255, 255, 255)
    for i,a in pairs(A) do
        if a.x>0 then  
        a.x=a.x+a.s  --adjust position
        if a.x>WIDTH then a.x=-1 end --delete if off the screen
        x=a.x+a.w --calculate right hand position, for collision testing
    else x=0 --set default if no arrow, so we don't get an error when collision testing
    end
    if a.x>0 then --if we still have an arrow, draw it
        stroke(0,0,0,255)
        strokeWidth(2)
        line(a.x,a.y,a.x+a.w,a.y)
    end
end
end

function touched(touch)
   if touch.state==BEGAN then 
      a.y=touch.y 
      a.x=a.sx 
   end
end

Here’s code to draw multiple arrows. You’ll have to work this into your other code.

-- Final Project

function setup()
    A={}
    arrowSpeed=10
    arrowWidth=40
end

function draw()
    background(255, 255, 255, 255)
    stroke(0,0,0,255)
    strokeWidth(2)
    for i,a in pairs(A) do
        a.x=a.x+arrowSpeed  --adjust position
        if a.x>WIDTH then 
            table.remove(A,i)   -- delete if off screen
        else
            line(a.x,a.y,a.x+arrowWidth,a.y)    -- draw arrow
        end
    end
end

function touched(touch)
   if touch.state==BEGAN then 
        table.insert(A,vec2(-1,touch.y))    -- add arrow to table
   end
end

Thanks a millions that’s really helpful!

@dave1707 how do you have the variables a.x and a.y without establishing them in some sort of function? I used your code to try the concept and it works perfectly, but I want them to spawn from the bottom of the screen, but switching the x’s and y’s just didn’t seem to do it

@dave1707 I guess what I’m asking is how does yours work when you don’t have something like “function LaunchArrow()” like in @iangid 's code

@canderson Here’s code to spawn arrows from the bottom of the screen. Compare this to my other code to see what changed. The comparison might give you an idea of what needed to be done. I don’t need a LaunchArrow function because I launch the arrow in the touched() function. I also added code so that an arrow won’t be launched unless touch.y is less than 200.

displayMode(FULLSCREEN)

function setup()
    A={}
    arrowSpeed=10
    arrowHeight=40
end

function draw()
    background(255, 255, 255, 255)
    stroke(0,0,0,255)
    strokeWidth(2)
    for i,a in pairs(A) do
        a.y=a.y+arrowSpeed  --adjust position
        if a.y>HEIGHT then 
            table.remove(A,i)   -- delete if off screen
        else
            line(a.x,a.y,a.x,a.y+arrowHeight)    -- draw arrow
        end
    end
end

function touched(touch)
   if touch.state==BEGAN and touch.y<200 then 
        table.insert(A,vec2(touch.x,touch.y))    -- add arrow to table
   end
end