as3 1119: Access of possibly undefined property getters/setters

409 views Asked by At

It would be amazing if someone could expand on the current answer, Thanks.

full error

Line 22 1119: Access of possibly undefined property CharacterX through a reference with static type flash.display:DisplayObject.

I'm trying to set a variable for the object shark, that is already defined in the object character First time using setters in flash, so I might not be doing this right.

code I'm using to set the variable I tried to comment out the stuff I thought was irrelevant to this issue, not actually commented out in real code.

var character:Character; 
//var bullet:Bullet=null;
//var bullets:Array = new Array();
//var enemies:Array = new Array();
//character=new Character(bullets);
addChild(character);
var shark:Shark=new Shark();
addChild(shark);
//var enemy:Enemy=null;
////var i:int;
//for (i=0; i<10; i++) {

//enemy = new Enemy(Math.random()*stage.stageWidth, Math.random()*stage.stageHeight);
//addChild(enemy);
//  enemies.push(enemy);
//}
//stage.addEventListener(Event.ENTER_FRAME, colTest);
//function colTest(e:Event ):void {
//  if(character.hitTestObject(turtle)){
//     character.gotoAndStop("Turtle");
//     }



//}

shark.setT(character.x, character.y)

class in which I'm attempting to define a variable using the function above.

package 
{
    import flash.display.*;
    import flash.events.*;

    public class Shark extends MovieClip
    {
            var CharacterX:Number = 0;
            var CharacterY:Number = 0;
        public function Shark()
        {

            this.x = 300;
            this.y = 200;
            addEventListener(Event.ENTER_FRAME,playGame);
        }
        public function setT(characterx:Number,charactery:Number){
            CharacterX = characterx - this.x;
            CharacterY = charactery - this.y;
        }

        function playGame(event:Event):void
        {
            var ease:int = 20;
            var speed:int = 10;
            var targetX:int = root.CharacterX - this.x;
            var targetY:int = root.CharacterY - this.y;
            var rotation = Math.atan2(targetY,targetX) * 180 / Math.PI;

cut code off here, didn't want to make a code dump can get you anything that might be relevant just ask.

Here is a pastebin of all of the code if it might help,

Shark class:

Actions on Frame 1:

Character class

1

There are 1 answers

2
Scab On

Let me start by saying I can't spot the exact problem here, but I have some ideas. Your error 1999 says that something of the type display object is trying to change your variable. This happens a lot when you use parent.myMethod() because parent is typed as display object. You would fix this by typecasting like (parent as MovieClip).myMethod

In your case I don't see the exact source of this problem. But you could try using this.characterX in your setT function