[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Scope question

Posted on 2003-11-26
3
Medium Priority
?
143 Views
Last Modified: 2010-04-03
Im relatively new to flash an was wondering how this works exactly.

suppose I have a class.....

class Product
{
//variable declaration
var id:Number
var productName:String
var price:Number
//constructor
function Product (id:Number, prodName:Name, price:Number)
 {
 }

function getProduct{
       this.id  = id;
       this.prodName = prodName;
       this.price = price;
}
}


Suppose I have another class where I need to reference this.price?

how do I go about this?  Can I just go ahead and make this.price global so I can use it in any of my classes?  Or do I have to go like this???

class Store{
         var myNewVar:Number;

          var cliplessPedal:Product=new Product(1, "Clipless Pedal", 11);
          cliplessPedal.getProduct
          myNewVar = cliplessPedal.price
}


thanks,

knoxxx

0
Comment
Question by:JohnnyKnoxville
  • 2
3 Comments
 
LVL 27

Expert Comment

by:negatyve
ID: 9827244
The first class should be something like that:

class Product
{
      //variable declaration
      var id:Number
      var productName:String
      var price:Number
      //constructor
      function Product (pId:Number, pProductName:String, pPrice:Number)
      {
            // save parameters
            id = pId;
            productName = pProductName;
            price = pPrice;
      }

      function getProduct():Object
      {
            // returns an object with the instance's properties
            return {id:id, productName:productName, price:price};
      }
}



the second one should be:

class Store
{
      var myNewVar:Number;
      // suppose you have created an instance of Product Class,
      // and to pass it's reference to the instance of Store
      function Store(Product_instance_reference:Object)
      {
            // save in myNewVar the property "price" that you get
            // invoking the getProduct method from the Product Class
            // instance whose reference is in the Product_instance_reference
            // parameter
            myNewVar = Product_instance_reference.getProduct().price
      }
}
0
 

Author Comment

by:JohnnyKnoxville
ID: 9828605
Cool..thanks.    Im planning on creating a bunch of classes.  This will be my first run at it.  Once I create all of my classes....where do I instantiate them?  After reading some tutorials on the web....it looks like I am supposed to create an 'Actions' layer and put the code in there.  Is this correct?  Where do all of the events get triggered?  Do I put code in movies and on components also?  Seems like I'd be putting code all over the place.


thanks,,

knoxxx
0
 
LVL 27

Accepted Solution

by:
negatyve earned 200 total points
ID: 9828637
>Seems like I'd be putting code all over the place.

you are going to write inside the external as file only the classes definitions.
inside your movie you just instantiate your object and apply them their methods.
for example, in a frame of your movie:

var cliplessPedal:Product = new Product(1, "Clipless Pedal", 11);
var testInstance:Store = new Store(cliplessPedal);
trace(testInstance.myNewVar); // should trace 11
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question