I am writing a function for a game, so that an enemy moves towards the player at a constant speed. I did this using trig identities such as cos^2 + sin^2 = 1 to get a ratio of x displacement to y displacement, then multiplied the ratios by the speed. But for some reason the speed keeps increasing over time. Here is the snippet of code from the games AS file:
[LEFT][COLOR=#993300]for[/COLOR] each [COLOR=#000000]([/COLOR] [COLOR=#993300]var[/COLOR] enemy1:Enemy1 [COLOR=#993300]in[/COLOR] enemies1 [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Controls Enemy1's behaviour*[/COLOR] [COLOR=#F000F0]*//Movement:*[/COLOR] enemy1.[COLOR=#000000]movecalc[/COLOR][COLOR=#000000]([/COLOR] player.[COLOR=#000000]x[/COLOR], player.[COLOR=#000000]y[/COLOR], enemy1speed[COLOR=#000000])[/COLOR]; enemy1.[COLOR=#000000]movex[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]false[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#F000F0]*//Hit tests followed by reversed function if true*[/COLOR] enemy1.[COLOR=#000000]movey[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]false[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#F000F0]*//Hit tests followed by reversed function if true*[/COLOR] [COLOR=#000000]}[/COLOR]
[/LEFT]
And this is the (rather long!) snippet from the enemies AS file:
[LEFT][COLOR=#993300]public[/COLOR] [COLOR=#993300]function[/COLOR] movecalc[COLOR=#000000]([/COLOR] px:[COLOR=#993300]Number[/COLOR], py:[COLOR=#993300]Number[/COLOR], speed:[COLOR=#993300]Number[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] < [COLOR=#000000]0[/COLOR] && [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] < [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Q1*[/COLOR] angleAlpha = [COLOR=#993300]Math[/COLOR].[COLOR=#993300]atan[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]y - py[COLOR=#000000])[/COLOR] / [COLOR=#000000]([/COLOR]x - px[COLOR=#000000])[/COLOR] [COLOR=#000000])[/COLOR]; angleTheta = [COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]3[/COLOR] * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]PI[/COLOR][COLOR=#000000])[/COLOR] / [COLOR=#000000]2[/COLOR] [COLOR=#000000])[/COLOR] + angleAlpha [COLOR=#000000])[/COLOR]; xval = -speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]cos[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; yval = -speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]sin[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] > [COLOR=#000000]0[/COLOR] && [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] < [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Q2*[/COLOR] angleAlpha = [COLOR=#993300]Math[/COLOR].[COLOR=#993300]atan[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] / [COLOR=#000000]([/COLOR]y - py[COLOR=#000000])[/COLOR] [COLOR=#000000])[/COLOR]; angleTheta = angleAlpha; xval = speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]sin[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; yval = -speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]cos[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] > [COLOR=#000000]0[/COLOR] && [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] > [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Q3*[/COLOR] angleAlpha = [COLOR=#993300]Math[/COLOR].[COLOR=#993300]atan[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] / [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] [COLOR=#000000])[/COLOR]; angleTheta = [COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]PI[/COLOR] / [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR] + angleAlpha [COLOR=#000000])[/COLOR]; xval = speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]cos[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; yval = speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]sin[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] < [COLOR=#000000]0[/COLOR] && [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] > [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Q4*[/COLOR] angleAlpha = [COLOR=#993300]Math[/COLOR].[COLOR=#993300]atan[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]x - px[COLOR=#000000])[/COLOR] / [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] [COLOR=#000000])[/COLOR]; angleTheta = [COLOR=#993300]Math[/COLOR].[COLOR=#993300]PI[/COLOR] + angleAlpha; xval = -speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]sin[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; yval = speed * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]pow[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]cos[/COLOR][COLOR=#000000]([/COLOR]angleAlpha[COLOR=#000000])[/COLOR], [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR] [COLOR=#993300]if[/COLOR] [COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] == [COLOR=#000000]0[/COLOR] && [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] == [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Over Target*[/COLOR] xval = [COLOR=#000000]0[/COLOR]; yval = [COLOR=#000000]0[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] == [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Same x as player but different y*[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] > [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] xval = [COLOR=#000000]0[/COLOR]; yval = speed; angleTheta = [COLOR=#993300]Math[/COLOR].[COLOR=#993300]PI[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR][COLOR=#000000]{[/COLOR] xval = [COLOR=#000000]0[/COLOR]; yval = -speed; angleTheta = [COLOR=#000000]0[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]py - y[COLOR=#000000])[/COLOR] == [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#F000F0]*//Same y as player, different x*[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR]px - x[COLOR=#000000])[/COLOR] > [COLOR=#000000]0[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] yval = [COLOR=#000000]0[/COLOR]; xval = speed; angleTheta = [COLOR=#000000]([/COLOR][COLOR=#993300]Math[/COLOR].[COLOR=#993300]PI[/COLOR] / [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR][COLOR=#000000]{[/COLOR] yval = [COLOR=#000000]0[/COLOR]; xval = -speed angleTheta = [COLOR=#000000]([/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]3[/COLOR] * [COLOR=#993300]Math[/COLOR].[COLOR=#993300]PI[/COLOR][COLOR=#000000])[/COLOR] / [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#000000]}[/COLOR] rotation = [COLOR=#000000]([/COLOR]angleTheta * [COLOR=#000000]([/COLOR][COLOR=#000000]180[/COLOR] / [COLOR=#993300]Math[/COLOR].[COLOR=#993300]PI[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000])[/COLOR]; [COLOR=#000000]}[/COLOR] [COLOR=#993300]public[/COLOR] [COLOR=#993300]function[/COLOR] movex[COLOR=#000000]([/COLOR]rev:[COLOR=#993300]Boolean[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] rev == [COLOR=#993300]true[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] xpos -= xval; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR][COLOR=#000000]{[/COLOR] xpos += xval; [COLOR=#000000]}[/COLOR] x = xpos; [COLOR=#000000]}[/COLOR] [COLOR=#993300]public[/COLOR] [COLOR=#993300]function[/COLOR] movey[COLOR=#000000]([/COLOR]rev:[COLOR=#993300]Boolean[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR] rev == [COLOR=#993300]true[/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] ypos -= yval; [COLOR=#000000]}[/COLOR] [COLOR=#993300]else[/COLOR][COLOR=#000000]{[/COLOR] ypos += yval; [COLOR=#000000]}[/COLOR] y = ypos; [COLOR=#000000]}[/COLOR]
[/LEFT]
Any idea why the speed would be increasing? I’ve never coded a game before so i would like to know where im going wrong and why it doesn’t work? The link to it is here