My Code doesnt work! Why?

I have another issue. Codea can only read 1 function. So I am forced to put all actions within one function. I really need help with this.

You mean only the draw() function geht’s called?
Yeah, if you write other functions you have to call these in the draw() function, but remember that the draw() function gets called several times per second, so be careful with print() :wink:
It was my mistake to!

How do you call other functions within a function?

https://bitbucket.org/TwoLivesLeft/codea/wiki/kidfunc

For example you have a function called “drawpigs”:

Function drawpigs()
Sprite(“whatever”,100,100)
End

Function draw()
Drawpigs() --thats it!
End

Like this!

So I am making a sniper game and you drag the reticle around and then click a button to fire. I drag the reticle int place and then click the button to fire but the reticle moves to the button. How do I set this up so that won’t happen?

It sounds like you have an issue with your program state, @Ldsguy1

You should do the following

  • Check if your touch hits a button
  • If so, activate the button
  • If not, move the sniper reticle

At the moment you seem to be moving the sniper reticle even if you press the button. You should check where the touch lands first, and only move the reticle if the touch didn’t hit anything else.

@Ldsguy1 I suggest that you need to look at the coordinates of your touch before you decide what to do with it.



For example, if your fire buttom is on the bottom of the screen you might have something like:



If CurrentTouch.y < 100 then

(fire routine)

else

(move routine)

end

So I have another issue. It works but theres a weird error. When you move the sprite it leaves a trail on moving sprites and it’s more like drawing than a game.

cx=300
cy=300
function draw()
    rect(0,0,0,0)
    fill(255, 0, 0, 255)
    stroke(255, 0, 0, 255)
    strokeWidth(20)
    line(75,50,75,150)
    line(25,100,125,100)
    move()
    player()
    end
    
function move()
    x=CurrentTouch.x
    y=CurrentTouch.y
    if y<150 and y>100 and x<80 and x>70 then
        cy=cy+10 end
    if x<125 and x>75 and y<110 and y>90 then
        cx=cx+10 end
    if y>50 and y<100 and x>70 and x<80 then
        cy=cy-10 end
    if x>25 and x<75 and y>90 and y<110 then
        cx=cx-10 end
    end

function player()
    sprite("Planet Cute:Character Boy",cx,cy,150,150)
end

Try throwing a background(0, 0, 0) at the top of your draw function. That’ll clear the screen each frame.

Thanks that cleared it up.