Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 26th, 2003, 12:46 AM
kubicon's Avatar
kubicon kubicon is offline
pogremar
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: At Work
Posts: 945 kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 14 h 50 m 47 sec
Reputation Power: 7
class code placement

I wrote c++ programs before so I'm familiar with classes. My question is about the placement of the source code in flash. Let me explain:
I want to make a class, say creature, that I can assign to movieclips on the stage and have them make use of the class.
for instance, say I have this code(which I don't know if it's correct).
PHP Code:
function CREATURE(obj) {
    
this.obj obj;
    
this.speed 5;
    
this.move();
}
CREATURE.prototype.move = function() {
    if (
key.isDown(key.LEFT)) {
        
this.obj._x -= this.speed;
    }
    if (
key.isDown(key.RIGHT)) {
        
this.obj._x += this.speed;
    }
    if (
key.isDown(key.UP)) {
        
this.obj._y -= this.speed;
    }
    if (
key.isDown(key.DOWN)) {
        
this.obj._y += this.speed;
    }
}; 


Like I said, I want to have this code just once and then have the MCs make use of it. So, I figure I would do something like this.

onClipEvent (load) {
ball = new CREATURE(this);
}
onClipEvent (enterFrame) {
ball.move();
}

I figured I put the first code snnipet, the class definition, on the time line and then make a class intance on the particular object. This is not working though. What's the correct way to do this?
__________________
Some day I'll create a smart quote to put here.

Reply With Quote
  #2  
Old October 26th, 2003, 06:31 AM
mushie mushie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 58 mushie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
depends on version really - 6 or 7??
version 6 (actionscript 1)
PHP Code:
//=================================
//     Create namespace in which to store classes
//=================================
if (_global.com == undefined) {
    
_global.com = new Object();
}
if (
_global.com.domain == undefined) {
    
_global.com.domain = new Object();
}
//=================================
/*constructor */
namespace.ClassName = function() {
    
// Create Instance properties.
    // no 'real' private and public in flash 6
    // Private
    
this.privateProperty value;
    
// Public
    
this.publicProperty this.params.passedValue;
    
// Initialise Instance.
    
this.method(this.params.passedValue1this.params.passedValue2);
    
// Remove constructor params object from the Instance.
    
delete this.params;
};
//
/* Set MovieClip as ClassName's superclass.*/
//
namespace.ClassName.prototype = new MovieClip();
//
/*Associate the Library's menu item symbol with ClassName class.*/
//
Object.registerClass("librarySymbolName"namespace.ClassName);
//
/* methods */
namespace.ClassName.prototype.method1= function(param) {
    
this.property param;
};
/*
.
.
.
*/
namespace.ClassName.prototype.methodN= function(param,obj,blah) {
    
this.property param;
    
/*
    .
    */
};
//****************************************************//
// instantiate your object from your class
//
// create some parameters and store in an object
myObj = new Object();
myObj.param1=value1;
/*
.
.
*/
myObj.paramN=valueN;
//
// create obj to pass to constructor containing params
var initObj = new Object();
// add params object
initObj.params myObj;
// create the instance - use of this depends on path, could be _root, _parent etc instead of 'this'
this.attachMovie("librarySymbolName""instanceName"levelinitObj);
// bingo, you're done :) 

flash 2004 - same way of instantiating object, but you asign the class to the library item by right-click>>properties

like that.

hth

** admins, why are the lines double in the code????
__________________
beware of geeks bearing animated gifs.

Reply With Quote
  #3  
Old October 26th, 2003, 08:15 AM
kubicon's Avatar
kubicon kubicon is offline
pogremar
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: At Work
Posts: 945 kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 14 h 50 m 47 sec
Reputation Power: 7
Thanks for the reply, but all the abstraction is confusing me. For instance:
namespace.ClassName
Which namespace do I use?
Also, where is all this code placed, in the timeline, instance or part in the timeline and part in the instance?

Reply With Quote
  #4  
Old October 26th, 2003, 08:55 AM
mushie mushie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 58 mushie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
PHP Code:
//=================================
//     Create namespace in which to store classes
//=================================

if (_global.com == undefined) {
    
_global.com = new Object();
}
if (
_global.com.domain == undefined) {
    
_global.com.domain = new Object();
}
//================================= 


put that anywhere - you dont HAVE TO do this, it just makes things easier in the long run - its a workaround for namespaces in flash 6, dont need it in 7. essentially it creates an empty global object to store the classes in.
since the namespace object is global it allows easy access from anywhere as well.

so for com substitute your top level domain, and for domain your, um, domain name.... or call it oyster.orgy.from.hell if you must, it doesnt matter

everything up until
PHP Code:
// instantiate your object from your class 
is the class definition
beneath that is the instantiation.

i put each of my class scripts in the first frame of the main timeline on a different layer, with another layer(s) for instantiating the classes - this is done with each as a function; i then have another function called main() that calls those functions.
finally i have a layer with a frame script of just
PHP Code:
 main(); 

to start everything.

everything willl ahve to be done in frame one of a movie unless you use
#initclip - look it up in the manual and on the web.

i though the abstraction was the way to go, rather than being asked where the 'varTracker' object was in the actionscript dictionary, and that i had commented quite clearly what each section did, sorry if that led to confusion.

clearer now? or still like mud?

Last edited by mushie : October 26th, 2003 at 08:58 AM.

Reply With Quote
  #5  
Old October 26th, 2003, 09:31 AM
kubicon's Avatar
kubicon kubicon is offline
pogremar
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: At Work
Posts: 945 kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level)kubicon User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 14 h 50 m 47 sec
Reputation Power: 7
still like mud. Thanks for the help, though. I guess I'm just not familiar enough, yet, with how flash handles objects. I will continue looking at some more examples.

Reply With Quote
  #6  
Old October 26th, 2003, 11:38 AM
mushie mushie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 58 mushie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
it is pretty ropey in flash; not helped by the fact that actionscript isnt exactly strict either.

heres a basic fla http://www.newmediascience.com/star.fla

very simple class declaration and instantiation, using an event listener to call the object method.

thats another problem, as movieclips dont have native event listeners - there is an undocumented (at least by macromedia) method for adding listeners to ANY objects called ASBroadcaster - have a look here, works great .
ASBroadcaster
ive just used an empty Object as a controller.

hth

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > class code placement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway