Title: Tutorial Five: Functions Post by: Access_Denied on June 14, 2007, 03:12:38 AM In this lesson, we'll be on functions. Functions are extremely easy. So let's get started.
--Functions --August 11,2006 red = Color.new(255,0,0) Now we are going to identify our function. Functions are usually identified befor the main loop. function MyFirstFunction() This is just the start of our function. This first line just tells the PSP that we are making a function. while true do screen:clear() screen:print(100,100,"I just made a function!",red) screen.waitVblankStart() screen.flip() end Now some of you may think that we just made our main loop. We didn't. We made our function. So when we call up our function in our real main loop, it's going to perform those lines. You should know what they mean by now. Now we just need to finish off our function with one simple line: end You're done. Your function is complete. Now lets move on to our main loop while true do pad = Controls.read() screen:clear() Looks familiar right? Now for some if then statements. We want to make it so when we press CIRCLE it does our function. It's very simple. Watch: if pad:circle() then MyFirstFunction() end This makes it when we press CIRCLE it does our function. NOTE: You could have named your function anything. Not just "MyFirstFunction" Now let's finish it off screen.waitVblankStart() screen.flip() end Run your code, then come back. Done. Good. Now, if you ran it, you found out that once you run your function, there's no way to go back. Well let's fix that. Go back to your function and add the following line under your "screen:clear()" pad = Controls.read() Then add this line into your function, if pad:triangle() then return end This makes it so when you are in your function, if you press TRIANGLE, it will go back. Now save your script and run it. |