Howdy, Stranger!

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

Trouble making a randomly generating background

edited September 2014 in Bugs Posts: 63

Something's wrong here, but I don't know what. Here is the bugged code that chooses each of the 3 rects y coordinates:

function setup()
deep3 = 0
    deep3y=0
    deep2 = 0
    deep2y=0
    deep1= 0
    deep1y=0

    deep1 = math.random(0,3)
    if deep1 < 1 then
        deep1y=HEIGHT/2
        else if deep1 > 2 then
            deep1y=0-HEIGHT/2
            else
            deep1y=0-HEIGHT*1.5
        end
    end

    function dm3()
        deep3=math.random(0,3)
        if deep3 < 1 and deep2>=1 and deep1>=1 then
            deep3y=HEIGHT/2
            else if deep3 > 2 and deep2 <= 2 and deep1 <= 2 then
                deep3y=0-HEIGHT/2
                else if deep3 >= 1 and deep3 <= 2 and deep2 < 1 or deep2 > 2 and deep1 < 1 or deep1 > 2 then
                    deep3y=0-HEIGHT*1.5
                    else
                    rm3()
                end
                function dm2()
                    deep2=math.random(0,3)
                    if deep2 < 1 and deep3>=1 and deep1>=1 then
                        deep2y=HEIGHT/2
                        else if deep2 > 2 and deep3 < 2 and deep1 < 2 then
                            deep2y=0-HEIGHT/2
                            else if deep2 >= 1 and deep2 <= 2 and deep3 < 1 or deep3 > 2 and deep1 < 1 or deep1 > 2 then
                                deep2y=0-HEIGHT*1.5
                                else
                                rm2()
                            end
                        end
                        dm2()
                        dm3()"

Comments

  • Posts: 725

    Your else if statements (two words) should probably be elseif (one word) for starters. This will have a knock on effect on where you put the end statements. Proper indentation would help too

  • edited October 2014 Posts: 63

    (fixed)

  • Jmv38Jmv38 Mod
    Posts: 3,295

    post the code with propoer indentation if you want bdy to help you with this kind of bug.

  • edited October 2014 Posts: 63

    I apologize for not having pasted the code with proper indentation earlier, but I have encountered a new bug this time. In this bug, the code functions perfectly well except for some reason, deep3y will always equal = -HEIGHT. The functions work perfectly, so I do not understand what causes this bug.

    Heres the code with proper indentation:

    function setup()
        spriteMode(CORNER)
        parameter.watch("deep1y")
        parameter.watch("deep2y")
        parameter.watch("deep3y")
        deep3=0
        deep3y=0
        deep2=0
        deep2y=0
        deep1=0
        deep1y=0
        dm1()
        dm2()
        dm3()
        end
    function dm1()
                        deep1=math.random(0,3)
                        if deep1 < 1 and deep3>=1 and deep2>=1 then
                            deep1y=HEIGHT
                            elseif deep1 > 2 and deep3 <= 2 and deep2 <= 2 then
                                deep1y=0
                                elseif deep1 >= 1 and deep1 <= 2 and deep3y ~= 0-HEIGHT and deep2y ~= 0-HEIGHT then
                                    deep1y=0-HEIGHT
                                    else
                                    dm1()
                                    end
                                end
    function dm2()
                        deep2=math.random(0,3)
                        if deep2 < 1 and deep3>=1 and deep1>=1 then
                            deep2y=HEIGHT
                            elseif deep2 > 2 and deep3 <= 2 and deep1 <= 2 then
                                deep2y=0
                                elseif deep2 >= 1 and deep2 <= 2 and deep1y ~= 0-HEIGHT and deep3y ~= 0-HEIGHT then
                                    deep2y=0-HEIGHT
                                    else
                                    dm2()
                                    end
                                end
    function dm3()
                        deep3=math.random(0,3)
                        if deep3 < 1 and deep2>=1 and deep1>=1 then
                            deep3y=HEIGHT
                            elseif deep3 > 2 and deep2 <= 2 and deep1 <= 2 then
                                deep3y=0
                                elseif deep3 >= 1 and deep3 <= 2 and deep1y ~= 0-HEIGHT and deep2y ~= 0-HEIGHT then
                                    deep3y=0-HEIGHT
                                    else
                                    dm3()
                                    end
                                end
    function draw()
        sprite("Platformer Art:Crate",0,deep1y,WIDTH,HEIGHT)
        sprite("Platformer Art:Block Brick",0,deep2y,WIDTH,HEIGHT)
        sprite("Platformer Art:Block Special",0,deep3y,WIDTH,HEIGHT)
    end
    
  • dave1707dave1707 Mod
    Posts: 7,554

    @Paintcannon When I run this deep3y is always equal to HEIGHT not -HEIGHT. Are you trying to get them to randomly equal HEIGHT, 0, and -HEIGHT and that each of the 3 are different values.

  • dave1707dave1707 Mod
    Posts: 7,554

    @Paintcannon If you want the 3 values to be height, 0, - height randomly, but they have to be different, try this code.


    function setup() spriteMode(CORNER) parameter.watch("value[1]") parameter.watch("value[2]") parameter.watch("value[3]") value={} h={HEIGHT,0,-HEIGHT} tab={1,2,3} for z=1,3 do a=math.random(#tab) value[z]=h[tab[a]] table.remove(tab,a) end end function draw() sprite("Platformer Art:Crate",0,value[1],WIDTH,HEIGHT) sprite("Platformer Art:Block Brick",0,value[2],WIDTH,HEIGHT) sprite("Platformer Art:Block Special",0,value[3],WIDTH,HEIGHT) end
  • edited October 2014 Posts: 63

    That actually seems way more efficient than the solution I came up with where I had 3 while loops continuously reassigning each of the deep variables value until all three had different values.

Sign In or Register to comment.