neondevil
05-02-2009, 06:19 PM
I'm making a game where the player controls 2 characters (albert and ozzy) in the code. I was trying to make it so the enemy would seek them out if it was close but then after they collided once it would retreat before trying again. So far all it's doing is seeking them out but then getting stuck inside of them and not retreating.
What am I doing wrong?
package{
//This is always used as the stage is like one big movie clip
import flash.display.MovieClip;
//Click on the event in the addeventlistener to find what you have to import and now ou can pull everything from that class into this script. It brings in the preexisting class.s
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
//We're extending the movieclip class because we're adding functionality to it
public class Grunt extends MovieClip{
//This is the variable that I'd turn on when the grunt gets hit so it can knock into other enemies and damage them
public var grunt_hitbyplayer:Boolean = false;
public var grunt_speed:Number = 0.5;
public var grunt_angle:Number = Math.random()*360;
public var grunt_drag:Number = .90;
public var grunt_accel:Number = .2;
public var force:Number = .5;
public var health:Number = 5;
public var slow:Boolean = false;
//This is where I was trying to solve the problem of this by making it a new object. It doesn't like it.
public var This:Object = new Object();
public var hitting:Boolean = false;
//Let's calculate some mother****ing distance between the enemy and albert and ozzy
public var distance_enemyandozzy:Number;
public var distance_enemyandalbert:Number;
//These are to turn on and off if it's tracking the player's characters
public var track_albert:Boolean = true;
public var track_ozzy:Boolean = true;
var run_away_timer_ozzy:Timer = new Timer(5000, 0);
var run_away_timer_albert:Timer = new Timer(5000, 0);
//This is the constructor function with the same name as the class, it gets executed first as soon as the class is called
public function Grunt(){
//This will give everything in the class an event listener
this.addEventListener(Event.ENTER_FRAME, onFrame);
run_away_timer_ozzy.addEventListener(TimerEvent.TI MER, running_away_ozzy);
run_away_timer_albert.addEventListener(TimerEvent. TIMER, running_away_albert);
//These are for the slow command, it keeps returning a null object so they're commented out. I have no idea why it does this.
//This.parent.stage.addEventListener(MouseEvent.MOUS E_DOWN, slow_down);
//This.parent.stage.addEventListener(MouseEvent.MOUS E_UP, normal_speed);
//Defining what kind of object this is in the constructor
This = this;
//trace("this is :"+This);
}
public function onFrame(event:Event){
//trace("Ozzy track" +track_ozzy);
//trace("Albert track" +track_albert);
//All of this is getting an angle and getting the grunt to move along it while adding drag then acceleration
var radians:Number = grunt_angle * Math.PI/2;
var vx:Number = Math.cos(grunt_angle) * grunt_speed;
var vy:Number = Math.sin(grunt_angle) * grunt_speed;
var ax:Number = Math.cos(grunt_angle) * force;
var ay:Number = Math.cos(grunt_angle) * force;
//Calculate that goddamn distance every frame because it needs to stay updated
var dxA:Number = This.parent.albert_container.x - this.x;
var dyA:Number = This.parent.albert_container.y - this.y;
var dxO:Number = This.parent.ozzy_container.x - this.x;
var dyO:Number = This.parent.ozzy_container.y - this.y;
//trace("This is ozzy distance: " +distance_enemyandozzy);
distance_enemyandozzy = Math.sqrt(dxA*dxA + dyA*dyA);
distance_enemyandalbert = Math.sqrt(dxO*dxO + dyO*dyO);
//This is to cap the grunt speed so it doesn't accelerate past 7
if(grunt_speed >= 4){
grunt_speed = 4;
}else{
grunt_speed += grunt_accel
}
ax *= grunt_drag;
ay *= grunt_drag;
vx += ax;
vy += ay;
//Here is where we give the grunt vision
if(distance_enemyandozzy <= 100 && track_ozzy){
var ozzy_angle:Number = Math.atan2(dyA, dxA);
var new_vxO:Number = Math.cos(ozzy_angle) * grunt_speed;
var new_vyO:Number = Math.sin(ozzy_angle) * grunt_speed;
//Adding acceleration to the new variables
new_vxO += ax
new_vyO += ay
this.x += new_vxO;
this.y += new_vyO;
}else{
this.x += vx;
this.y += vy;
}
if(distance_enemyandalbert <= 100 && track_albert){
var albert_angle:Number = Math.atan2(dyA, dxA);
var new_vxA:Number = Math.cos(albert_angle) * grunt_speed;
var new_vyA:Number = Math.sin(albert_angle) * grunt_speed;
//Adding acceleration to the new variables
new_vxA += ax
new_vyA += ay
this.x += new_vxA;
this.y += new_vyA;
}else{
this.x += vx;
this.y += vy;
}
//To slow them down if the player is chosing to move
/* if(slow == true){
grunt_speed = grunt_speed/2;
}*/
//These 2 ifs are just to keep it from leaving the play area till I can replace them with hit test objects
if(This.x >= stage.stageWidth || This.x <= 0){
grunt_angle += 180;
ax *= -1
health += -1
grunt_speed = 0;
}
if(This.y >= stage.stageHeight || This.y <= 0){
grunt_angle += 180;
grunt_speed = 0;
ay *= -1
health += -1
}
//Hit testing for Albert and Ozzy
if (This.hitTestObject(This.parent.albert_container.h itbox_albert)){
grunt_angle += 180;
ax *= -1
grunt_speed = 1;
//This starts the timer to get back to true so the enemies track after a few seconds
track_albert = false;
run_away_timer_albert.start();
}
if (This.hitTestObject(This.parent.ozzy_container.hit box_ozzy)){
grunt_angle += 180;
ax *= -1
grunt_speed = 1;
track_ozzy = false;
run_away_timer_ozzy.start();
}
//hitboxes for the stage walls, just reverses direction and angle
if (This.hitTestObject(This.parent.crowd_left.hitbox) ){
grunt_angle += 180;
ax *= -1
}
if (This.hitTestObject(This.parent.crowd_right.hitbox )){
grunt_angle += 180;
ax *= -1
}
if (This.hitTestObject(This.parent.crowd_top.hitbox)) {
grunt_angle += 180;
ax *= -1
}
if (This.hitTestObject(This.parent.crowd_bottom.hitbo x)){
grunt_angle += 180;
ax *= -1
}
}
function running_away_ozzy(e:TimerEvent):void{
trace("AAAAH WE'RE COMING BACK FOR +OZZY+!!");
track_ozzy = true;
}
function running_away_albert(e:TimerEvent):void{
trace("AAAAH WE'RE COMING BACK FOR ~ALBERT~!!");
track_albert = true;
}
//This gets called and returns the boolean hitting if it collides with an object. It gets called constantly in onFrame
//All of this down here and you see commented out is where I tried to slow it down, it gives the same non-null command as the hit test
//It does slow things down when used but it just keeps giving a null error and I can't understand why.
/* public function slow_down(event:MouseEvent){
slow = true;
}
public function normal_speed(event:MouseEvent){
slow = false;
}*/
}
}
What am I doing wrong?
package{
//This is always used as the stage is like one big movie clip
import flash.display.MovieClip;
//Click on the event in the addeventlistener to find what you have to import and now ou can pull everything from that class into this script. It brings in the preexisting class.s
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
//We're extending the movieclip class because we're adding functionality to it
public class Grunt extends MovieClip{
//This is the variable that I'd turn on when the grunt gets hit so it can knock into other enemies and damage them
public var grunt_hitbyplayer:Boolean = false;
public var grunt_speed:Number = 0.5;
public var grunt_angle:Number = Math.random()*360;
public var grunt_drag:Number = .90;
public var grunt_accel:Number = .2;
public var force:Number = .5;
public var health:Number = 5;
public var slow:Boolean = false;
//This is where I was trying to solve the problem of this by making it a new object. It doesn't like it.
public var This:Object = new Object();
public var hitting:Boolean = false;
//Let's calculate some mother****ing distance between the enemy and albert and ozzy
public var distance_enemyandozzy:Number;
public var distance_enemyandalbert:Number;
//These are to turn on and off if it's tracking the player's characters
public var track_albert:Boolean = true;
public var track_ozzy:Boolean = true;
var run_away_timer_ozzy:Timer = new Timer(5000, 0);
var run_away_timer_albert:Timer = new Timer(5000, 0);
//This is the constructor function with the same name as the class, it gets executed first as soon as the class is called
public function Grunt(){
//This will give everything in the class an event listener
this.addEventListener(Event.ENTER_FRAME, onFrame);
run_away_timer_ozzy.addEventListener(TimerEvent.TI MER, running_away_ozzy);
run_away_timer_albert.addEventListener(TimerEvent. TIMER, running_away_albert);
//These are for the slow command, it keeps returning a null object so they're commented out. I have no idea why it does this.
//This.parent.stage.addEventListener(MouseEvent.MOUS E_DOWN, slow_down);
//This.parent.stage.addEventListener(MouseEvent.MOUS E_UP, normal_speed);
//Defining what kind of object this is in the constructor
This = this;
//trace("this is :"+This);
}
public function onFrame(event:Event){
//trace("Ozzy track" +track_ozzy);
//trace("Albert track" +track_albert);
//All of this is getting an angle and getting the grunt to move along it while adding drag then acceleration
var radians:Number = grunt_angle * Math.PI/2;
var vx:Number = Math.cos(grunt_angle) * grunt_speed;
var vy:Number = Math.sin(grunt_angle) * grunt_speed;
var ax:Number = Math.cos(grunt_angle) * force;
var ay:Number = Math.cos(grunt_angle) * force;
//Calculate that goddamn distance every frame because it needs to stay updated
var dxA:Number = This.parent.albert_container.x - this.x;
var dyA:Number = This.parent.albert_container.y - this.y;
var dxO:Number = This.parent.ozzy_container.x - this.x;
var dyO:Number = This.parent.ozzy_container.y - this.y;
//trace("This is ozzy distance: " +distance_enemyandozzy);
distance_enemyandozzy = Math.sqrt(dxA*dxA + dyA*dyA);
distance_enemyandalbert = Math.sqrt(dxO*dxO + dyO*dyO);
//This is to cap the grunt speed so it doesn't accelerate past 7
if(grunt_speed >= 4){
grunt_speed = 4;
}else{
grunt_speed += grunt_accel
}
ax *= grunt_drag;
ay *= grunt_drag;
vx += ax;
vy += ay;
//Here is where we give the grunt vision
if(distance_enemyandozzy <= 100 && track_ozzy){
var ozzy_angle:Number = Math.atan2(dyA, dxA);
var new_vxO:Number = Math.cos(ozzy_angle) * grunt_speed;
var new_vyO:Number = Math.sin(ozzy_angle) * grunt_speed;
//Adding acceleration to the new variables
new_vxO += ax
new_vyO += ay
this.x += new_vxO;
this.y += new_vyO;
}else{
this.x += vx;
this.y += vy;
}
if(distance_enemyandalbert <= 100 && track_albert){
var albert_angle:Number = Math.atan2(dyA, dxA);
var new_vxA:Number = Math.cos(albert_angle) * grunt_speed;
var new_vyA:Number = Math.sin(albert_angle) * grunt_speed;
//Adding acceleration to the new variables
new_vxA += ax
new_vyA += ay
this.x += new_vxA;
this.y += new_vyA;
}else{
this.x += vx;
this.y += vy;
}
//To slow them down if the player is chosing to move
/* if(slow == true){
grunt_speed = grunt_speed/2;
}*/
//These 2 ifs are just to keep it from leaving the play area till I can replace them with hit test objects
if(This.x >= stage.stageWidth || This.x <= 0){
grunt_angle += 180;
ax *= -1
health += -1
grunt_speed = 0;
}
if(This.y >= stage.stageHeight || This.y <= 0){
grunt_angle += 180;
grunt_speed = 0;
ay *= -1
health += -1
}
//Hit testing for Albert and Ozzy
if (This.hitTestObject(This.parent.albert_container.h itbox_albert)){
grunt_angle += 180;
ax *= -1
grunt_speed = 1;
//This starts the timer to get back to true so the enemies track after a few seconds
track_albert = false;
run_away_timer_albert.start();
}
if (This.hitTestObject(This.parent.ozzy_container.hit box_ozzy)){
grunt_angle += 180;
ax *= -1
grunt_speed = 1;
track_ozzy = false;
run_away_timer_ozzy.start();
}
//hitboxes for the stage walls, just reverses direction and angle
if (This.hitTestObject(This.parent.crowd_left.hitbox) ){
grunt_angle += 180;
ax *= -1
}
if (This.hitTestObject(This.parent.crowd_right.hitbox )){
grunt_angle += 180;
ax *= -1
}
if (This.hitTestObject(This.parent.crowd_top.hitbox)) {
grunt_angle += 180;
ax *= -1
}
if (This.hitTestObject(This.parent.crowd_bottom.hitbo x)){
grunt_angle += 180;
ax *= -1
}
}
function running_away_ozzy(e:TimerEvent):void{
trace("AAAAH WE'RE COMING BACK FOR +OZZY+!!");
track_ozzy = true;
}
function running_away_albert(e:TimerEvent):void{
trace("AAAAH WE'RE COMING BACK FOR ~ALBERT~!!");
track_albert = true;
}
//This gets called and returns the boolean hitting if it collides with an object. It gets called constantly in onFrame
//All of this down here and you see commented out is where I tried to slow it down, it gives the same non-null command as the hit test
//It does slow things down when used but it just keeps giving a null error and I can't understand why.
/* public function slow_down(event:MouseEvent){
slow = true;
}
public function normal_speed(event:MouseEvent){
slow = false;
}*/
}
}