Howdy, Stranger!

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

2 pieces of code, both same, one works - the other doesn’t, why?

edited February 16 in Questions Posts: 58

Hello,

I am taking up @Ignatz “learn codea” pdf guide.

I would like to know what exactly I am doing wrong.
The purpose of this code @Ignatz produced, was to demonstrate once the red line (aka: the arrow) hits a yellow floating ball, we get a score.

I did not paste his code, instead I typed it. I’m a beginner, so for me typing in the code allows it to sink in.

I compared our codes - and I cannot find what is causing the error on my side, but his code is working fine.

Please just let me know what I did wrong
(over explaining with examples that have nothing to do with this code - - will confuse me)

It’s been a couple years since I’ve been on this forum - and when I was on here, I was a beginner grappling with the concepts of variables.

Here is @Ignatz working code:

function setup()      


    balloonWidth=50 
    balloonHeight=60 
    balloonX=WIDTH/2     
    balloonY=0 
    balloonSpeed=1     

    arrowWidth=40 
    arrowStartX=50 
    arrowX=arrowStartX     
    arrowY=HEIGHT/2 
    arrowSpeed=3     
    score=0 
        end  

function draw()     
    background(0)     
    pushStyle()     
    fill(255,255,0,150)     
    balloonY=balloonY+balloonSpeed     
    if balloonY-balloonHeight/2>HEIGHT 
    then balloonY=0 
    end     

ellipse(balloonX,balloonY,balloonWidth,balloonHeight)     
        arrowX=arrowX+arrowSpeed     
        if arrowX>WIDTH 
        then arrowX=arrowStartX 
        end     

        if arrowX>balloonX-balloonWidth/2 
        and arrowX<=balloonX+balloonWidth/2 
        and arrowY>balloonY-balloonHeight/2 
        and arrowY<=balloonY+balloonHeight/2 
        then score=score+1   
        print("Hit!",score)          

        balloonY=0  arrowX=arrowStartX  --reset balloon and arrow     
        end     
    stroke(245, 30, 55)     
    strokeWidth(5)     line(arrowX,arrowY,arrowX+arrowWidth,arrowY)     popStyle() 
end

My code (what did I do wrong?) please POINT out the specific code line or code lines —Thank You


function setup() balloonWidth=50 balloonHeight=60 balloonX=WIDTH/2 --sets (x axis) position to mid screen. balloonY=0 --sets y axis position balloonSpeed=1 --sets speed --ARROWS arrowWidth=40 arrowStartX=50 --where arrow starts from left arrowX=arrowStartX --current x value of arrow arrowY=HEIGHT/2 --later this will be touch arrowSpeed=3 score=0 end function draw() background(4) pushStyle() fill(255,255,0,150) --adjust vertical (y axis) position balloonY=balloonY+balloonSpeed --checks if ball at top of screen, restarts draw bottom if ball at top of screen if balloonY-balloonHeight/2>HEIGHT then balloonY=0 end ellipse(balloonX,balloonY,balloonWidth,balloonHeight) --ARROWS arrowX=arrowX+arrowSpeed --similiar to balloon if arrowX>WIDTH then arrowX=arrowStartX end --Collision Detect if arrowX>balloonX-balloonWidth/2 and arrowX<=balloonX+balloonWidth/2 and arrowY>balloonY-balloonHeight/2 and arrowY<=balloonY+balloonHeight/2 then score=score+1 print("Hit!",score) balloonY=0 arrowX=arrowStart --reset balloon and arrow end --STROKE stroke(239, 52, 57) --arrow color strokeWidth(5) --pixels wide line(arrowX,arrowY,arrowX+arrowWidth,arrowY) --draw arrow popStyle() end

Comments

  • dave1707dave1707 Mod
    Posts: 9,415

    In the third line of Collision Detect, you spelled ballonY wrong, it should be balloonY.

  • I’m going to jump out a window. :D :D :D

    I combed though that code, in multitask view (photo app of the code on one side) Codea on the other for about 25 minutes, three times.

    All this time, it was one letter - how comical, but I’m cool with it.
    Thank you @dave1707

  • Posts: 2,348
    @tactfulgamer - the more you code the better you get at tracing errors. It's trivial things like letter case that can trip up even the best programmers - very frustrating at times. Thankfully you've got support like @dave1707 who can sniff these out. Don't get frustrated just ask the forum. Happy coding.
  • dave1707dave1707 Mod
    Posts: 9,415

    @tactfulgamer When you run the code and it stops because of an error, it shows you what the problem is. It says you have a nil value, gives you the line number of the error, and shows you the variable that’s has the problem. In this case it showed you ballonY. It won’t always be this easy though. Sometimes you’ll get an error, but it may be caused by something else. The more mistakes you make, the better you’ll get at finding them. So keep on coding and keep making mistakes.

  • edited February 16 Posts: 58

    Yes, thank you. @Bri_G that really did catch me off guard. I do look forward to my combing the code skills improving.

    @dave1707 you hit the nail on the head.
    I was coming here to report just that. After fixing balloon, I have now ran into two errors, which appear once Codea tries to redraw the balloon.

    I checked both code lines mentioned in the error reports.

    The error reports state, arrowX is a nil value, but how can that be, when in the function setup arrowX was declared a variable for arrowStartX. Therefore not nil - has a function - thus, providing function draw - parameters to pull from. (This is my thoughts on the issue)

    The code is spelled exactly the same as @Ignatz code. I’m definitely stumped as to what is causing the errors. Maybe you can enlighten me on what I did wrong?

    Here are the two error reports generated (after the arrow hits balloon):

    Main: 72: attempt to perform arithmetic on a nil value
    (global ‘arrowX’)
    stack traceback:
    Main: 72: in function
    ‘draw’

    Referring to this line of code:

    line(arrowX,arrowY,arrowX+arrowWidth,arrowY)
    

    Here’s the other error, which appears right below the first error report:

    Main: 48: attempt to perform arithmetic on a nil value
    (global ‘arrowX’)
    stack traceback:
    Main: 48: in function
    ‘draw’

    Referring to this line of code:

    arrowX=arrowX+arrowSpeed
    
  • dave1707dave1707 Mod
    Posts: 9,415

    @tactfulgamer Once I fix the balloonY error, the code runs OK for me. The balloon goes off the top of the screen and reappears at the bottom. Start with your original code and fix the balloonY error and see what happens.

  • edited February 16 Posts: 58

    @dave1707 Yes, I did fix balloonY and those are the errors I receive.

    See here:

    function setup()
        balloonWidth=50 
        balloonHeight=60 
        balloonX=WIDTH/2     
        balloonY=0    
        balloonSpeed=1   
    
    
        --ARROWS
        arrowWidth=40
        arrowStartX=50 
        arrowX=arrowStartX 
        arrowY=HEIGHT/2
        arrowSpeed=3
        score=0
        end  
    
    
    
    
        function draw()    
        background(4)     
        pushStyle()
        fill(255,255,0,150)    
    
        --adjust vertical (y axis) position    
        balloonY=balloonY+balloonSpeed    
    
    
    
        if balloonY-balloonHeight/2>HEIGHT
        then balloonY=0
        end
        ellipse(balloonX,balloonY,balloonWidth,balloonHeight)
    
    
        --ARROWS
        arrowX=arrowX+arrowSpeed --CODE ERROR HERE
        if arrowX>WIDTH
        then arrowX=arrowStartX
        end
    
        --Collision Detect
        if arrowX>balloonX-balloonWidth/2 
        and arrowX<=balloonX+balloonWidth/2
        and arrowY>balloonY-balloonHeight/2 **—balloonY FIX**
        and arrowY<=balloonY+balloonHeight/2
        then score=score+1
        print("Hit!",score)
    
        balloonY=0 
        arrowX=arrowStart 
        end
    
    
    
    
    
        --STROKE
        stroke(239, 52, 57)     --arrow color
        strokeWidth(5) --pixels wide
        line(arrowX,arrowY,arrowX+arrowWidth,arrowY) --CODE ERROR HERE
        popStyle()
    
    end
    
  • dave1707dave1707 Mod
    Posts: 9,415

    At the end of the Collision Detect code, arrowStart should be arrowStartX.

            arrowX=arrowStartX
    
  • edited February 16 Posts: 58

    @dave1707 Now that was a good laugh :D

    Seems I have an affinity for single letters twisting me up.

    The good thing in all of this. I can kind of read and understand the code to a degree.

    Huge step from where I was at before. Making gains.

    Thank you

  • dave1707dave1707 Mod
    Posts: 9,415

    The more you code and the more mistakes you make and fix, the better you get. So keep on coding and have fun.

  • Posts: 171

    This is why most software engineers use an IDE with code analysis. For example there is a Lua Linter here https://github.com/mpeterv/luacheck
    Perhaps it can be added to Codea and then we can have highlights and other visual cues to let you know “variable ‘ballonY’ has not been defined”

  • edited March 3 Posts: 2,348
    @tactfulgamer - one tip I can give you, it helps me, is to indent your code. Well formatted code helps to see the flow of the program. As you develop your skills you will move to more complex functions so an understanding of good layout helps and is better adopted sooner than later.

    Example you format the if ... Then loop on separate lines - usually the if is followed by the condition followed by the the then syntax. The result of that line is the action provided in the following line or the elseif and else keywords highlight the other option. Indenting the 'action' lines illustrates your code better. e.g in your code above.

    ~~~
    If balloonY - balloonHeight/2 > HEIGHT then
    balloonY = 0
    else
    balloonY = 50 -- dummy line added to show else condition
    end
    ~~~

    When you get to use nested loops this can be very important to help understand the flow.

    Search the forum for examples of code and you should be able to pick up best practices.

    Edit: - sometimes you just can't get things to work right, copied your code and edited but wouldn't format properly using the ~~~ etc. Rewrote it in a text editor - ditto. Did you use a phone for editing this code?
    This doesn't show the indentation but you can see the structure.
    @dave1707 - can you explain why I can't add code from TextEdit on my Mac to this web page and getting this issue?
Sign In or Register to comment.