Copy link to clipboard
Copied
Hey all, I am pretty new to Actionscript/programming so my code might just be a complete mess but I have been trying for hours to get this to work with no luck.
I am making a pretty basic game that needs to use a state machine. I am adding the character (Char) to the stage in my Main class. I also think I am connecting it with the state machine's main class which I have named Agent. I am getting a 1061 error with Char.setState(Agent.DEFENSE); on line 120 but when I comment it out everything works fine other than the character isn't using/seeing the state machine at all. The state machine is part of a template from school. I tried to clean it up here but may have missed some things so there might be a couple of things that don't necessarily apply, sorry. My Main class is below:
package {
import agent.Agent;
import classes.*;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
import flash.utils.Timer;
public class Main extends Sprite {
public var Char:char = new char(); //Instansiate character
public var setState:Agent = new Agent(Char);
public var agent:Agent = new Agent(Char); //Connects Char to Agent
public var Enemy:enemy = new enemy(); //Instansiate enemy knight
public var agents = new Vector.<Agent>();
public function Main(): void {
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveChar);
Char.addEventListener(Event.ENTER_FRAME, handleCollision);
}
//Initialize added to stage for both the main character and the enemies
private function addedToStageHandler(event: Event): void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
//addChar();
enemySpawn();
}
//Add main character to the stage
/*private function addChar(): void {
var Char:Agent = new Agent(Char);
addChild(Char);
agents.push(Char);
Char.x = 500
Char.y = 350;
trace("Made Reggie");
} */
/*private function addWalls(): void {
graphics.lineStyle(8, 0x000000, 100);
var wall_east = wall_east.graphics.lineTo(100, 300);
wall_east.graphics.moveTo(100, 100);
} */
//Set up main character movement
//FUTURE NOTE: Use Idle State for idle character
private function moveChar(event: KeyboardEvent): void {
if (event.keyCode == 40 || event.keyCode == 83) { //Down
Char.y += 10;
Char.gotoAndStop(5);
} else if (event.keyCode == 38 || event.keyCode == 87) { //Up
Char.y -= 10;
Char.gotoAndStop(4);
} else if (event.keyCode == 37 || event.keyCode == 65) { //Left
Char.x -= 10;
Char.gotoAndStop(2);
} else if (event.keyCode == 39 || event.keyCode == 68) { //Right
Char.x += 10;
Char.gotoAndStop(3);
} else {
Char.gotoAndStop(1); //Idle
}
}
//Spawn knight enemy
private function enemySpawn() {
trace("Adding Enemy");
stage.addChild(Enemy);
Enemy.x = 250;
Enemy.y = 300;
}
//Set up hit detection for walls so player can't leave designated area
private function handleCollision(e: Event): void {
//Handle character collision with walls
if (Char.hitTestObject(wall_north)) {
Char.y += 5;
trace("HIT");
} else if (Char.hitTestObject(wall_south)) {
Char.y -= 5;
trace("HIT");
} else if (Char.hitTestObject(wall_west)) {
Char.x += 5;
trace("HIT");
} else if (Char.hitTestObject(wall_east)) {
Char.x -= 5;
trace("HIT");
}
//Handle enemy collision with walls
if (Enemy.hitTestObject(wall_north)) {
Enemy.y += 5;
trace("HIT");
} else if (Enemy.hitTestObject(wall_south)) {
Enemy.y -= 5;
} else if (Enemy.hitTestObject(wall_west)) {
Enemy.x += 5;
} else if (Enemy.hitTestObject(wall_east)) {
Enemy.x -= 5;
}
}
private function charDefense(event:KeyboardEvent) {
var setState:Agent = new Agent(Char);
if (event.keyCode == 16) {
Char.setState(Agent.DEFENSE);
}
}
private function init(e: Event = null): void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// entry point
//graphics.beginFill(0xeeeeee);
//graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
//agents = new Vector.<Agent>();
//addEventListener(Event.ENTER_FRAME, gameloop);
for (var i:int = 0; i < 1; i++)
{
var Char:Agent = new Agent(Char);
A
var a: Agent = new Agent();
addChild(a);
agents.push(a);
a.x = mouseX;
a.y = mouseY;
} */
private function gameloop(e: Event): void {
for (var i: int = 0; i < agents.length; i++) {
agents.update();
}
}
trace("End Main");
}
}
My Agent class is below:
package agent
{
import agent.states.ChaseState;
//import agent.states.ConfusionState;
//import agent.states.FleeState;
import agent.states.IAgentState;
import agent.states.IdleState;
//import agent.states.WanderState;
import agent.states.CharDefState;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.events.KeyboardEvent;
import flash.events.Event;
import Main;
import classes.*;
public class Agent extends Sprite
{
public static const IDLE:IAgentState = new IdleState(); //Define possible states as static constants
//public static const WANDER:IAgentState = new WanderState();
public static const CHASE:IAgentState = new ChaseState();
//public static const FLEE:IAgentState = new FleeState();
//public static const CONFUSED:IAgentState = new ConfusionState();
public static const DEFENSE:IAgentState = new CharDefState();
private const RAD_DEG:Number = 180 / Math.PI;
private var _previousState:IAgentState; //The previous executing state
private var _currentState:IAgentState; //The currently executing state
private var _pointer:Shape;
private var _tf:TextField;
public var velocity:Point = new Point();
public var speed:Number = 0;
public var fleeRadius:Number = 50; //If the mouse is "seen" within this radius, we want to flee
public var chaseRadius:Number = 150; //If the mouse is "seen" within this radius, we want to chase
public var numCycles:int = 0; //Number of updates that have executed for the current state. Timing utility.
private var Char:char; //Instansiate character
public function Agent(p:char)
{
Char = p;
addChild(Char);
trace("Agent:added character");
_currentState = IDLE; //Set the initial state
}
public function update():void {
trace("Updating...");
if (!_currentState) return; //If there's no behavior, we do nothing
numCycles++;
_currentState.update(this);
x += velocity.x*speed;
y += velocity.y*speed;
if (x + velocity.x > stage.stageWidth || x + velocity.x < 0) {
x = Math.max(0, Math.min(stage.stageWidth, x));
velocity.x *= -1;
}
if (y + velocity.y > stage.stageHeight || y + velocity.y < 0) {
y = Math.max(0, Math.min(stage.stageHeight, y));
velocity.y *= -1;
}
_pointer.rotation = RAD_DEG * Math.atan2(velocity.y, velocity.x);
}
public function setState(newState:IAgentState):void {
if (_currentState == newState) return;
if (_currentState) {
_currentState.exit(this);
trace("setting state...");
}
//set another setState for character access
_previousState = _currentState;
_currentState = newState;
_currentState.enter(this);
numCycles = 0;
}
public function get previousState():IAgentState { return _previousState; }
public function get currentState():IAgentState { return _currentState; }
}
}
I am suspiscious of line 118. What is it trying to do - create a new instance of an Agent? Try commenting that out instead and see where the errors turn you. Also, try to keep track of using addChild... it seems to be happening a couple times at least for your Char.
Copy link to clipboard
Copied
I am suspiscious of line 118. What is it trying to do - create a new instance of an Agent? Try commenting that out instead and see where the errors turn you. Also, try to keep track of using addChild... it seems to be happening a couple times at least for your Char.
Copy link to clipboard
Copied
Thanks, I think it was from the multiple addChilds. Really not sure how that happened haha. Thanks!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now