ForumsProgramming ForumAI Targeting Problem Need Help

9 3336
rytherix
offline
rytherix
68 posts
Nomad

ok, now that I have this AI targeting script that I wrote, it enables allied players vehicles to scan around and whenever their scanner comes around and hits a target it locks on and tells the weapon to fire. Now this doesnt sound to complicated to me, but flash sure is having a hard time with it. Heres an example of the code I wrote
onClipEvent (load) {
//ts = target sequence
//ts 1 = targeted
//ts 2 = target locked
//ts 0 = finding target
this._x=random(501)
_y=60+random(80)
movespeed=-2

drag=1
missileloaded = 1;
hit._xscale = 100;
hit._yscale = 100;
}
onClipEvent (enterFrame) {
if (this._x<=90){
ts=3
}
if (ts == 3) {
missileloaded = 1;
hit._xscale = 100;
hit._yscale = 100;
ts = 0;
drag=1
}
if (ts == 0) {

if (drag==1){
this._x += movespeed;
if (this._x>=900) {
movespeed=-3
_y=90-random(60)
}
if (this._x<=400){
movespeed=3
_y=90-random(60)
}
}

if (hit._xscale<=10){
hit._xscale +=.5;
hit._yscale +=.5;
}
//A10
for (i=1; i<=_parent._parent.numA10; i++) {
if (hit.hitTest(_parent._parent["a10"+i].craft)) {
movespeed=-.5
hit._rotation -= 5;
if (hit._xscale>70) {
hit._xscale -=1;
hit._yscale -=1;
}
if (hit._xscale <= 70) {
if (missileloaded == 1) {
hit._xscale=10
hit._yscale=10
missileloaded = 0;
_parent.body.launcher.fire=1
}
drag=0
this._x = _parent._parent["a10"+i]._x;
this._y = _parent._parent["a10"+i]._y;
}
}
}
//UAV
for (i=1; i<=_parent._parent.numUav; i++) {
if (hit.hitTest(_parent._parent["uav"+i].uav)) {
movespeed=-.5
hit._rotation -= 5;
if (hit._xscale>70) {
hit._xscale -=1;
hit._yscale -=1;
}
if (hit._xscale <= 70) {
if (missileloaded == 1) {
hit._xscale=10
hit._yscale=10
missileloaded = 0;
_parent.body.launcher.fire=1
}
drag=0
this._x = _parent._parent["uav"+i]._x;
this._y = _parent._parent["uav"+i]._y;
}
}
}
}
}

I think this may be way too complicated for flash to run on everyframe several times. but what could I possibly replace it with thats simpler

  • 9 Replies
rytherix
offline
rytherix
68 posts
Nomad

actually the code is longer than that because theres 7 enemies not 2 as displayed in this example

MikeVarilek
offline
MikeVarilek
36 posts
Nomad

How many times per frame are the for loops going? (numA10 and numUav are not defined in the code sample you've given)
And using the _xscale and _yscale properties of objects is horribly inefficient.

rytherix
offline
rytherix
68 posts
Nomad

it runs 30 times a second, just running enterFrame (because the game is set to run at 30fps)
I think that would be great if I reduced it to maybe 15, but how would I do that? is there a enterFrame/2? lol
also I will take out the _xscale and _yscale, thanks for the advice

Louissi
offline
Louissi
66 posts
Peasant

you can make a counter... like:

if(bob == true){
//targetting code
bob = false;
}else if(bob == false){
bob = true;
}

it should run your code half of the time

rytherix
offline
rytherix
68 posts
Nomad

thanks for every1's help, I think I found a solution, which was to lower the variety of detectables, which is working for now
ya, I tried soemthing like that Louissi it was kinda funny, the game would run fast litereally every time it paused, wasnt practical, i lol'd

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Glad to hear it is working! Good luck! >

Louissi
offline
Louissi
66 posts
Peasant

haha

xXxTetimaruxXx
offline
xXxTetimaruxXx
418 posts
Nomad

Hope it works then

dragoncrusher
offline
dragoncrusher
773 posts
Peasant

Good one, hope it works.

Showing 1-9 of 9