Hello.

Question on Action Script 3.

I want to find out one problem. How to call a method of another class from one class? In the example shown here, for some reason, the picture does not appear to me. In the FirstClass class, the qwadrat method of the SecondClass class is called. In this method there should be a green square on the stage. But I do not have it. What's wrong?

Or, better write a template to call a method of another class.

package { import flash.display.Sprite; import SecondClass; public class FirstClass extends Sprite { public function FirstClass() { var kartinka:SecondClass = new SecondClass(); kartinka.qwadrat(); } } } package { import flash.display.Sprite; public class SecondClass extends Sprite { public var qw:Sprite = new Sprite(); public function qwadrat():void { qw.graphics.lineStyle(1); qw.graphics.beginFill(0x00ff00); qw.graphics.drawRect(20, 20, 200, 200); qw.graphics.endFill(); addChild(qw); } } } 
  • kartinka => image qwadrat => rectangle - Specter

3 answers 3

So you did not add SecondClass anywhere, as I understand it ... He also needs to call addChild.

  • SecondClass is a class and the addChild method is not applied to it. I think so. An instance of the SecondClass class, kartinka, was created in the FirstClass class, with a constructor call, and thus it was already created. The addChild method applies to display objects. - mitair
  • The addChild () method is applicable to all classes inherited from the Sprite class, the SecondClass extends Sprite class is no exception. - AlexAndR
  • Question? How to apply the addChild method to a class? In this case, to the SecondClass class in the FirstClass class. - mitair
  • everything that is somehow added to the main control is displayed public class Main extends MovieClip if you have it in a global variable, for example, you can write at least instead of addChild (qw); something like mainControl.addChild (qw); - AlexAndR

It is necessary to add in the class FirstClass addChild (kartinka.qw);
in this way:

public class FirstClass extends Sprite {

 public function FirstClass() { var kartinka:SecondClass = new SecondClass(); kartinka.qwadrat(); addChild(kartinka.qw); } 

}

    stage.addChild (qw); it was necessary to write in SecondClass!

    • Therefore, when added to SecondClass does not work. It is necessary to write in FirstClass. And addChild (qw) or stage.addChild (qw) is not particularly important. It works now. - mitair