user1637318
user1637318

Reputation: 11

AS3 - "Call to a possibly undefined method"

I'm very new to ActionScript 3 but I've been gradually building a small "Tanks"-like shooting game and now I'm at the point of adding my bullet shooting, to do this i've made a new AS3 class and imported it etc, however when I run the game I get the error:

Call to a possibly undefined method turretShot

please help me in finding out why this happens. This error shows twice in my compiler errors and the lines it's for are very similar, here is the parts of code affected:

shootTurret.as (Class file)

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

    public class turretShot extends MovieClip
    {
        var enemy_mc:MovieClip;
        public function turretShot(enemy_mc:MovieClip)
        {
            var xSpeed:Number;
            var ySpeed:Number;
            var angle:Number;
            public var shotSpeed:Number;
            speed = 30;
            var gravity:Number = 1;
            var thisShot:MovieClip;
            var platform1_mc:MovieClip;
            var platform2_mc:MovieClip;
            var thisParent: *;
            enemy_mc = enemy;
            this.addEventListener(Event.ADDED, initialize);
        }
        function initialize(event:Event):void
        {
            this.addEventListener(Event.ENTER_FRAME, moveShot);
            thisParent = event.currentTarget.parent;
            platform1_mc = thisParent.platform1_mc;
            platform2_mc = thisParent.platform2_mc;
            thisShot = MovieClip(this.parent.getChildByName(this.name));
            angle = this.rotation / 180 * Math.PI;
            xSpeed = Math.cos(angle) * speed;
            ySpeed = Math.sin(angle) * speed;
        }   
        function moveShot(event:Event):void
    }
}

I've imported this in my game .FLA actionscript on the necessary frame with:

import resources.*;

as my class file is in the "resources" folder in the game's directory.

Now, the two sections of code that are throwing these errors... not sure if it's anything to do with them or just the class, but they're:

(4th line)

var shot_mc:MovieClip;
if (keyPressed == Keyboard.SPACE && spaceKeyDown == false)
{
    shot_mc = new turretShot(enemy_mc);
    shot_mc.x = user_mc.x + user_mc.turret_mc.x;
    shot_mc.y = user_mc.y + user_mc.turret_mc.y;
    shot_mc.rotation = user_mc.turret_mc.rotation;
    addChild(shot_mc);
    spaceKeyDown = true;
}

and (first line):

var shot_mc:MovieClip = new turretShot(user_mc);
shot_mc.x = enemy_mc.x + enemy_mc.enemyTurret_mc.x;
shot_mc.y = enemy_mc.y + enemy_mc.enemyTurret_mc.y;
shot_mc.rotation = enemyTargetRotation;
shot_mc.speed = Math.random() * 15 + 15;
addChild(shot_mc);

Any help would be appreciated.

Upvotes: 0

Views: 7187

Answers (3)

danii
danii

Reputation: 5703

I corrected the syntax for this class so you can see what I meant in the comments section for the other question. I gave all the variables public scope and the methods private scope but feel free to change this to suit your needs.

You should note that the code you posted has many syntax mistakes and that you should feel confident about ActionScript3 and OOP before trying to code a full game. Please study this tutorial (or any other) on AS3 and classes: kirupa tutorial on Classes in ActionScript 3

On a side note, class names are usually written in UpperCamelCase. This is a convention followed by most ActionScript3 (and Java, etc) programmers and if you stick to it your code will be more readable and thus it should be easier to help you next time ;)

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

    public class TurretShot extends MovieClip
    {
        public var enemy_mc:MovieClip;
        public var xSpeed:Number;
        public var ySpeed:Number;
        public var angle:Number;
        public var shotSpeed:Number;
        public var speed:Number = 30;
        public var gravity:Number = 1;
        public var thisShot:MovieClip;
        public var platform1_mc:MovieClip;
        public var platform2_mc:MovieClip;
        public var thisParent: *;

        public function TurretShot(enemyParam:MovieClip)
        {

            enemy_mc = enemyParam;
            this.addEventListener(Event.ADDED, initialize);
        }

        private function initialize(event:Event):void
        {
            this.addEventListener(Event.ENTER_FRAME, moveShot);
            thisParent = event.currentTarget.parent;
            platform1_mc = thisParent.platform1_mc;
            platform2_mc = thisParent.platform2_mc;
            thisShot = MovieClip(this.parent.getChildByName(this.name));
            angle = this.rotation / 180 * Math.PI;
            xSpeed = Math.cos(angle) * speed;
            ySpeed = Math.sin(angle) * speed;
        }

        private function moveShot(event:Event):void
        {
           //do stuff every frame
           trace("enter frame");
        }


    }
}

Hope this helps!

Upvotes: 0

The_asMan
The_asMan

Reputation: 6403

Like Dave Hart stated you have a lot of errors however the specific error you are asking about is because you did not import the class into your FLA.

import resources.turretShot;

Upvotes: 0

Dave Hart
Dave Hart

Reputation: 347

There are several errors in your turretShot class, which the compiler probably also reports. Fix them and this message should go away.

The variables that you define inside the constructor should be defined outside of it, along with the var enemy_mc:MovieClip;. It's also a good reflex to make them public, private, or protecteddepending on your code logic.

I also see that the line enemy_mc = enemy; references an enemy variable that doesn't exist. You probably meant your constructor to be public function turretShot(enemy:MovieClip).

There may be other errors in the code I didn't pick up.

Upvotes: 1

Related Questions