Skip to main content
Inspiring
August 5, 2011
Answered

distance wont work...........is this script wright????

  • August 5, 2011
  • 1 reply
  • 688 views

what i whant is when any of the units get to a certain distance of the enemy it should trace(shoot).

can anyone help me...............

................................................................................

var unitA:Array=[unit,unit1,unit2];

var c;

var s;

var deltaX;

var deltaY;

var dist;

var range;

_root.onEnterFrame = function() {

   var unit = unitA;

c = _root.unit;

s = _root.enemy;

deltaX = c._x-s._x; deltaY = c._y-s._y;

// rounded distance

dist = Math.sqrt((deltaX*deltaX)+(deltaY*deltaY));

}

function rangetoshoot(){

if(unit.dist >= 100);

trace(shoot);

}

.........................................................................................................

This topic has been closed for replies.
Correct answer Ned Murphy

The script now looks like this but all i get is undifined.

var deltaX;

var deltaY;

var dist;

_root.onEnterFrame = function() {

   var unitB = unitA;

deltaX = unitB._x-_root.enemy._x;

deltaY = unitB._y-_root.enemy._y;

// rounded distance

dist = Math.sqrt((deltaX*deltaX)+(deltaY*deltaY));

if(unitB.dist <= 100);

trace(shoot);

}


The code you show is incomplete at best.  And you still do not have anything that loops thru the array to check each unit.  The undefined could be pointing at your lack of defining a value for i, though it could also be that you haven't define a value for shoot.  If shoot is not a variable and your intention is to display the word "shoot", then you need to use quotes

1 reply

Ned Murphy
Legend
August 5, 2011

The code is not right and not a clear indication of intentions. Are you familiar with using loops?  If not, you need to learn about them so that you can test each unit to see if it is where you want it to be to trace.  Also, you need to use a conditional... your rangetoshoot function has one, but your code doesn't call that function.  Also, I am not sure if you are testing for the right condition, so just fair warning... it will trace only when the unit is farther than 100, not less than.

Can you explain what the units are that you are using in these lines of code?

var unit = unitA;

c = _root.unit;

because _root.unit will not be that same as the "var unit" you just assigned.

Inspiring
August 5, 2011

ahh ok i whant it to trace when i am 100 are lower. well im using the units(unit,unit1,unit2) from my array and i thought this unit linked with this one

var unit = unitA;

c = _root.unit;

Ned Murphy
Legend
August 5, 2011

For the lower than you would use <=

If you want that unit to be the same unit, then use that unit and not "c", in fact, why substitute at all if there's only two lines using the c and s

var unit = unitA;

deltaX = unit._x-_root.enemy._x;

deltaY = unit._y-_root.enemy._y;