A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Controlling Sprite with Classes

Hybrid View

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    25

    Controlling Sprite with Classes

    Ok the solution to this will probably be so simple, it'll hurt me!
    I used to know how to do this, but have since forgotten, and can't remember how, or find the resource I used to learn it in the first place.

    Basically, I want to move a sprite made in AS3, with an external class.

    This is the code I want to use as an example, just so I can get to grips with it, and apply it in other things.

    Library:
    MovieClip with AS name of "orb"

    Main File Timeline:

    Code:
    import flash.events.Event;
    
    addEventListener(Event.ENTER_FRAME, enteringFrame);
    
    function enteringFrame(e:Event):void{
    	var spr:orb = new orb();
    	spr.x = 175;
    	spr.y = 30;
    	spr.addEventListener(Event.ENTER_FRAME, animateOrb);
    	addChildAt(spr,0);
    }
    
    function animateOrb(e:Event):void{
    	e.target.animate();
    }
    animate.as
    Code:
    package  {
    	import flash.display.MovieClip;
    	
    	public class animate extends MovieClip{
    
    		public function animate() {
    			this.y += 10;
    		}
    	}
    }
    I seem to remember linking the as file to the movieclip somehow, but the error i get for this right now is:

    Code:
    TypeError: Error #1006: animate is not a function.
    	at animate/animateOrb()
    What do I need to make sure of to get this to work?

    Thanks for any help in advance!

  2. #2
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    ActionScript Code:
    package {
        import flash.display.MovieClip;

        public class animate extends MovieClip {
            public function animate() {
            }
            public function Ani(Y:int):int {
                //this.y += 10;
                return Y+1;
            }
        }
    }

    Actionscript Code:
    import flash.events.Event;

    addEventListener(Event.ENTER_FRAME, enteringFrame);

    function enteringFrame(e:Event):void {
        removeEventListener(Event.ENTER_FRAME, enteringFrame);
        var spr:orb = new orb();
        spr.x = 175;
        spr.y = 30;
        spr.addEventListener(Event.ENTER_FRAME, animateOrb);
        addChildAt(spr,0);
    }

    function animateOrb(e:Event):void {
        var a:animate=new animate();
        e.target.y=a.Ani(e.target.y);
    }

    This will work. But its not a good solution.



    arkitx
    Last edited by arkitx; 07-08-2011 at 06:22 PM.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You need your animate method to be a method of orb, since you are calling it on an orb instance. Alternatively, you could pass the orb to animate rather than calling it as a method of orb.

    I'd create an orb.as file, and copy/move the animate function into that. It does not need to be in animate.as, which I assume is your document class.

    By convention, class names should begin with an uppercase letter, and method/variable names should begin with a lowercase letter.

  4. #4
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Yes, you follow what 5TonsOfFlax suggested you to do if you don't have any other plan to make it in another separate class.



    arkitx

Tags for this Thread

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