Shooting in x or y

Does anyone know how to make a sprite shoot an image in either the x or y axis? It would help if there could be a button to press to shoot too, how can I do this?

Drfine missile as false at the beginning of your code, Then say if missile is true, then missile = missile + 1
@dave1707 should have something layin around :wink:

@CodeaNoob, missile cannot be both a Boolean and an integer value.

put “shoot” in the custom search box at upper right, there have been a couple of threads on this

@MichaelWeedmard I tried searching for this in the forum, but couldn’t find it so here it is again. Slide your finger up/down on the right side of the screen to rotate the ship. Tap the left side to shoot bullets.


displayMode(FULLSCREEN)

function setup()
    t1={}    -- table for targets
    b1={}    -- table for bullets
    xa=0
    ya=1
    ang=0
    exp=0
    e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    e1.info="edge"
    e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
    e2.info="edge"
    e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
    e3.info="edge"
    e4=physics.body(EDGE,vec2(WIDTH,0),vec2(0,0))
    e4.info="edge"
end

function draw()
    background(140, 140, 140)
    fill(255)
    fontSize(30)
    if #b1==0 then
        text("Tap left side of screen to shoot bullets",400,1000)
        text("Slide finger on right side to rotate ship",400,950)
    end
    
    pushMatrix()    -- draw rotated ship
    translate(WIDTH/2,HEIGHT/2)
    rotate(180-ang)
    sprite("Tyrian Remastered:Plane Boss Destroyed",0,0)
    popMatrix()  
    
    if #t1==0 then
        for z=1,10 do
            targets()
        end
    end
     
    for z=1,#t1 do    -- check targets
        if t1[z].remove then    -- remove target
            tx=t1[z].x
            ty=t1[z].y
            exp=20
            t1[z]:destroy()
            table.remove(t1,z)
            sound(SOUND_EXPLODE, 42925)
            break
        else
            x=t1[z].x    -- draw target
            y=t1[z].y
            sprite("Tyrian Remastered:Evil Orb",x,y,20)
        end  
    end
    
    for z=1,#b1 do    -- check bullets
        if b1[z].remove then    -- remove bullet
            b1[z]:destroy()
            table.remove(b1,z)
            break
        else
            x=b1[z].x    -- draw bullets
            y=b1[z].y
            fill(255,0,0)
            sprite("Tyrian Remastered:Mine Big Spiked",x,y,10)
            if x<0 or x>WIDTH or y<0 or y>HEIGHT then    -- off screen
                b1[z]:destroy()
                table.remove(b1,z)
                break
            end
        end
    end
    
    if exp>0 then    -- draw explosion
        explosion(tx,ty)
        exp=exp-1
    end
end

function explosion(x,y)
    sprite("Tyrian Remastered:Explosion Huge",x,y,exp*5)
end

function collide(c)    -- collision occured
    if c.state==BEGAN then
        for z=1,#t1 do    -- check which target 
            t1[z].remove=checkBody(c,t1[z].x,t1[z].y)
        end
        for z=1,#b1 do    -- check which bullet
            b1[z].remove=checkBody(c,b1[z].x,b1[z].y)
        end
    end   
end

function checkBody(c,x,y)    -- check which bodies are colliding
    if c.bodyA.info=="bullet" and c.bodyB.info=="target" then
        if c.bodyA.x==x and c.bodyA.y==y or c.bodyB.x==x and c.bodyB.y==y then
            return true
        end
    end    
    if c.bodyA.info=="target" and c.bodyB.info=="bullet" then
        if c.bodyA.x==x and c.bodyA.y==y or c.bodyB.x==x and c.bodyB.y==y then
            return true
        end
    end
    if c.bodyA.info=="bullet" and c.bodyB.info=="edge" then
        if c.bodyA.x==x and c.bodyA.y==y then
            return true
        end
    end    
    if c.bodyA.info=="edge" and c.bodyB.info=="bullet" then
        if c.bodyB.x==x and c.bodyB.y==y then
            return true
        end
    end
    return false
end

function bullets(x)    -- create bullets
    local b=physics.body(CIRCLE,5)
    b.x=WIDTH/2
    b.y=HEIGHT/2
    b.gravityScale=0
    b.linearVelocity=vec2(xa*400,ya*400) 
    b.remove=false
    b.info="bullet"
    table.insert(b1,b)
end

function targets()    -- create targets
    local t=physics.body(CIRCLE,10)
    t.x=math.random(WIDTH-60)+30
    t.y=math.random(HEIGHT-60)+30
    t.restitution=1
    t.gravityScale=0
    t.remove=false
    t.info="target"
    t.linearVelocity=vec2(math.random(-100,100),math.random(-100,100))
    table.insert(t1,t)
end       

function touched(t)
    if t.state == BEGAN then    
        if CurrentTouch.x < WIDTH/2 then    -- create bullets
            bullets()
        end
    end
    if CurrentTouch.x>WIDTH/2 then    -- rotate ship
        ang=ang+t.deltaY
        xa=math.sin(math.rad(ang))
        ya=math.cos(math.rad(ang)) 
    end           
end

Thanks!