Copy link to clipboard
Copied
So i am attempting to make a variable in my main code file, for a character. The problem is that I have a code file for the character, so it gives me an error whenever i attempt to make a variable.
Code to make the variable:
public var Char:character = new character();
Error I am receiving:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at character()
at main()
I figured it out, thank you very much. I was accidentally adding my loop twice in my character file.
Copy link to clipboard
Copied
In Publish Settings you can check the box that says Permit Debugging, that would then give you the line number where the problem occurred.
By convention, Class files start with a capital letter. It would make more sense if your Class was Character, and the line would read:
public var char:Character = new Character();
But that wouldn't explain the error. The error message looks like the problem may be inside the character class. What does that code look like?
Copy link to clipboard
Copied
The entire code file:
package
{
import flash.events.*;
import flash.display.Stage;
import flash.display.MovieClip;
public class character extends MovieClip
{
// global variables go here
private var _root;
private var stepDown:Number = 1;
private var stepUp:Number = 1;
private var stepLeft:Number = 1;
private var stepRight:Number = 1;
private var addyStepDown:Number = 1;
private var addyStepUp:Number = 1;
private var addyStepLeft:Number = 1;
private var addyStepRight:Number = 1;
private var keyCode:Number;
private var speed:Number = 6;
private var damage:Number = 4;
private var wisdom:Number = 1;
public function character()
{
//program initialization code goes here
addEventListener(Event.ADDED, link);
stepDown = 0;
stepUp = 0;
stepLeft = 0;
stepRight = 0;
addyStepDown = 0;
addyStepUp = 0;
addyStepLeft = 0;
addyStepRight = 0;
gotoAndStop(1);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
private function link(e:Event)
{
addEventListener(Event.ENTER_FRAME, loop);
_root = (root as MovieClip);
_root.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
_root.stage.addEventListener(KeyboardEvent.KEY_UP, keyLetGo);
}
private function keyLetGo(e:KeyboardEvent)
{
if (keyCode == 40)
{
stepDown = 1;
}
if (keyCode == 38)
{
stepUp = 1;
}
if (keyCode == 37)
{
stepLeft = 1;
}
if (keyCode == 39)
{
stepRight = 1;
}
}
private function keyPressed(e:KeyboardEvent)
{
keyCode = e.keyCode;
if (keyCode==40 /*DOWN*/)
{
y += speed;
stepDown += 1;
stepUp = 0;
stepLeft = 0;
stepRight = 0;
if (stepDown == 2)
{
gotoAndStop(2);
}
if (stepDown == 3)
{
gotoAndStop(3);
}
if (stepDown == 4)
{
gotoAndStop(1);
stepDown = 1;
}
}
if (keyCode==38 /*UP*/)
{
gotoAndStop(4);
y -= speed;
stepUp += 1;
stepDown = 0;
stepLeft = 0;
stepRight = 0;
if (stepUp == 2)
{
this.gotoAndStop(5);
}
if (stepUp == 3)
{
gotoAndStop(6);
}
if (stepUp == 4)
{
gotoAndStop(4);
stepUp = 1;
}
}
if (keyCode==37 /*LEFT*/)
{
stepLeft += 1;
stepDown = 0;
stepUp = 0;
stepRight = 0;
if (stepLeft == 2)
{
this.gotoAndStop(8);
}
if (stepLeft == 3)
{
gotoAndStop(9);
}
if (stepLeft == 4)
{
gotoAndStop(7);
stepLeft = 1;
}
x -= speed;
}
if (keyCode==39 /*RIGHT*/)
{
x += speed;
stepRight += 1;
stepDown = 0;
stepLeft = 0;
stepUp = 0;
if (stepRight == 2)
{
this.gotoAndStop(11);
}
if (stepRight == 3)
{
gotoAndStop(12);
}
if (stepRight == 4)
{
gotoAndStop(10);
stepRight = 1;
}
}
if (keyCode == 16 && hitTestObject(_root.addy)==true)
{
_root.addyTalkBox.textBox.text = "Hello Vyvan";
_root.addyTalkBox.nameBox.text = "Addy";
stage.addChild(_root.addyTalkBox);
_root.addyTalkBox.x = 275;
_root.addyTalkBox.y = 350;
_root.addyTalkBox.addyOpt.visible = false;
}
}
private function loop(e:Event)
{
//code that needs to update constantly goes here
if (_root.compAddy == true)
{
if (this.y + 50 < _root.addy.y)
{
_root.addy.y -= _root.addySPD;
addyStepUp += 1;
addyStepDown = 0;
addyStepLeft = 0;
addyStepRight = 0;
if (addyStepUp == 2)
{
_root.addy.gotoAndStop(5);
}
if (addyStepUp == 3)
{
_root.addy.gotoAndStop(6);
}
if (addyStepUp == 4)
{
_root.addy.gotoAndStop(4);
addyStepUp = 1;
}
}
if (this.y - 50 > _root.addy.y)
{
_root.addy.y += _root.addySPD;
addyStepDown += 1;
addyStepUp = 0;
addyStepLeft = 0;
addyStepRight = 0;
if (addyStepDown == 2)
{
_root.addy.gotoAndStop(2);
}
if (addyStepDown == 3)
{
_root.addy.gotoAndStop(3);
}
if (addyStepDown == 4)
{
_root.addy.gotoAndStop(1);
addyStepDown = 1;
}
}
if (this.x + 50 < _root.addy.x)
{
_root.addy.x -= _root.addySPD;
addyStepLeft += 1;
addyStepDown = 0;
addyStepUp = 0;
addyStepRight = 0;
if (addyStepLeft == 2)
{
_root.addy.gotoAndStop(8);
}
if (addyStepLeft == 3)
{
_root.addy.gotoAndStop(9);
}
if (addyStepLeft == 4)
{
_root.addy.gotoAndStop(7);
addyStepLeft = 1;
}
}
if (this.x - 50 > _root.addy.x)
{
_root.addy.x += _root.addySPD;
addyStepRight += 1;
addyStepDown = 0;
addyStepLeft = 0;
addyStepUp = 0;
if (addyStepRight == 2)
{
_root.addy.gotoAndStop(11);
}
if (addyStepRight == 3)
{
_root.addy.gotoAndStop(12);
}
if (addyStepRight == 4)
{
_root.addy.gotoAndStop(10);
addyStepRight = 1;
}
}
/*if (this.y > _root.Char.y)
{
this.y -= speed;
}
if (this.x < _root.Char.x)
{
this.x += speed;
this.scaleX =1;
}
if (this.x > _root.Char.x)
{
this.x -= speed;
this.scaleX =-1
}*/
}
if (stepDown == 1)
{
gotoAndStop(1);
}
if (stepUp == 1)
{
gotoAndStop(4);
}
if (stepLeft == 1)
{
gotoAndStop(7);
}
if (stepRight == 1)
{
gotoAndStop(10);
}
}
}
}
Copy link to clipboard
Copied
root might be stage, which wouldn't be movieclip. Could you work this way?:
public var Char:character = new character(this);
and:
public function character(mc:*)
{
_root = mc as MovieClip;
Copy link to clipboard
Copied
Now the error is
at Character()
at main()
In my character file, line 37 is where i add the event listener for my loop. In my main file, line 10 is where i add the variable.
Copy link to clipboard
Copied
If it's still a problem can you post a zip online of the files, for me to try?
Copy link to clipboard
Copied
I figured it out, thank you very much. I was accidentally adding my loop twice in my character file.
Copy link to clipboard
Copied
Good to hear, thanks for reporting back.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now