PDA

View Full Version : simple AI



JapanMan
August 17th, 2008, 01:47 AM
I'm trying to make a little blue square chase a little red dot, controlled by your mouse. I have:



2 Layers, 1 called "mouse" and 1 called "enemy."
An MC named "mouse" (instance name "mouse") with this code:



onClipEvent (load) {
Mouse.hide();
}
onClipEvent (enterFrame) {
this._x =
_root._xmouse;
this._y = _root._ymouse;
updateAfterEvent(mouseMove);
}


An MC named "enemy" (instance name "enemy") with this code:



onClipEvent (load) {
var speed = 3;
if (mouse._x<this._x) {
this._x -= speed;
}
if (mouse._x>this._x) {
this._x += speed;
}
}
What am I doing wrong?

rahul_7star
August 17th, 2008, 03:04 AM
onClipEvent (enterFrame) instead of onClipEvent (load)


onClipEvent (enterFrame)) {
var speed = 3;
if (_root.mouse._x<this._x) {
this._x -= speed;
}
if (_root.mouse._x>this._x) {
this._x += speed;
}
}

where mouse is the name of square box.


I'm trying to make a little blue square chase a little red dot, controlled by your mouse. I have:



2 Layers, 1 called "mouse" and 1 called "enemy."
An MC named "mouse" (instance name "mouse") with this code:



onClipEvent (load) {
Mouse.hide();
}
onClipEvent (enterFrame) {
this._x =
_root._xmouse;
this._y = _root._ymouse;
updateAfterEvent(mouseMove);
}


An MC named "enemy" (instance name "enemy") with this code:



onClipEvent (load) {
var speed = 3;
if (mouse._x<this._x) {
this._x -= speed;
}
if (mouse._x>this._x) {
this._x += speed;
}
}
What am I doing wrong?

JapanMan
August 17th, 2008, 03:55 AM
YES! Thank you! That's exactly what it needed.

JapanMan
August 17th, 2008, 04:15 PM
Ok, Round 2:

So the AI works fine now, but what good is an AI enemy if it can't actually kill you? I'm trying to do the hitTest for you (mouse) and the evil squares o' doom (enemy). I also now have a Dynamic Text object (score) that counts 5 more every second. After 10 seconds - 50 points - the movie jumps ahead to a new Key Frame in the "Enemy" layer and adds another evil square, with a faster speed.

I've been adding squares by Ctrl+Click-dragging the previous one, so they are all named "enemy" and have the instance name "enemy."

I now added another Layer - running the whole length of the movie - with my collision testing in it, so that if you are hit at any time, it registers, and i don't have to repeat the code in each of my divisions of the timeline. The code in it is:


if (_root.mouse.hitTest(_root.enemy)) {
dead = "You Dead";
}
where "dead" is another Dynamic Text box that I'm using as my easy way to see if the Test was registered; It hasn't, which is why I'm here :(

If need be, I can post the .fla file. Thanks in advance!

SparK_BR
August 17th, 2008, 06:23 PM
dead.text = "You Dead"

JapanMan
August 17th, 2008, 11:40 PM
dead.text = "You Dead"

no, I tried that just now, and it didn't make a difference. There's still no response.

EDIT: Ha! tried changing what it did if the test was true. I had it play a new frame of the "mouse" MC and it worked. Guess there was just a problem with the text setup. Anyway, it's working now.

SparK_BR
August 24th, 2008, 07:29 PM
make sure the text box has a "dead" instance name
then it will work