Community

AI Targeting Problem Need Help

Posted Jul 1, '08 at 8:14pm

rytherix

rytherix

60 posts

Gold - Squire

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

Posted Jul 1, '08 at 8:15pm

rytherix

rytherix

60 posts

Gold - Squire

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

 

Posted Jul 1, '08 at 9:46pm

MikeVarilek

MikeVarilek

38 posts

Wood - Squire

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.

 

Posted Jul 1, '08 at 10:51pm

rytherix

rytherix

60 posts

Gold - Squire

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

 

Posted Jul 2, '08 at 1:53pm

Louissi

Louissi

26 posts

Iron - Squire

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

 

Posted Jul 2, '08 at 11:14pm

rytherix

rytherix

60 posts

Gold - Squire

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

 

Posted Jul 2, '08 at 11:20pm

Klaushouse

Klaushouse

1,700 posts

Iron - Lord

Glad to hear it is working! :D Good luck! >:D

 

Posted Jul 7, '08 at 2:45pm

Louissi

Louissi

26 posts

Iron - Squire

haha

 

Posted Jul 7, '08 at 4:04pm

xXxTetimaruxXx

xXxTetimaruxXx

441 posts

Wood - Knight

Hope it works then ;)

 

Posted Jul 7, '08 at 5:05pm

dragoncrusher

dragoncrusher

784 posts

Wood - King

Good one, hope it works.

 
Reply to AI Targeting Problem Need Help

You must be logged in to post a reply!