Be a Supporter!

How to call methods?

  • 292 Views
  • 2 Replies
New Topic Respond to this Topic
SonOfChaos
SonOfChaos
  • Member since: Jan. 12, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
How to call methods? 2009-05-04 07:22:03 Reply

Okay, I've got a problem calling methods or functions or whatever their called for different files. Hypothetical Code below to explain problem.

main file.

[code]
package{
public class whatev extends MovieClip{
public function whatev(){
var s:animal = new animal()
addChild(s)
s.addEventListener(Event.ENTER_FRAME, collisionCheck)
}
public function collisionCheck(evt:Event){
//Does stuff like calls method from animal class called morestufftodo...how?
}
}
}

[/code]

animal file

[code]
package {
public class animal extends MovieClip{
public function animal(){
//does stuff
}
public function morestufftodo(){
//I want this to fire upon being called by main class in event of a collision
}
}
}

[/code]

Hope you all can help

dragonjet
dragonjet
  • Member since: Dec. 2, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to How to call methods? 2009-05-04 09:52:41 Reply

you don't call these,
these are not methods, but classes.
ie: AS File, ActonScript File, Class File
you import them.

import packageName.*;
import packageName.className;
henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to How to call methods? 2009-05-05 02:15:43 Reply

He made his point in the code.

There are three ways of doing this, some complicated, some not. I will list them all in a random order fo you to pick the one you need.

The first one is to dispatch an event of your own if a collision is detected. Pro: the sending class doesn't need to know who is listening. pro: it works if you want more than one thing to run at a collision. Con: if you want any special parameters, you will need to write a custom event object class and use that.

The second one is to have a reference to the class instance that has the method. Pro: it's fairly simple calling syntax. Pro: you might have a need for the reference for other purposes anyway. Con: It ties the code to just one type.

The third one is to use Function objects. Con: it's fairly advanced. Pro: You can call those anonymous inline functions. Pro: it's easy to swap the function to call. Pro: in as 3 it works fine with methods. Con: It doesn't work fine with methods in as 2, they forget their objects.

Happy brain overload due to my overkill response. But I take pride in leaving nothing out. I inform, you decide.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.