Solved

Scope question

Posted on 2003-11-26
3
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
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…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
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.

733 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