Archived

This topic is now archived and is closed to further replies.

shurcool

Anyone proficient at Flash MX?

Recommended Posts

Hey guys, Sorry about posting this in here, but I don't really know any good Macromedia Flash forums, and it is a game I'm making. I'm mostly self-taught in Flash (reading tutorials, help files, experimenting, etc.), because the course I took in last year in high-school was very basic. I'm making another game in Flash, and this time I need a more object oriented approach. Suppose I'm making an airplane game. My object would be a Plane (a movie clip). Inside, it contains all the information about itself, such as its coordinates and direction (_x, _y, _rotation), as well as some other things (such as bHumanController, when true, it uses keyboard for input, else its AI code that controls the flight). So here's what the inside of my Plane movie clip looks like.
var fX = _x;
var fY = _y;
var fSpeed = 0.0;
var nAngle = _rotation;
var bMaxSpeed = false;
var bHumanController = false;

onEnterFrame = function()
{
	if (bHumanController) {
		// User input
	} else {
		// Bot AI
	}

	// Simple 2D flight physics, etc.
} 
So I have my main scene, suppose it's _root. I want to create a couple of airplanes, and initialize their properties. So I set the Plane movie clip's linkage identifier to "Plane", and go like this in _root:
// Create my first Plane object, which will be centered and will be controlled by a human
attachMovie("Plane", "Plane0", 0);
Plane0._x = 320;
Plane0._y = 360;
Plane0.bHumanController = true;

// Create the 2nd Plane object, this time it's located in top right corner and is controlled by AI
attachMovie("Plane", "Plane1", 1);
Plane1._x = 560;
Plane1._y = 220;
Plane1.bHumanController = false;	// Unnecessary, since it's already false by default 
The problem arises because of this line in the Plane movice clip code (topmost code): var bHumanController = false; As you can see, it initializes the variable to false, and that's exactly what I want. The problem is that it does this *after* I change those values from _root, thus making both the planes AI controlled. So, my question is... How do I create instances of movie clips with variables that are initialized, and change them later on? There must be a good and elegant way of doing this (instead of just removing the default initialization for bHumanController altogether), so please help me. Thanks in advance, shurcooL` ---
shurcool
wwdev
[edited by - shurcooL on November 15, 2003 12:06:04 AM]

Share this post


Link to post
Share on other sites
Your problem is two fold. Let me try to explain what you do:

1) You create the object
2) Then you want to initialize some values
3) Then you want to set some values

But what you are trying to achieve is a mix of 2) and 3).

First you want the code to initialize bHumanController to false. Then you change some values such as position and rotation and next you want the code to initialize the values of fX, fY, etc. to the new set x and y values.

So if you would change your code to initialize when it is created, your values for fX, fY, etc. will not be set correctly.

Here is what I propose that you could do:


// Create my first Plane object, which will be centered
// and will be controlled by a human

attachMovie("Plane", "Plane0", 0);
plane0.initializeValues();
Plane0.bHumanController = true;
Plane0._x = 320;
Plane0._y = 360;
plane0.resetPosition();

// Create the 2nd Plane object, this time it''s located
// in top right corner and is controlled by AI
attachMovie("Plane", "Plane1", 1);
plane1.initializeValues();
Plane1.bHumanController = false;
Plane1._x = 560;
Plane1._y = 220;
plane1.resetPosition();


Share this post


Link to post
Share on other sites
Habox, thanks for the suggestion, I guess I'll do it that way for now. I was kinda hoping that initObject would work though (update: it does work, it's just a shortcut for what I did before), but for some reason it doesn't. Maybe I'm using it wrong?

myMovieClip.attachMovie( idName, newName, depth [, initObject] )

initObject - An object containing properties with which to populate the newly attached movie clip. This parameter allows dynamically created movie clips to receive clip parameters. If initObject is not an object, it is ignored. All properties of initObject are copied into the new instance. The properties specified with initObject are available to the constructor function. This parameter is optional.

So I would expect something like this to do the job, but once again, the var declarations override it. Is there a magical thing where I can put their default values (in C++ it's called a constructor) so that it wouldn't do this?

var init = { bHumanController : true, bMaxSpeed : true };

attachMovie("Plane", "Plane0", 0, init);
I've tried looking at ScrollBar UI component code, because the same lines above do work for initializing its values, but kinda gave up. Guess I'll have to look into that a bit more, or does anyone know?

---
shurcool
wwdev


[edited by - shurcooL on November 17, 2003 9:30:19 AM]

Share this post


Link to post
Share on other sites
All right, I've figured it out now! It works exactly how I wanted. Thanks to all who helped.

In case someone ever needs to do something similar, here's what you do...

Look into the ActionScript documentation on Object.registerClass and #initclip/#endclipinit functions. That should tell you everything you need to know, so hopefully you can go from there. You create a class constructor function, register the class, and then attachMovie the MC. Or you could just place it while in design mode (when you draw stuff), that'll work too. An example would be...

#initclip	// This is used so that this gets called before anything else, so that it'd be ready for usage when you use it

function Plane()
{
// Variables can go here, along with their default values
Plane.prototype.fSpeed = 0.0;
}

Plane.prototype = new MovieClip(); // Inherit from a MovieClip class

Plane.prototype.MyFunction = function() { /* ... */ }

Plane.prototype.onEnterFrame = function() { /* Etc. */ }

Object.registerClass("PlaneSymbol", Plane); // Connect the class Plane and the movie clip with the linkage export name "PlaneSymbol" together

#endinitclip
Hope you get the idea.

---
shurcool
wwdev


[edited by - shurcooL on November 17, 2003 8:35:27 PM]

Share this post


Link to post
Share on other sites