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: http://maxiking16.posterous.com/upgrade-pro

Tagged:

Comments

  • 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.



    touched(CurrentTouch)



    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

                        drawdialogs()


    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
            Store
       Elseif store then
             Not store
       End
    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)
    http://maxiking16.posterous.com/

  • 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.

    prohttp://s3.amazonaws.com/files.posterous.com/maxiking16/D6h8SDEtnoF3QhAmp43xyvmGAMb3qgQETm8zqTJ81cXRxfGdVszaW1LKWwjc/Upgrade_Pro_0.3.codea?AWSAccessKeyId=AKIAJFZAE65UYRT34AOQ&Expires=1324137243&Signature=aW6tbncvBxu8ls7WjuF/peunn2U=

  • 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.

    http://devilstower.posterous.com/upgrade-pro-test

  • 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 :)

    http://maxiking16.posterous.com/upgrade-pro-04

  • 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()
    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?

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

    else

    (move routine)

    end

  • 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.

    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
    

            

  • 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.