Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Drawing a triangle

    Help
    3
    5
    173
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      Richard F last edited by

      I am trying to draw a triangle pointing in a specific direction based on a variable using the following code:

      direction = 0
      If direction == 0 then
         Front = {50,50}
         Back1 = {30,100}
         Back2 = {30,100}
      Else
         Front = {50,150}
         Back1 = {30,100}
         Back2 = {70,100}
      Endif
      Triangle (Front, Back1,Back2,red,false)
      Update()
      Sleep(2)
      

      It returns the following error

      Line 11
      Function triangle
      Variable does not exist: front

      I believe front is defined one way or another in the if statement and I can't see how the variable does not exist. So I must be doing something wrong, but I can't work out what.

      Any help will be greatly appreciated.

      1 Reply Last reply Reply Quote 0
      • N
        Nisse5 F last edited by

        "Front" is defined as a local variable within the block. When the execution reach "Endif", the scope of the variable doesn't exists anymore.

        You can put a "Front={0,0}" before the if statement will fix it.

        1 Reply Last reply Reply Quote 2
        • sys64738
          sys64738 F last edited by

          Can paste the triangle line into the if and else also so it has access to those variables, or make all the variables global like Nisse said.

          I was thinking about making vector version of the asteroids game but making the triangle ship face any direction seemed like that most difficult bit, I am sure there is some math equation for it though.

          R 1 Reply Last reply Reply Quote 1
          • R
            Richard F last edited by

            Thanks, that sounds like it'll work, I will test it later. I knew variables defined within a function were local, but didn't realise it applied to an if statement too. It explains another problem I had come across where I had to pass what I thought was a global variable into a function to use it, it must have been locally defined within an if statement.
            Thanks a lot.

            1 Reply Last reply Reply Quote 0
            • R
              Richard F @sys64738 last edited by Richard

              @sys64738 the equation for pointing any direction based on a mid point x,y and an angle from straight up would be:

              point = []
              for i = 0 to 3 loop
                 point[i]= {x + (length*sin(angle+(120*i))),y - (length*cos(angle+(120*i)))}
              repeat
              triangle (point[0],point[1],point[2],white,true)
              

              Or at least something very similar.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post