Firstly, all For loops need a Next to terminate them, so it might be a typo in the book.
If you want the function to be able to access the player1 variable, you either need to declare it as a Global, or pass it to the function. The reason you're getting the "variable must be type" error is because Blitz is creating a local variable called "player1" inside this function, but it automatically makes it an integer and not a type.
You'll also need to make sure the "moveSpeed" variable is either a global or a constant, otherwise the function won't be able to access it.
I apologise if you know any of this already :)
One way to do it is put "Global" before the first use of the player1 variable, probably when it's first created.
This will let all functions in your program access and modify it. This can be a bad thing though, so you may want to pass it to the function instead. There's one gotcha here though. When passing variables to a function normally, they are passed by value. In other words, if you pass a variable called "myVar" and modify it within the function, the changes will only be effecting inside the function - try this for a good example:
HOWEVER, if you pass an object (a type instance) to a function, it is passed by reference. This means that changes in the function will change it outside the function too. Here's another example:
Hopefully that made sense. Although this behaviour can be a little confusing to begin with, it makes things much easier later on.
Anyway, to answer you question (finally!), here is how to fix your problem by passing the player object to the function:
Hope that helps :)
If you want the function to be able to access the player1 variable, you either need to declare it as a Global, or pass it to the function. The reason you're getting the "variable must be type" error is because Blitz is creating a local variable called "player1" inside this function, but it automatically makes it an integer and not a type.
You'll also need to make sure the "moveSpeed" variable is either a global or a constant, otherwise the function won't be able to access it.
I apologise if you know any of this already :)
One way to do it is put "Global" before the first use of the player1 variable, probably when it's first created.
Global player1.Player
This will let all functions in your program access and modify it. This can be a bad thing though, so you may want to pass it to the function instead. There's one gotcha here though. When passing variables to a function normally, they are passed by value. In other words, if you pass a variable called "myVar" and modify it within the function, the changes will only be effecting inside the function - try this for a good example:
Local myVar = 1 print "Value before function: " + myVar valueTest(myVar) print "Value after function: " + myVar Function valueTest(someVar) someVar = 3 Print "Value within function: " + someVar End Function
HOWEVER, if you pass an object (a type instance) to a function, it is passed by reference. This means that changes in the function will change it outside the function too. Here's another example:
Type myType Field someValue$ End Type Local test.myType = New myType test\someValue = "Hello" Print "Before function: " + test\someValue typeTest(test) Print "After function: " + test\someValue Function typeTest(this.myType) test\someValue = "Goodbye" End Function
Hopefully that made sense. Although this behaviour can be a little confusing to begin with, it makes things much easier later on.
Anyway, to answer you question (finally!), here is how to fix your problem by passing the player object to the function:
; Main code Local player1.Player = new Player ; Call the key control KeyControl(player1) Function KeyControl(this.Player) If KeyDown(KEY_LEFT) this\x = this\x - moveSpeed EndIf ; etc End Function
Hope that helps :)