2
votes

I'm trying to call a class to my main flash file in as3.

my class:

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class Snow extends MovieClip
    {
        private var flakesVector:Vector.<MovieClip> = new Vector.<MovieClip>();
        private var timer:Timer = new Timer(2000);

        public function Snow(speed:int = 3, flakesNumber = 100 ):void
        {
            for(var i:int = 0; i < flakesNumber; i++)
            {
                var flake:Snowflake = new Snowflake();

                flake.vel = (Math.random() * speed) + 0.5;
                flake.xSpeed = Math.floor(Math.random() * (0.5 - -0.5 + 1)) + -0.5;

                flake.scaleX = (Math.random() * 1) + 0.3;
                flake.scaleY = flake.scaleX;
                flake.x = Math.random() * stage.stageWidth;
                flake.y = Math.random() * stage.stageHeight;

                addChild(flake);

                flakesVector.push(flake);
            }

            addEventListener(Event.ENTER_FRAME, fall);
            timer.addEventListener(TimerEvent.TIMER, changeMovement);
            timer.start();
        }

        private function fall(e:Event):void
        {
            for(var i:int = 0; i < flakesVector.length; i++)
            {
                flakesVector[i].x += flakesVector[i].xSpeed;
                flakesVector[i].y += flakesVector[i].vel;

                if(flakesVector[i].y > stage.stageHeight)
                {
                    flakesVector[i].x = Math.random() * stage.stageWidth;
                    flakesVector[i].y = -flakesVector[i].height;
                }
            }
        }

        private function changeMovement(e:TimerEvent):void
        {
            for(var i:int = 0; i < flakesVector.length; i++)
            {
                flakesVector[i].xSpeed *= -1;
            }
        }

    }
}   

And in my main fla i try to call it like this:

import classes.Snow;

var Snowy:Snow = new Snow();
addChild(Snowy);

But when i try to do it if i dont give any instance name to my snowflake movieclip i get "Cannot acess a property or method of a null object reference" and if i name my instance snowflake i get the error "type was not found or was not a compile-time constant: Snowflake"

What am I doing wrong?

Thank you.

EDIT:

but if i call the class document as Snow I get a lot of errors: any suggestion?

enter image description here

2
yes I already did it but I still get stuck in the same place with the error "type was not found or was not compile time constant:Snowflake" maybe it doesn't find the movieclip in my library but I have him there with the name "Snowflake" and export to ActionScript with the class name "Snowflake".user3727136
your class has no package scope defined (it's only 'package') but yet you import it with a package scope: "classes". That already cannot work.BotMaster
yes I know I can do it like that but I want to make the class being called by a button, and like that the class is always active. Basically I want to turn on and off the snow, is it possible in this way? @esdebonuser3727136

2 Answers

1
votes

Where is your class?? is next to your fla file??

then only use:

var Snowy:Snow = new Snow();
addChild(Snowy); 

Or in your properties panel

in class type: Snow

enter image description here

EDIT: Your code work fine

Working

0
votes
// import classes.Snow; // <---- REMOVE THIS

var Snowy:Snow;
var snowSW:Boolean=false;

button.addEventListener(MouesEvent.CLICK, switchSnow);

function switchSnow(e:MouseEvent):void{
    if(!snowSW){
       Snowy= new Snow();
       addChild(Snowy);
     }else{
       removeChild(Snowy);
       Snowy= null;
    }
}