开发者

How to reference var from frame on timeline in an object class

开发者 https://www.devze.com 2023-02-05 12:45 出处:网络
I\'m using Flash Professional cs5/AS3 I\'ll try and describe this the best I can. I\'m new to ActionScript.

I'm using Flash Professional cs5/AS3

I'll try and describe this the best I can. I'm new to ActionScript.

So, in my timeline I have a var on a frame that represents "lives" and i have some code in the timeline that takes down the number of lives depending on certain events, which all works great.

so, now i wanted to make a constructor class that I could reuse for a bunch of movie clip objects and I only want these objects to be able to move if the lives variable is greater than certain number. So now, building my constructor class for these objects i just wanted put an if statement that is looking to see if the lives are greater than a certain number, which if it is then should make these objects do what i want...But, when i run the project I get "1120: Access of undefined property lives." lives is the var I made obviously like I said, and it works fine being referenced everyone else except when I make a new .as file for these objects then try and reference it. I get the same error when I try and establish "lives" in the main project class too. I'm not sure where I should put this var or how I can make it so i can reference it from an object class.

I'm not really sure how to word or describe my issue which has made it hard to search for a tutorial. 开发者_JAVA技巧Any suggestions i'm sure this has to be a simple task.


You can make "lives" into a static variable in your class file. You should know that there is some controversy on whether or not static vars are a good thing, but if your project is not too big and it works for you then you can use it. It's certainly easy.

If you declare something as static in a class, it will become global (=available to all your code).

Basically you write this in your class (.as) file:

public static var lives:uint = 99;

Now, in your timeline code you have to replace the var you are using with this one. You access static vars by typing out the class name followed by a dot and the name of the static var. Example: Your class is named "MyClass". Then you access the var by typing MyClass.lives

You do have to import the class file, but I assume you know how that works and with CS5 auto-complete (ctrl+spacebar) I think it does it for you. It'll be something like this:

import myclassdirectory.MyClass;


It is considered best practice to use the timeline for your graphic assets and external classes for code , this will give you far more flexibility and problems such as the one you mention shouldn't prove too difficult to solve.

Consider some parameters in your document class.

 public class MyDocumentClass
 {
      //An Object to store various parameters,
      //you could also use separate vars depending on
      //your MCs requirements
      private var params:Object ;

      public function MyDocumentClass
      {
          params = {};
          params.lives = 99;

          //Create a new MovieClip instance
          var myObject:MyObject = new MyObject( params );

          //fron now on you can access the lives property in your object
          addChild( myObject );

      }
  }


The other answers are definitely good solutions and were very helpful, the first one may be better. But what i decided to use for my situation was just create a function on the main frame that contained the code to decrease a life. Then I just ran the function from the other fram by doing this

//created a var in my movieclip that "contained" the main timeline
var main:MovieClip = MovieClip(this.parent.parent);

//then simply ran the function from the movieclip
main.decreaseLife();
0

精彩评论

暂无评论...
验证码 换一张
取 消