A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: importing animation controller

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5

    importing animation controller

    Hello friends! I unfortunately need help once again ...

    Ok so, the project is to display, and animate a sprite using imbeds instead of importing them into a .fla library.

    here is my test sprite sheet:


    I use an assets.as to import the sprite sheets this file is located in /assetsTest folder:
    Actionscript Code:
    package assetsTest  {
        import flash.display.*;
        public class assets {
            [Embed(source='myImage.png')]
            public static var myImage:Class;
        }
       
    }

    now that we have the image cached, i send it over to my animation.as script (located in same folder as above):

    Actionscript Code:
    package assetsTest
    {

        import flash.events.*;
        import flash.geom.*;
        import flash.display.*;
        import assetsTest.assets;

        public class animation extends MovieClip
        {
            var bowserTilesHeight:int;
            var bowserTilesWidth:int;
            var bowserTilesLength:int;
            var animationIndex:int;
            var animationCount:int;
            var animationDelay:int;
            var bowserX:int;
            var bowserY:int;
            var bowserRect:Rectangle;
            var bowserPoint:Point;

            var canvasBD:BitmapData;
            var canvasBitmap:Bitmap;

            var tileSheet:Bitmap;
            var tileSheetData:BitmapData;

            var backGroundBitmapData:BitmapData;

            public function animation()
            {
                bowserTilesHeight = 91;
                bowserTilesWidth = 66;
                bowserTilesLength = 16;
                animationIndex = 0;
                animationCount = 0;
                animationDelay = 1;

                bowserX = (stage.stageWidth / 2)-(bowserTilesWidth / 2);
                bowserY = (stage.stageHeight / 2)-(bowserTilesHeight / 2);

                canvasBD = new BitmapData(bowserTilesWidth,bowserTilesHeight,true,0);
                canvasBitmap = new Bitmap(canvasBD);

                backGroundBitmapData = new BitmapData(bowserTilesWidth,bowserTilesHeight,true,0);

                tileSheet = new assets.myImage();
                if (tileSheet == null) {
                    // catch error
                    trace("Bowser Sprite Sheet Missing... code: Ani_49");
                    return;
                }
                tileSheetData = tileSheet.bitmapData.clone();

                bowserRect = new Rectangle(0,0,bowserTilesWidth,bowserTilesHeight);
                bowserPoint = new Point(0,0);

                addChild(canvasBitmap);

                canvasBitmap.x = bowserX;
                canvasBitmap.y = bowserY;

                addEventListener(Event.ENTER_FRAME,aniLoop);
            }
            private function aniLoop(e:Event)
            {
                clearFrame();
            }
            private function clearFrame():void
            {
                canvasBD.lock();

                canvasBD.copyPixels(backGroundBitmapData, backGroundBitmapData.rect, bowserPoint);

                drawBowser();
            }
            private function drawBowser():void
            {
                if (animationCount == animationDelay)
                {
                    animationIndex++;
                    animationCount = 0;
                    if (animationIndex == bowserTilesLength)
                    {
                        animationIndex = 0;
                    }
                }
                else
                {
                    animationCount++;
                }
                bowserRect.x = int((animationIndex % bowserTilesLength)) * bowserTilesWidth;
                bowserRect.y = int((animationIndex / bowserTilesLength)) * bowserTilesHeight;
                canvasBD.copyPixels(tileSheetData,bowserRect, bowserPoint);
                canvasBD.unlock();
            }

        }

    }

    now to my question, how can i make it so i can use a Main.as to run this animation.as file?

    every time i try it says undefined method animation through a reference with static type class.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Location
    Australia
    Posts
    21
    Okay, not sure how much help I'll be, but from what I can tell, you want to access animation.as from Main.as.

    What I'd do is import animation.as into Main.as, create a new instance of animation.as and then do what you want with it there. I don't know what's in Main.as so I can't quite figure out what the heck would be causing that wacky "undefined method animation" thing.

    How have you been trying to get animation.as inside of Main.as currently?

    Also, nice Bowser pinball sprite sheet, heh.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thank you for your reply!

    Main.as has nothing in it so far, right now it is still a blank class. im still learning a lot about writing in 100% actionscript.

    how would i make an instance of it and be able to make the animation.as work?

    ty again!

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Ok, so after running a debug i finally figured out what was causing the problem, but not exactly sure why... if someone would explain to me why this would be the issue and possibly a work around?

    the problem was in the animation.as defining the location of the redraw box. I am assuming referencing the stage is a no no?
    Actionscript Code:
    bowserX = (stage.stageWidth / 2)-(bowserTilesWidth / 2)
    bowserY = (stage.stageHeight / 2)-(bowserTilesHeight / 2);

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Location
    Australia
    Posts
    21
    Yep, it would probably be the stage thing. If you're importing animation.as into Main.as, you would need to pass down the stage from Main.as to animation.as. How you do this is easy, just make an argument and a variable to hold the stage reference.

    You would add a global variable for the class, then in the public function animation(), you would add an argument, like so:

    Code:
    private var stageRef:Stage; //This is the global stage reference variable
    
    public function animation(stageRef:Stage){
        this.stageRef = stageRef; //Sets the global stage reference variable (above) to the stage passed through as an argument
    }
    So now that you have a stage container, anywhere that you want to reference "stage" in animation.as gets replaced with "stageRef". E.g.

    Code:
    //bowserX = (stage.stageWidth / 2)-(bowserTilesWidth / 2); -- old code
    bowserX = (stageRef.stageWidth / 2)-(bowserTilesWidth / 2); //New code, with stageRef
    Now to pass the stage down from Main.as is simple. Import your animation class in Main.as and create a new instance of it:

    Code:
    import assetsTest.animation //I think this is the file path for animation.as, not sure, make sure to double check
    
    public function Main(){
        var anim:animation = new animation(stage); //Set up a new instance of animation.as, passing the stage along with it as a variable
        addChild(anim); //Add the object, as currently, in animation.as, addChild will be adding objects inside of animation.as, not to the stage. Adding our instance of animation.as will allow us to see everything (if we don't add it, we don't see anything)
    }
    I might be a bit wrong here, I'm not sure. But definitely with the stage reference, it's fine to do that, you just need to pass it through to children and stuff so that they're not referencing something that's non-existant.

    If it isn't working I'd be happy to have a look at the source files if you'd like and see what I can do.
    Last edited by Danny22; 04-15-2012 at 10:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center