PDA

View Full Version : rpg AI



mordormaster
January 24th, 2008, 02:03 PM
hiya everyone, my npc problem is now fixed and now i was wondering if it was possible to have ai in a scrolling background, so far i tried having the enemy have normal system where it seekers the player on the screen but because i want it to scroll with the background is there anyway i can get it to scroll with the background and still get closer to the player if he comes close

therobot
January 24th, 2008, 06:59 PM
Sounds like you just need to create a decent hierarchy for your movieClips.

_root.map.enemies
_root.map.hero
_root.map.items
etc.

so when you scroll the map...
_root.map._x += scrollSpeed
...everything contained in the map scrolls with it.

joran420
January 24th, 2008, 09:33 PM
are you using a tilebased system? if so beware of scrolling backgrounds...they can cause some pretty serious lag...

mordormaster
January 25th, 2008, 12:51 PM
could i have an example of the system your describing please, i just had a go but with little sucess, and no its an artistic based system which i guess means slightly less lag

mordormaster
January 28th, 2008, 01:25 PM
could anybody send me an example please, i have most other things working and most of the art done so all im waiting for is this code before i could finish my game, could anybody please

ArmoredSandwich
January 28th, 2008, 02:58 PM
In English!

It isn't really rocket science. What you do is attach your "enemies" (those who "seekers", what the **** is that anyways) to your background movieclip rather than on the root. If you then move (or scroll) the background movieclip you'll automagically scroll the enemies too.

You could also add every enemy to an array and then loop trough the array, incrementing or decreasing the x/y position of every movieclip in it like this:



for (var i = 0; i < _root.enemiesArray.lenght; i ++)
{
_root.enemiesArray[i]._x -= _root.enemySpeed;
}
In a language you might understand!

It isnt really rohcet science what i do is attach ur enemies those who seekers what the **** is that anywyasd to ur backgr0und movieclip rather than on the root if you then move or scroll the background movieclip ull automagically scroll the enemies 2 u could also add ev3ry enemy to an array and then loop trough the array incrementing or decreasing the xy position of every movieclip in it like this for (var i = 0; i < _root.enemiesArray.lenght; i ++) {
_root.enemiesArray[i]._x -= _root.enemySpeed;
}

therobot
January 29th, 2008, 08:55 PM
It isnt really rohcet science what i do is attach ur enemies those who seekers what the **** is that anywyasd to ur backgr0und movieclip rather than on the root if you then move or scroll the background movieclip ull automagically scroll the enemies 2 u could also add ev3ry enemy to an array and then loop trough the array incrementing or decreasing the xy position of every movieclip in it like this for (var i = 0; i < _root.enemiesArray.lenght; i ++) {
_root.enemiesArray[i]._x -= _root.enemySpeed;
}

ty ytmndzorz
thast wht i was sayign1

Jerryscript
January 30th, 2008, 03:33 AM
If you put your enemies in the same container as your scrolling background, then you can transform your enemy and hero coordinates to determine the direction your enemy needs to move to go towards the hero:
background.enemy.onEnterFrame=function(){
var pointA = getGlobal(this);
var pointB = getGlobal(hero);
if(pointA.x>pointB.x){
this._x-=5;
}else{
this._x+=5;
}
};

function getGlobal(clip){
var it = clip;
var point = new Object();
point.x = clip._x;
point.y = clip._y;
while(it._parent != _root){
it = it._parent;
}
if(it != clip){
it.localToGlobal(point);
}
return point;
}

mordormaster
January 30th, 2008, 12:43 PM
i have spent a good part of today trying to get this code to work and still it wont work for me, could somebody please help me to get it working (and sorry howling shooter for using your work as a basis)

mordormaster
February 1st, 2008, 05:51 PM
coudl somebody help me please, i have just got items and spells working so could somebody help me with this, it would almost complete my game

parkerdc
February 1st, 2008, 11:25 PM
You have 3 problems...

1) Your hero is not moving. It might look like it, but he's not.

The hero _x and _y never change, so when you try to compare the values, nothing will get triggered. You are moving the map underneath the hero. Fixed with the following code:

var herox=(_root.map._width/2)-_root.map._x;
var heroy=(_root.map._height/2)-_root.map._y;

2) You items are not centered or zeroed on the workspace.

This makes the above code off, I have fixed this in the attached file.

3) All of your symbols and code are on one frame.

It makes it easier to debug your when when it's on different frames. I changed some of the items.

The chase code would be updated as follows.

if(this._x>herox+8 && this._x>herox){
this._x-=5;
}else if(this._x<herox-8 && this._x<herox){
this._x+=5;
}
if(this._y>heroy+8 & this._y>heroy){
this._y-=5;
}else if(this._y<heroy-8 & this._y<heroy){
this._y+=5;
}

The +8,-8 is how close the enemy will stop chasing and prevents jitter enemy movement. If the hero moves away, the enemy will resume chasing.

mordormaster
February 2nd, 2008, 06:02 AM
thank you very very very much :)
i shal now be able to add him into my world and let him loose on my hero, but if i can may i ask if three things are possible, is it possible to make the enemy chase within a certain distance? is it possible to make the enemy face the way my hero is? is it possible to stop the enemy moving throught backgrounds?
thank you veyr much to everyone who has helped me especially jerryscript and parkerdc

parkerdc
February 2nd, 2008, 12:00 PM
mordormaster

A few questions to make sure I understand your post.

>chase within a certain distance
Do you mean when the hero comes close to the enemy it awakes and chases? When the hero moves outside the range, the enemy goes to sleep.
**Added JerryScript's updated code that does this**
Or
Do you want the enemy to stay close to a cave, and is on an invisible leash. meaning when it gets 200 pixels away from the cave it stops chasing.

>enemy face the hero
Do you mean 8 positions? (left,right,up,down,up-left,up-right, etc)
OR
360 rotate.
**Added JerryScript's 360 code that does this**

Post a sample of the backgrounds so I can see how you plan to put them on the map.

Jerryscript
February 2nd, 2008, 12:40 PM
I'm not sure what changes parkerdc made to the code, I can't view FLAs. Using the code I posted, here is a modification that will only have the enemy chase the hero if he is within 200px, and it will change the rotation of the enemy so it is always facing the hero. It will make the enemy move in a full 360 degrees, so you may want to change it to only allow certain directions.

background.enemy.onEnterFrame=function(){
pointA = getGlobal(this);
pointB = getGlobal(hero);
// determine the distance using pythag's theorem
dist = Math.sqrt( Math.pow( pointA.x - pointB.x, 2) + Math.pow( pointA.y - pointB.y, 2) );
// only chase if within 200px, and stop if touching
if(dist < 200 && dist > this._width){
// determine the angle between the hero and the enemy
angle = Math.atan2( pointB.y - pointA.y, pointB.x - pointA.x );
// rotate enemy to face the hero
this._rotation = angle * (180 / Math.PI);
// move the enemy along the vector that leads to the hero
// adjust the value of 5 to change the speed
this._x += Math.cos(angle) * 5;
this._y += Math.sin(angle) * 5;
}else{
// normal enemy movement when not chasing goes here
}
};

function getGlobal(clip){
var point = new Object();
point.x = clip._x;
point.y = clip._y;
var it = clip;
while(it._parent != _root){
it = it._parent;
}
if(it != clip){
it.localToGlobal(point);
}
return point;
}

parkerdc
February 2nd, 2008, 12:56 PM
JerryScript, great update on the chase code.

I have included JerryScript's new chase code.

mordormaster
February 3rd, 2008, 07:13 AM
thank you guys a millon :) i am acdtually ridiculously impressed with that, i can now have my game working, it wil be realised soon and you will be mentioned in the credits :)