Solved

Scope question

Posted on 2003-11-26
3
133 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 50 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now