r3dux.org

A number-pimping side project from the valleys in *NEW* upside-down flavour.

  • Home
  • ABOUT
  • OLD SITE
  • SEARCH
  • FEEDBACK

How To: Access properties of MovieClip/Sprite/DisplayObject instances from within a class in ActionScript 3

r3dux | May 5, 2011

When it comes to programming, the devil is always in the details. In this case, I just wanted to add a MovieClip to the stage in a flash file, and then access that MovieClip’s instance properties to control some other instance stuff from within a class. Sounds simple, right? But could I do it? Could I ****. If you’re reading this, then there’s a good chance you’re in the same boat. Luckily for us both, it’s not too difficult to do at all. Here’s some simple proof of concept code to help you along:

Note: This first example uses a class called Ball. If you want to see it in action either download my pre-made code (Adobe Flash CS4 format), or just draw a circle or something, select it, convert to symbol (shortcut key: F8), tick the “Export for ActionScript…” checkbox and give it a class name of “Ball” (capital B) then use the following code to access instance properties of a given Ball object from within the class itself.

Main Flash File (.fla) Code

import Ball;
 
var myBall:Ball = new Ball(stage);
myBall.name = "myBall"; // If you don't set a name, you can't call getChildByName("THE-NAME")
myBall.x = 100;
myBall.y = 100;
stage.addChild(myBall);
 
var ball2:Ball = new Ball(stage);
ball2.name = "ball2";
ball2.x = 150;
ball2.y = 150;
stage.addChild(ball2);

Ball.as ActionScript File Code

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.display.Stage;         // Needed to use stages
	import flash.display.DisplayObject; // Needed for getChildxxx functions, which return a DisplayObject
 
	public class Ball extends MovieClip
	{
		// Per object instance variables
		// private var someVar:SomeType;
 
		// Static (shared) instance of what stage the balls are on (i.e. a reference to the main stage)
		private static var ballStage:Stage;
 
		// Constructor
		function Ball(theStage:Stage):void
		{
			// Set the stage the particles are on so we can use it to get details.
			ballStage = theStage;
 
			// Bind each particle to update once per frame
			this.addEventListener(Event.ENTER_FRAME, updateBall);
		}
 
		private function updateBall(e:Event):void
		{
			trace(ballStage.getChildAt(0)); // MainTimeline
 
			var tmp:DisplayObject = ballStage.getChildAt(1); // myBall
			trace("By getChildAt, x is: " + tmp.x);
 
			tmp = ballStage.getChildByName("ball2"); // ball2 - requires the built-in ".name" property to be set!
			trace("By getChildByName(blah): " + tmp.x);			
 
			// Move the balls around a little on the stage, just so we can see the properties change
			if (this.x >= 200)
			{
				this.x = 1;
			}
			else
			{
				this.x += 1;
			}
		}
 
	} // End of class
 
} // End of package

The trick is that we keep a reference to (or really a copy of) the main flash file’s stage handy by passing it into the constructor, so we can refer to it and get at any instances of anything on that stage to work with. Also, if we’re getting an instance by name, then we have to set the .name property! I thought you could just get it by the instance name itself (as in, the variables own name, not it’s .name property), but apparently not.

You might wonder why go to all this effort when we could just call this.x instead of ballStage.getChildByName(“SOME_NAMED_DISPLAYOBJECT”) or such in the above code, but the reason is that by jumping through these extra hoops we can access the properties of instances of other classes inside any other class. For example, if I had a Football class and a Player class, I could access the properties of a Football instance on the stage from within the Player class, or vice-versa.

To show a more concrete example, try using the cursor keys to move the attractor (just an instance of a MovieClip on the stage) around the below – the particles interact with the location of the attractor MovieClip, because I grab a copy of it by name (which is slower than by index from what I read, but this is just an example) and then move each particle instance from that information in the Particle class:

Full source for the above keyboard attraction example can be found: here.

Cheers!

Comments
2 Comments »
Categories
Coding
Tags
ActionScript, ActionScript 3, DisplayObject, Flash, getChildAt, getChildByName, Instances, MovieClip, properties, property
Comments rss Comments rss
Trackback Trackback

ActionScript 3.0: An Introduction to ActionScript 3.0 in Flash CS4 – Week 3 Lesson 2

r3dux | March 26, 2010

We’re making good progress here in Week 4 of CUFDIG302A – Author Interactive Sequences, so we’re looking at more Symbols and Instances, and how we can use Timers to call functions at specified intervals.

If you’ve not gone through any of the previous weeks slides and you’re new to coding, then take a step back to and take it order – it’ll really help.

Flash Slides Week 3 Lesson 2

I’ve been a bit busy of late and been finding it hard to get the time and inclination to get the slides together in the different format soon after they’re done. Comments, suggestions, thanks etc. are always welcome and give me incentive to keep on keepin’ on…

As usual, the download link to the slides along with some legal gubbins regarding their use is after the break, so jump on in and assert your control!

Read the rest of this entry »

Comments
No Comments »
Categories
Coding
Tags
ActionScript, CS4, CUFDIG302A, Flash, Instances, Symbols, Timers
Comments rss Comments rss
Trackback Trackback

ActionScript 3.0: An Introduction to ActionScript 3.0 in Flash CS4 – Week 3

r3dux | March 5, 2010

We’re still finding our feet a little here in Week 3 of CUFDIG302A – Author Interactive Sequences, so we’re looking at functions, symbols, instances and the start of using event listeners with functions to control things on the screen.

If you’ve not gone through any of the previous weeks slides and you’re new to coding, then take a step back to and take it order – it’ll really help. Otherwise enjoy!

Oh, and the class has moved from just one 3-hour session per week to a 3-hour session plus a 2-hour session – so expect more updates from now until the end of the course in a couple of weeks time, at which point we might decide to take on some more complex ActionScript for a more involved course…

ActionScript 3.0 Slides - Week 3 - Lesson 1

As usual, the download link to the slides along with some legal gubbins regarding their use is after the break, so jump on in and assert your control!

Read the rest of this entry »

Comments
3 Comments »
Categories
Coding
Tags
ActionScript, CS4, CUFDIG302A, Flash, Functions, Instances, Slides, Symbols
Comments rss Comments rss
Trackback Trackback

Translate

Categories

Archives

Tags

3D ActionScript ActionScript 3.0 Adobe AI Ballarat Bash C++ Class Convert CS4 Effect Error Film Flash GLSL Gnome Hack How-To install Jaunty Java Kinect Linkage Linux Mash-Up Microsoft Motion OpenGL Particle Problem PS3 Remix Retro script Slides Sound Systems Texture Ubuntu Video VirtualBox Wii Windows XBox

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away.”

 - Antoine de Saint Exupery

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox