I am trying to target a MC on the stage with a turret MC, through an external AS file. I thought what I have would work to test if the target MC is in range, but apparently it won’t. Any ideas?
I want the turret to have _targeting set to true when the enemy is within 50 pixels, and then clear the _targetEnemy value and search for the next enemy in range. This is for a turret game. If I’m going about this the wrong way, whats a solution?
ActionScript Code:
[LEFT][COLOR=#000000]**function**[/COLOR] turretActions[COLOR=#000000]([/COLOR]event:Event[COLOR=#000000])[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000ff]this[/COLOR]._placed[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#808080]*//this._targetEnemy=targetEnemy1;*[/COLOR]
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000ff]this[/COLOR]._targeting[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]**var**[/COLOR] myRadians:[COLOR=#0000ff]Number[/COLOR]=[COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]atan2[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000ff]this[/COLOR]._targetEnemy.[COLOR=#000080]x[/COLOR] - [COLOR=#0000ff]this[/COLOR].[COLOR=#000080]x[/COLOR] + [COLOR=#000080]10[/COLOR],[COLOR=#0000ff]this[/COLOR]._targetEnemy.[COLOR=#000080]y[/COLOR] - [COLOR=#0000ff]this[/COLOR].[COLOR=#000080]y[/COLOR] + [COLOR=#000080]10[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000]**var**[/COLOR] myDegrees:[COLOR=#0000ff]Number[/COLOR]=[COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]round[/COLOR][COLOR=#000000]([/COLOR]myRadians * [COLOR=#000080]180[/COLOR] / [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]PI[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#0000ff]this[/COLOR].[COLOR=#000080]turret[/COLOR].[COLOR=#000080]rotation[/COLOR]=myDegrees * -[COLOR=#000080]1[/COLOR];
[COLOR=#808080]*//this.turretbullet.rotation=myDegrees * -1;*[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#0000ff]else[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]**var**[/COLOR] t:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#000080]1[/COLOR]; t<[COLOR=#000080]10[/COLOR]; t++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR]![COLOR=#0000ff]this[/COLOR]._targeting[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]**var**[/COLOR] hitEnemy = [COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#ff0000]"targetEnemy"[/COLOR]+t[COLOR=#000000]][/COLOR];
[COLOR=#000000]**var**[/COLOR] hitEnemyDistX = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]abs[/COLOR][COLOR=#000000]([/COLOR]hitEnemy.[COLOR=#000080]x[/COLOR]-[COLOR=#0000ff]this[/COLOR].[COLOR=#000080]x[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000]**var**[/COLOR] hitEnemyDistY = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]abs[/COLOR][COLOR=#000000]([/COLOR]hitEnemy.[COLOR=#000080]y[/COLOR]-[COLOR=#0000ff]this[/COLOR].[COLOR=#000080]y[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000]**var**[/COLOR] hitEnemyDist:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]sqrt[/COLOR][COLOR=#000000]([/COLOR]hitEnemyDistX*hitEnemyDistX + hitEnemyDistY*hitEnemyDistY[COLOR=#000000])[/COLOR];
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR]hitEnemyDist<[COLOR=#000080]50[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR]![COLOR=#0000ff]this[/COLOR]._targeting[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]this[/COLOR]._targetEnemy = hitEnemy;
[COLOR=#0000ff]this[/COLOR]._targeting = [COLOR=#000000]**true**[/COLOR];x
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000ff]this[/COLOR]._targeting[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR]hitEnemyDist>[COLOR=#000080]50[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]this[/COLOR]._targeting = [COLOR=#000000]**false**[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[/LEFT]