PDA

View Full Version : How to use Object.registerClass with 2.0 class


Eric Echols
11-03-2003, 11:33 AM
I think I have been looking at this too long, and am probably missing something very simple, but I'm getting aggravated.

Can you use Object.registerClass with an AS 2.0 Class, if so HOW?

Any help would be greatly appreciated.

Stimpson
11-03-2003, 02:58 PM
With a AS 2.0 class you can't use Object.registerclass, they really changed things. Take a look at these docs at macromedia to see how it's done with AS2.0 classes (If you haven't already since you were looking) :

http://www.macromedia.com/devnet/mx/flash/articles/buildtest_comp.html
http://www.macromedia.com/devnet/mx/flash/articles/component_architecture.html
http://www.macromedia.com/devnet/mx/flash/articles/footer_component.html

You can use Object.registerClass with AS1.0 style classes with AS2.0 settings. But that's probebly not what youre looking for (wouldn't make any sense either :rolleyes: )

Eric Echols
11-03-2003, 03:27 PM
Thanks Stimpson,

I'm reading them now. I'll write back if I'm still baffled.

:)

Eric Echols
11-03-2003, 03:40 PM
My class isn't for a UIComponent, which is probably why I'm having such a hard time finding the solution. I'm returning an ASObject from ASP.NET Flash Remoting, however I found out after a couple of hours of research, that even though you have to assign objects an ASType in .Net, you are only able to receive a generic object, in the Actionscript.

I really need a cast statement, like in C/C++ here, is there anything similar in AS 2.0? Getting desperate, I may end up rewriting the class with an interface, if I'm stuck for much longer.

Thanks again for you help, and any additional comments or help would be greatly appreciated.

Stimpson
11-03-2003, 04:19 PM
Hmmm, I'm afraid I haven't got much experience with .Net flash remoting. You can check out the remoting section of macromedia and see if there is something specific about you're prob:

http://www.macromedia.com/devnet/mx/flashremoting/

But I guess you've already done that.

Don't know about cast though, think not, but I think you're better, if possible, with stinking to As 1.0 style classes. It's much more permitting and you can use Object.registerclass.

thehiatus
11-04-2003, 04:21 PM
I don't know if it's what you're looking for, but you can cast objects in AS2.0 by using the class name as a function. For example, if you have a class called "Shape" and you wanted to cast a generic Object as a Shape, you could do the following:

// assume your generic Object is a variable, "obj".
var s:Shape = Shape(obj);(

Hope this helps.

hoekeat
11-06-2003, 10:11 PM

hoekeat
11-06-2003, 10:15 PM
i am not 100% sure what u mean, but i will give it a try

when u say "an AS 2.0 Class", do u mean class defined in an external .as file wif the signature of

class myClass extends MovieClip{

function myFunction()
{
...blah blah blah
}

function myOtherFunction()
{
...blah blah
}
}

well, yes, u can do it

just create a new symbol in ur library, then put

#initclip
Object.registerClass("myClassSymbol", myClass );
#endinitclip

into the code for the Symbol library resource

ericlin
11-07-2003, 12:34 AM
How about this:


Object.registerClass("Car", vehcleClass );
attachMovie("Car","myCar",2);
Object.registerClass("Truck", vehcleClass );
attachMovie("Truck","myTruck",3);

agent81
11-07-2003, 02:03 AM
vehicleClass??

****, i don't know:D :D

ericlin
11-07-2003, 04:44 AM
:)
I copied the first two lines and paste below before I changed the second symbol name. So, I got typo 'i' at the same place.

If my Class name and filename also missed the 'i', that code will still work.
:)

Eric Echols
11-07-2003, 01:27 PM
Thanks to everyone for the responses. I have discovered a solution to the problem that I encountered. I will now fully describe the problem and the solution/workaround that I have used.

******** Warning this will include code that should be considered Alpha quality, and should not be used as an authoritative example ********

/************************************************** ***********************
** PacKage: Data **
** Class: User **
** **
** Class Object for User Information **
** **
** Location: Local Mapped Drive X: **
** Directory: Data **
************************************************** ***********************/
class Data.User {
var UserName:String;
var Password:String;
var FirstName:String;
var LastName:String;
var UserID:Number;
public function User() {
UserID = 0;
UserName = "";
Password = "";
FirstName = "";
LastName = "";
}
public function SetUserName(uname:String){
UserName = uname;
}
public function SetPassword(pwd:String){
Password = pwd;
}
public function SetFirstName(fname:String){
FirstName = fname;
}
public function SetLastName(lname:String){
LastName = lname;
}
public function GetUserID():Number{
return (UserID);
}
public function SetUserID(cuid:Number){
UserID = cuid;
}
public function GetFirstName():String{
return (FirstName);
}
public function GetLastName():String{
return (LastName);
}
public function GetPassword():String{
return (Password);
}
}




/************************************************** ***********************
** AddUser Class **
************************************************** ***********************/
#include "classes/AddUserResponse.as"

function AddUser(){
this.init();
trace("AddUser Init");
}

ASBroadcaster.initialize(AddUser.prototype);

Object.registerClass("AddUserClass", AddUser);

AddUser.prototype.init = function(){
var UserResult:User = new User();
this.UserResult.FirstName = "";
this.UserResult.LastName = "";
this.UserResult.UserName = "";
this.UserResult.Password = "";
this.UserResult.UserID = 0;

this.gatewayUrl("http://localhost/remoting1/gateway.aspx");
this.serviceName = "Remoting.User";
}
AddUser.prototype.setGatewayUrl = function(gateway) {
this.gatewayURL = gateway;
}

AddUser.prototype.getGatewayUrl = function(){
return this.gatewayURL;
}

AddUser.prototype.setServiceName = function(service) {
this.serviceName = service;
}

AddUser.prototype.getServiceName = function(){
return this.serviceName;
}

AddUser.prototype.doSave = function(UserData) {
for(item in UserData){
if(item == "" || item == undefined){
this.onStatus({description:"All Fields are Required"});
return;
}
}
this.UserResult = UserData;
trace(this.gatewayURL);
var server = NetServices.createGatewayConnection(this.gatewayUR L);
var service = server.getService(this.serviceName, this);
service.AddNewUser(UserData);
}

AddUser.prototype.AddNewUser_Result = function(results) {
trace("Results = "+ results.toString());
var response = new AddUserResponse(results, this.UserResult);
this.broadcastMessage("onNewUserAdded", response);
}

AddUser.prototype.onStatus = function(status) {
this.broadcastMessage("onError", status);
trace("Status ="+status);
}



/************************************************** ***********************
** The AddUserResponse object holds all of the data returned by the **
** ASP.NET Remoting.User Class Library and creates a local object **
** of the User Class named UserResult which holds the returned data. **
** **
** @class AddUserResponse **
************************************************** ***********************/

_global.AddUserResponse = function(adduserresults, userdata) {
if(this._init) {
this.processResponse(this.rawResponse, this.userdata);
return;
}
var UserResult:User = new User();
this._init = true;
this.rawResponse = null;
this.processResponse(adduserresults, userdata);
}

Object.registerClass("AddUserResponse",AddUserResponse);

AddUserResponse.prototype.processResponse = function(rawResponse, UserData:User) {
for(item in rawResponse){
trace(item);
}
this.UserResults = UserData;
this.UserResults.UserID = rawResponse.UserID;
}

///////////////////////////////////////
// Getter method
///////////////////////////////////////

AddUserResponse.prototype.getUserResults = function():User {
return this.UserResults;
}


This is the Method in the Main Application Object that accepts the User interface Action, that uses the above classes.



ApplicationClass.prototype.onNewUserAdded = function(userobj) {
this.dim_mc._visible = false;
this.hideLoadingMessage();
ApplicationClass.UData = userobj.UserResults;
ApplicationClass.UserID = userobj.UserResults.UserID;
trace(ApplicationClass.UserID);
var message:String = "Your UserID is "+userobj.UserResults.UserID;
var title:String = "User Information";
this.showErrorMessage(message, title);
}


To Answer my own question, which hasn't been implemented in this version of the code:
To Cast an Object -----
myObject = User (returnedObject);


I will update this message when I have fully implemented, and have a working version using the cast method.

bleepbloop
07-21-2004, 03:08 PM
and to complicate things further, I have a question about the RegisterClass mention at the top of this thread.

If you cannot use registerclass anymore, how would you add a custom Object (instance of a custom class) to a shared object? I have a Player object that I would like to add to a shared object, and the examples I have for sharedObjects suggest that I use Object.registerClass ("MyClassID", myClass) so that an object knows which class instantiated it.

Do you know if it would be implicit in an actionscript object that it knows which class created it? Meaning that I am worrying about nothing? ;)

Thanks in advance.

JoshuaLawrence
07-22-2004, 05:29 AM
Eric, if I read your code correctly, you aren't using Object.registerClass() with ActionScript 2.0 classes, you are using ActionScript 1.0 classes whenever you want to register them. Is that so?

I'm having the same problem with both Java Flash Remoting, and just using a LocalConnection.

jgraup
02-20-2007, 08:48 PM
function theClass (){}// constructor
theClass.prototype = new MovieClip();// super class
theClass.prototype.tester = function( str:String ){ trace( ((str==undefined)?'':str) + ' test' ) }; // new method
theClass.prototype.mover = function( val ){ this._x += ((isNaN(val))?50:val) }; // new method

// test class
var newClass = new theClass();
newClass.tester( 'from new class: ' ); // from new class: test

// on the fly
Object.registerClass( 'BallLinkageID', theClass ); // register class to library linkageID - "BallLinkageID"
var mc:MovieClip = this.attachMovie ( 'BallLinkageID', 'mc', this.getNextHighestDepth() ); // attach instance to the stage
mc.tester( 'from new mc: ' ); // from new mc: test
mc.mover ( 100 ); // ball moves 100 pixels to the right

// set class of the current ball on the stage
ball.__proto__ = theClass.prototype
ball.tester( 'from ball mc: ' );
ball.mover ( 100 ); // ball moves 100 pixels to the right
ball.tester( 'still a ball: ' );

// reset it's class to a new class
function blankClass (){}
blankClass.prototype.newFunc = function() { trace( 'newFunc: ' + arguments )}
var myBlankClass = new blankClass();

ball.__proto__ = blankClass.prototype
ball.tester( 'i never trace: ' );
ball.newFunc( 'im a new function' );
ball._x = 0; // still works

trace( newline )
trace( mc.__proto__ == blankClass.prototype ); // false
trace( mc.__proto__ == theClass.prototype ); // true
trace( mc.__proto__ == MovieClip.prototype ); // false

trace( newline )
trace( ball.__proto__ == blankClass.prototype ); // true
trace( ball.__proto__ == theClass.prototype ); // false
trace( ball.__proto__ == MovieClip.prototype ); // false

senocular
02-20-2007, 08:57 PM
yeah... bit of an old post :)

jgraup
02-21-2007, 10:13 PM
Well, that's an understandable statement coming from THE Senocular.

In the furture, I'll try to keep my comments more relevant to subjects within the past 2 years, but it takes about that long to get through whatever you've come up within any given month :)

So for now, I just stick to the 'if it's new to you...' approach, and blog about it at http://www.justgooddesign.com/blog/

On a somewhat similar and more fresh subject, as I am currently a Flash prototyper, if you have thoughts/approaches on using dynamic assets with dynamic code I'd be interested. It may still be on topic with "How to use Object.registerClass with 2.0 class".

By dynamic code, I'll specify - 'a way to account for multiple iterations of interaction styles using dynamic assets that could be specified in XML and accessed/updated locally'.

Maybe I'm thinking something like Simon Wacker's - XML Dialect for Flash User Interfaces (http://www.simonwacker.com/blog/archives/000105.php) but less complicated.

Cheers!