Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

My Code doesnt work! Why?

edited December 2011 in Questions Posts: 168

There is a Big mistake in my Code. But i dont find it! Can somebody help me?

For understanding: i want These Dialogs to appear when i Touch the House or the Shop. When i Touch the House i want the left Dialog and the Guy-Icon to appear, and when i Touch the Shop i want the right Dialog and the Shop-Icon to appear. When i Touch the already-touched-Building i want the Dialog to dissappear. Its that simple, but i Cant manage it -.-

Here is the Code:



  • In your touch loop HOUSEisItRight is getting set to true and then false with the same touch

    Using elseif is a way to get only one condition executed at a time

  • Posts: 1,255

    Just taking a quick look...

    First, the touched() function never gets called. The only function that gets called on each cycle is draw(), so if you want the touched function to get called, you have to include a call to it there.

    Second, within the touched function, you've used the variable touch, but you haven't passed anything to it.

    To get around these issues, add this line to the bottom of your draw function.


    Next, there's nothing to call the dialog functions. If you touch one of the houses, it does change the variables, but doesn't call the other functions. The code has to be told to call that function.

    So, for example, go in after the line and add a call to draw the dialogs, like this:

                        HOUSEisItRight = true


    In general, that's what is missing: the calls that tell it to actually perform a function.

  • So does anyone else have the "flashing color" issue where when you strat a program the background will flash like a strobe and nothing will run? You have to hit reset a bunch in order for it to stop.

  • I remember having that and forget precisely what I did. I think a combination of rect(0,0,10,10) and background(0,0,0) at the start of draw() should fix. I think after 1.2.6 the rect won't be needed.

  • SimeonSimeon Admin Mod
    Posts: 5,386

    @Ldsguy1 the flashing shouldn't happen any more with 1.2.5. Are you using the latest version?

  • Yes I am.

  • Though I am using old code.

  • @mark: wow, its better than before. But it does what i expected: it only shows my upgradewindows if touch.state == began, and when i put my Finger off or move it, the Color is changed to HOUSE-/STOREDIALOGf(alse)t(ouch)c(olor), wich means the Window is Not kept open for further instructions...
    The moving issue can be changed by adding: or Touch.state == moving, but the Otter One? I tried to use "touch" instead of "currenttouch", but then it says its not existing... Any idea?

  • @ipda: ok, sounds Logic. I'll change it. But how? Can you just give an example?

  • i ment @Mark and @Ipda41001

  • edited December 2011 Posts: 622
    If began then
       If not store then
       Elseif store then
             Not store
    End --began

    This isn't exactly your code but the if structure is what I'm attempting to show.

  • Doesnt work to... I thought "Touch.state == ended would solve it but Look...
    (the 0.1 is the One with your if-idea)

  • Posts: 1,255

    The dialog goes away because it's not being drawn in the draw() function.

    Remember, draw gets called every cycle (several times a second).soyou are drawing your dialog, and then draw immediately comes along and erases it.

    The secret is to draw the dialog from the draw() function, and use a state variable to indicate whether or not it should be drawn().

  • Wow your right! Ill try it

  • Doesnt work either... I took the isItRight as a statevariable, but now no dialog is drawn...

    Hhh... Thinking about giving up... I think i should try something that is much easier, i cant always ask you poor guys.


  • Posts: 1,255

    See if this looks more like what you had in mind. I added a little variable to track dialog state. I also commented out your tint statements in the dialog. It looks like everything was getting tinted with alpha=0, making it invisible.

  • Omg thank you so much! I optimized your changes, and now, thats what i looked for! Thats what was thinkin of! Now, im going to work on further versions :)

  • Oh try this One, i added ability to upgrade the store :)

  • 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() ;)
    It was my mistake to!

  • How do you call other functions within a function?

  • For example you have a function called "drawpigs":

    Function drawpigs()

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

    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?

  • SimeonSimeon Admin Mod
    Posts: 5,386

    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.

  • Posts: 1,255

    @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)


    (move routine)


  • edited December 2011 Posts: 78

    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.

    function draw()
        fill(255, 0, 0, 255)
        stroke(255, 0, 0, 255)
    function move()
        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
    function player()
        sprite("Planet Cute:Character Boy",cx,cy,150,150)


  • Posts: 20

    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.

Sign In or Register to comment.