by Fuzzy » Sun Mar 26, 2006 12:43 am
I have a little function that I use that may suit your purposes. I was going to save for later and use it in a demo or tutorial, but I think this is a good place to introduce it to the GE users.
go to your global code script editor, and type this in.
int Lint(int a, int z, double j) {return (int)(a*(1-j) + z*j);}
At the bottom, there is a blank beside name. Type in something descriptful, like Useful Functions. Hit add.
Now, I know it looks a little obfuscated, but its really simple, and you dont need to know about the guts of it. Here is how you use it. (you can only access it in the script editor, it doesnt appear in the event or action menus)
You feed it three numbers, two that are integers(no decimals), and one that is a floating point number(use decimal points). When you do this, the function will return a number which is partway between the first two numbers, using the third number as a distance. It rounds down to the nearest whole number.
Thats messy, I'll give you some examples.
Pretend your AI paddle is at x=100, and the ball is heading towards x=130. You want the paddle to head that way, and hopefully intercept the ball. The AI on this level is 70% effective, or 0.7.
In the place that your paddle does its thinking, you place this line:
TargetX=Lint(100,130,0.7);
TargetX is a variable that is used to store where the paddle is headed of course. TargetX should now be equal to 121, if all goes right, and you use your draw and movement functions(like moveto()) to move the paddle there.
The nice thing about this is it removes the need to think about whether or not you want to block the ball. The third number, in conjunction with the size of the paddle determines that.
The function also saves you the problem of figuring out whether you want to move left or right; It doesnt matter if you put Lint(130,100, 0.7) or Lint(100,130,0.7). THe only thing to watch for is that the third number is not greater than 1.0.
Please reply if any of this is unclear.
Oh, Lint stands for Linear Interpolation, if you wrere wondering.
Mortal Enemy of IF....THEN(and Inspector Gadget)
Still ThreeFingerPete to tekdino