Advertisement

Ai Help! [ActionScript3]

Started by June 14, 2009 06:35 PM
7 comments, last by Kylotan 15 years, 5 months ago
Okay I'm lost and need a little help. I don't really know what to do anymore. I have 2 objects Player1 and NPC. Player1 can be moved by the user and NPC is moved by computer. The game is tag... i can easily make the NPC chase the player by having it goto Player1. Now I want the NPC to run away from the Player as soon as the Player gets tagged (I have a variable called selectedPlayer that outputs either player1 or comp.)

if (selectedPlayer == "player1")
{
compRun();
}else if(selectedPlayer == "comp")
{
compChase();
}
compChase()

var speed:int = 4.5;
			if (player1.x > comp.x && !comp.frozen){
				comp.x += speed ;
			}else if(player1.x < comp.x && !comp.frozen){
				comp.x -= speed ;
			}
			if (player1.y > comp.y && !comp.frozen){
				comp.y += speed;
			}else if(player1.y < comp.y && !comp.frozen){
				comp.y -= speed;
			}
Now can someone give me psuedo code of point me in the right direction for compRun()
Take a look at Steering Behaviors: http://www.red3d.com/cwr/steer/. I think they will help you.
Advertisement
To make compRun(), you could simply reverse the '+'s and '-'s in compChase()...
Then he will just run into walls.....
Quote: Then he will just run into walls.....
There must be more to your question than what you posted then, because, generally speaking, an NPC can run into walls whether he's chasing the player or fleeing from the player.

Can you explain in more detail how you want the NPC to behave? What's the environment like? Is it a maze? Mostly open with a few walls? How's it represented? A grid? Using arbitrary geometry such as line segments or polygons?
I am extremely new to this whole game design thing.
I'm using Flash Actionscript 3. Basically i have 4 walls that form a square inside the square are two circles. 1 circle is red and thats player 1 and the other circle is green thats the NPC.

Player1 starts as "it" (its a tag game)
Comp has to run from Player 1

atm my movement is basically just
if W pressed
player1.x -= speed; (in flash Y is 0 on top of the screen and +x on the bottom)


I had a new idea what if i made a random point that isnt near the player for the npc to run too... so i tried to implement that but now its completely crashing flash when i run this script ... maybe u can see an error that i dont.
public function compRun(){tempSpr.x = Math.random()* 320;tempSpr.y = Math.random()* 320;if (player1.x >= tempSpr.x){while (player1.x – tempSpr.x <= 50){tempSpr.x = Math.random() * 320;}}else if (tempSpr.x >= player1.x){while (tempSpr.x – player1.x <= 50){tempSpr.x = Math.random() * 320;}}if (player1.y >= tempSpr.y){while (player1.y – tempSpr.y <= 50){tempSpr.y = Math.random() * 320;}}else if (tempSpr.y >= player1.y){while(tempSpr.y – player1.y <= 50){tempSpr.y = Math.random() * 320;}}if (tempSpr.x > comp.x && !comp.frozen){comp.x += speed ;}else if(tempSpr.x < comp.x && !comp.frozen){comp.x -= speed ;}if (tempSpr.y > comp.y && !comp.frozen){comp.y == speed;}else if(tempSpr.y < comp.y && !comp.frozen){comp.y -= speed;}}
Advertisement
Quote: Original post by QpSmiley
Then he will just run into walls.....
You didn't mention walls! :oP

In your code you have:
comp.y == speed;
when surely you meant:
comp.y += speed;

The idea of a random spot is basicly sound, unless the spot chosen is directly behind the attacker: then comp would head straight towards his attacker!
Better to pick a random spot which is closer to the runner than the attacker, but further from the attacker than the runner's current location.

That link that pek supplied is well worth a look BTW!

I'm gonna try something else i think this will work I'm running into a problem though.

Basically I'm gonna look where the player is and then go the opposite... my stage is 320x320 so... im gonna see if player1.x * .5 > 160 // 160 is 320/2

function moveComp(event:Event){	if (selectedPlayer == "player1")	{		compRun();				}else if(selectedPlayer == "comp")	{		compChase();					}}


		public function compRun()		{			if (player1.x * .5 > 160 && !comp.frozen){				comp.x -= speed ;				trace("Player1.x :" + player1.x + " comp.x: " + comp.x);							}else if(player1.x * .5 < 160 && !comp.frozen){				comp.x += speed ;				trace("Player1.x :" + player1.x + " comp.x: " + comp.x);										}			if (player1.y * .5 > 160 && !comp.frozen){				comp.y -= speed;				trace("Player1.y :" + player1.y + " comp.y: " + comp.y);							}else if(player1.y *.5 < 160 && !comp.frozen){				comp.y += speed;								trace("Player1.y :" + player1.y + " comp.y: " + comp.y);							}																					}


but this is whats happening...(red dot is player1) (green dot is comp)
http://www.kongregate.com/games/BlackCia/supertag-beta

So what should I do it looks like its only looking at player1's initial position not player1's updated position
To me, it seems to do exactly what you want it to.

This topic is closed to new replies.

Advertisement