A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [F8] oop basics

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Posts
    27

    [F8] oop basics

    I'm good at actionscript and am now trying to learn oop.

    I'm having trouble duplicating an object on the stage as an object with class attached. I want to avoid exporting library items on first frame.

    If I have an instance of "ball" on the stage named "ball_x" and a class named "Ball", how would I get the class to create a duplicate of "ball_x" with the class attached?

    Thanks!

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Use the 'new' command to build a second instance of the class, anything in the MC it's associate with at compile-time will come along for the ride.

    var secondBall:Ball = new Ball();
    addChild(secondBall);

  3. #3
    Junior Member
    Join Date
    Jan 2008
    Posts
    27
    Thanks for replying but I need the code for the class that creates the duplicate movie clip. Below is the code that doesn't work but is what I'm looking for. Also, I'm using actionscript 2.

    Code:
    import Ball;
    var ball_1:Ball = new Ball("ball_1",100,300);
    ball_1.moveBall();
    Code:
    //  Ball.as
    class Ball extends MovieClip{
    	//
    	function Ball(passName,passX,passY) {
    		this = _root.ball_x.duplicateMovieClip(passName,100);
    		this._x = passX;
    		this._y = passY;
      		};//
    	//
    	function moveBall() {
    		this._x = 10;
      		};//
    	//
    	};//  class
    Thanks!

  4. #4
    Senior Member
    Join Date
    Jan 2008
    Location
    Montreal
    Posts
    101
    Here's the code you're looking for:

    Timeline Code
    Code:
    import Ball;
    var ball_1:Ball = new Ball(_root, "ball_1",100,300);
    ball_1.moveBall();
    Class Code
    Code:
    //  Ball.as
    class Ball extends MovieClip {
    	// Member Variable
    	private var m_originalMovie:MovieClip = null;
    	private var m_duplicateMovie:MovieClip = null;
    	
    	function Ball(parentClip:MovieClip, passName:String, passX:Number, passY:Number) {
    		// You absolutely need to attach the movieclip to a parent movieclip.
    		// You cannot assign anything to the this pointer. This represent the current object. You need to provide another
    		// variable to hold the reference to your duplicated clip.
    		// If you don't want to create the original clip here, you need to take the same line and put it in your timeline.
    		m_originalMovie = parentClip.attachMovie(passName, passName, _root.getNextHighestDepth(), {_x:passX, _y:passY});
    		// Now you duplicate the movieclip. Make sure you put it on proper depth to not override the original clip
    		m_duplicateMovie = m_originalMovie.duplicateMovieClip(passName + "_duplicate", parentClip.getNextHighestDepth());
    		// You can move the clip here like you did but you could also have provided the following object to the duplicateMovieClip method: {_x:passX, _y:passY}
    		m_duplicateMovie._x = passX;
    		m_duplicateMovie._y = passY;
    	}//
    	//
    	function moveBall() {
    		m_duplicateMovie._x = 100;
    		m_duplicateMovie._y = 100;
    	}
    }//  class
    Look at it and experiment with it. However, for your first tries, make sure that "Export to first frame" is actually ckecked in otherwise you'll see nothing. If you do not want to export on first frame, you'll have to drag an instance of the "ball_1" clip on the timeline in another frame before using it. Also, by default, the classes are exported to the first frame. You can change this in the "Publish Settings" in the Flash tab. Just click on the "Settings..." button and change the frame on which the classes are exported.

    Good Luck!

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