Drawing a triangle
-
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: frontI 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.
-
"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.
-
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.
-
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. -
@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.