View Full Version : Enemies run at you and attack
Leo-The-CRaZeD
October 1st, 2006, 11:55 PM
I have a game that is ALMOST fully finished. I have everything els done, except for the enemy AI. Let's say I have an MC called "enemy", and I have the main character MC, and the enemy has to chase the person in the direction they are going (It's a side scroller) and attack with the "Attack" animation. How do I do this? Thanks!
evildrummer
October 3rd, 2006, 10:49 AM
explain what you want: do you mean so the AI runs at the character then when within a ceratn range runs an animation called attack?
if so I think this might help.
left = (_root.char._x - 50);
left = (_root.char._x + 50);
_root.enemy.onEnterFrame = function () {
if (_root.enemy._x < left) {
_root.enemy.gotoAndPlay("walk_right");
_root.enemy._x++;
} else if (_root.enemy._x > right) {
_root.enemy.gotoAndPlay("walk_left");
_root.enemy._x--;
} else {
_root.enemy.gotoAndPlay("attack");
//code for attack
};
evildrummer
October 3rd, 2006, 03:31 PM
Ok, I made some MAJOR improvementrs to the last script I posted so here it is, but to make it work make sure the registration point of the enemy and char are in the middle.
_root.enemy_mc.onEnterFrame = function () {
charW = (_root.char_mc._width / 2);
range = 50
enemyW = (_root.enemy_mc._width / 2);
if ((_root.enemy_mc._x + enemyW ) < ((_root.char_mc._x - charW) - range)) {
_root.enemy_mc.gotoAndPlay("Move_RIght");
_root.enemy_mc._x++;
} else if ((_root.enemy_mc._x - enemyW ) > ((_root.char_mc._x + charW) + range)) {
_root.enemy_mc.gotoAndPlay("Move_left");
_root.enemy_mc._x--;
} else {
//code for attack
}
}
evildrummer
October 4th, 2006, 09:22 AM
Another slight modification to allow quicker changes:
_root.enemy_mc.onEnterFrame = function () {
range = 50;
speed = 50;
charW = (_root.char_mc._width / 2);
enemyW = (_root.enemy_mc._width / 2);
if ((_root.enemy_mc._x + enemyW ) < ((_root.char_mc._x - charW) - range)) {
_root.enemy_mc.gotoAndPlay("Move_RIght");
_root.enemy_mc._x += speed;
} else if ((_root.enemy_mc._x - enemyW ) > ((_root.char_mc._x + charW) + range)) {
_root.enemy_mc.gotoAndPlay("Move_left");
_root.enemy_mc._x -= speed;
} else {
//code for attack
}
}
mnkeymasta
October 4th, 2006, 06:11 PM
ok.... why did you just triple post?
evildrummer
October 5th, 2006, 05:41 PM
well if you looked at times they were done at different times + I thought it would be noticed more and as I did them in stages I thought he would like to be able to see how each one is formed thus easier to learn!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.