• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Calling a function from another class

Guest
Apr 27, 2009 Apr 27, 2009

Copy link to clipboard

Copied

Hi:

How do you call a function from another class in AS3? Functions referring properties/methods from instances created in that class. For example I have a class that creates a textfield and a scrollbar and another that load some images in a Scroll Pane component. I do it in that way because in some menus appears only text and in others text & images

What I need is with the scrollbar, scroll both text and images

om_bio.jpg

Thanks in advance

TOPICS
ActionScript

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2009 Apr 27, 2009

Copy link to clipboard

Copied

you must have access to the class (for a static method) or a class instance (for an instance method).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 28, 2009 Apr 28, 2009

Copy link to clipboard

Copied

thanks for your answer; i'm very new to AS3. now i think i'm clear about the differences between static & instance methods but still some doubts accessing the last ones

static methods

i only need to specify the class name and the method like in Math class where i don't need to create an instance to use its methods

instance methods

i need a reference to the class instance to access the methods but how exactly is it done? i've been searching but only found examples about static methods

briefly, i have 3 classes:

- NavBar: generates the menu from an XML file

package classes {

  ...

  import classes.Contents;

  import classes.ImgScroll;

  public class NavBars extends Sprite {

    private var contenido:Contents;

    private var scrollImg:ImgScroll;

    ...

    private function setContenido(par:String):void {

      contenido = new Contents("xml file");

      ...

      if (menuWithImages) scrollImg = new ImgScroll("folderName");

      ...

- Contents: generate a dynamic textfield and populate it from an XML file. create the custom scroll bar

- ImgScroll: load images from the server (if the selected menu has images). it has instance methods to scroll images

so, when the user interacts with the scroll bar the text scrolls and i must call an instance method in class ImgScroll

thanks in advance

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi!

I have the same issue here, can you help me please?

I am trying to import with the instance method:

Main .as file:

package { (...)

function xmlLoaded(event: Event): void {

(...)

  var classe: TouchList = new TouchList();

  classe.translate();

 

Iimported .as function class:

public class TouchList extends Sprite

  {

     public function translate():void

     {

      (...)

     }

It gives me an error in line:

var classe: TouchList = new TouchList();

"Wrong number of arguments. Expected 2"

Can you help me?

Thank you

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

@filipeo32198069 - There's nothing wrong with the code you showed. Are you sure your TouchList class is saved as TouchList.as and you have a class path to it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

LATEST

I have found the error!

I had two instances for the same class, and for some reason Flash coudn't accept that.

Unfortunately, it didn't solve my problem.

But that isn't related to this post, so i will write it in another post.

Thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

What I would do is have NavBar dispatch an event when the bar is scrolled - your class extends Sprite so you can use EventDispatcher. Then the listener function can call your ImgScroll as necessary. Esentially use a Main class to instantiate your other classes, then the Main class can listen for events from the other classes and respond as needed.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines