?
Solved

Scope question

Posted on 2003-11-26
3
Medium Priority
?
141 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 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
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.
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

752 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