开发者

Creating Classes and Properties in AS3

I'm new to AS3. Learning how to create classes. Is comp = new HouseObjects creating a new class? Is comp creating an instance of the HouseObjects? I realize that this is inside public class TreeHouse. I'm thinking that HouseObjects, how I set it up is not a class...not sure what the correct way to set up classes and properties.

Also I noticed, that when I tried to link another movieclip using the same linkage name HouseObjects--it asked to enter a unique class. I'm trying to create multiple instances from the same class called HouseObjects.

Creating Classes and Properties in AS3

package {

 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 import flash.events.Event;

 public class TreeHouse extends MovieClip

 {

private var comp:MovieClip;
开发者_JS百科var powerData:int; // stores user data (of selected data)
//var currentPower:int; // stores current power

public function TreeHouse()
{
    comp = new HouseObjects; // linkage in library
    comp.power = 2; // amount of power
    comp.name = "comp";
    comp.buttonMode = true;
        comp.bstate = 0; // button state


    //add event listeners -- listens to functions that are called       
   comp.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
   comp.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
   comp.addEventListener(MouseEvent.CLICK, toggleClick);
   comp.addEventListener(MouseEvent.CLICK, toggleClick);

  stage.addChild(comp); // add computer to stage -----------------------------------
  trace("tracing...");
  comp.x = 100;
  comp.y = 100;

} 

// function rollOver -------------------------------------------------------------- 
function rolloverToggle(e:MouseEvent) {     
    if (e.currentTarget.currentFrame == 1)
    e.currentTarget.gotoAndStop(2);
   if (e.currentTarget.currentFrame == 3)
    e.currentTarget.gotoAndStop(4);
}

// function rollOut-- --------------------------------------------------------------    
function rolloutToggle(e:MouseEvent) {
    if (e.currentTarget.currentFrame == 2)
    e.currentTarget.gotoAndStop(1);
    if (e.currentTarget.currentFrame == 4)
    e.currentTarget.gotoAndStop(3); 
}

// function toggleClick-------------------------------------------------------------
   function toggleClick(e:MouseEvent) {


  // On MouseEvent gotoAndStop(Frame Number)
  if (e.currentTarget.currentFrame == 2)
   {
    e.currentTarget.gotoAndStop(3);
    e.currentTarget.bstate = 1;
   }

   if (e.currentTarget.currentFrame == 4)
   {
    e.currentTarget.gotoAndStop(1);
    e.currentTarget.bstate = 0;
   }     

 //var powerData:int = HouseObjects[e.currentTarget.power]; // set power value  

  // Find out which object selected-------------------------------------------------
  //trace("movieClip Instance Name = " + e.currentTarget); // [object Comp]
  //trace(houseArray[e.currentTarget.name]); // comp
  trace("using currentTarget: " + e.currentTarget.name); // comp
  //trace("powerData: " + powerData); // power of user data
  //trace("houseArray: " + houseArray[0]); // the 0 index of house array
  trace(e.currentTarget.power); // currentTarget's power************ 


   }

 } //end of class

} // end of package


I am not quite sure if I understood your question correctly. comp = new HouseObjects creates a new instance (object) of the type HouseObjects. (A little research on OOP basics would probably make life easier for you.)

Regarding the »Please enter a unique class name« error: You cannot assign the same class to two library symbols because the symbol is hooked up to the class internally so that if you create a new instance (var x = new HouseObjects; addChild(x);), the content from the linked symbol is also added to the display list. If there were multiple library symbols linked to the same class, how would the Flash compiler know which one to choose?


Your question is pretty broad and , as klickverbot suggests, it would be better if you took a little time to understand basic OOP concepts.

There are a lot of resources available to get you started with AS3, check this for instance http://tv.adobe.com/watch/colin-moocks-lost-actionscript-weekend/course-1-introduction

Colin Moock's tutorial is very easy to follow and will give you most of the tools you need to get started.


If you are new to AS3, and OOP in particular, you should check out Moock's Essential Actionscript 3 which is beyond fantastic for a step by step education in OOP in AS3.

HouseObjects appears to be a class and you are creating a new instance of it for the variable comp

You've got a duplicate definition. It appears that you are trying to use Flash Pro to extend HouseObjects for the lightbulb. It doesn't work like this in Flash Pro. You are creating a MovieClip symbol and giving it a class definition. It has to extend MovieClip and you cannot change this in this case. You could likely extend HouseObjects in an AS3 file and make use of it in your application.

Personally think that if you want to really get your head around OOP with AS3 you should get the book and get out of Flash Pro. Use an IDE like Flash Builder, FDT, Flash Develop, or IntelliJ IDEA. It is a lot easier to understand when you get away from the dialogs and other complications of the Flash Pro IDE :>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜