Reputation: 11
In ActionScript, always when I declare a class for an object I use the same name of the object for the class and the function I want to call. For instance, if the object is Card the class and function names are Card too.
But for the first time in the AS game programming university book I see a declared class that is not like I said.
part of the book is teaching "how to create a Matching Game" and they declare two classes for the game. one that is the main class is about matching cards and everything we need to create the game. and the second class is just for flipping cards and just for more beauty. in the first one we create a new symbol in second frame to call the class and its function, and the names are same. so when we reach second frame the symbol calls its class and then its function. and one of the display objects in this frame is "Card". and we need these cards to flip when they are turning over and we dont do this by creating movieClip for them, we just do this by writing AS for the Cards. you can see the actionScript here:
my question is which function Flash will choose to play when the name of the function is not the same as the name of the object and class? (that is "Card" here)
package {
import flash.display.*;
import flash.events.*;
public dynamic class Card extends MovieClip {
private var flipStep:uint;
private var isFlipping:Boolean = false;
private var flipToFrame:uint;
// begin the flip, remember which frame to jump to
public function startFlip(flipToWhichFrame:uint) {
isFlipping = true;
flipStep = 10;
flipToFrame = flipToWhichFrame;
this.addEventListener(Event.ENTER_FRAME, flip);
}
// take 10 steps to flip
public function flip(event:Event) {
flipStep--; // next step
if (flipStep > 5) { // first half of flip
this.scaleX = .2*(flipStep-6);
} else { // second half of flip
this.scaleX = .2*(5-flipStep);
}
// when it is the middle of the flip, go to new frame
if (flipStep == 5) {
gotoAndStop(flipToFrame);
}
// at the end of the flip, stop the animation
if (flipStep == 0) {
this.removeEventListener(Event.ENTER_FRAME, flip);
}
}
}
}
Upvotes: 1
Views: 333
Reputation: 9572
When you create a class in Actionscript , your class has a specific function called the constructor which effectively has the same name as the class.
public class MyExample
{
public function MyExample()
{
trace('MyExample constructor!');
}
}
When you instantiate your class , the constructor is called.
// trace 'MyExample constructor!'
private var ex1:MyExample = new MyExample();
Your class can have public methods that , unlike your class constructor , will not be automatically called
public class MyExample
{
public function MyExample()
{
trace('MyExample constructor!');
}
public function letsFlipSomeCards():void
{
trace('I like this card game!');
}
}
You will need to specifically invoke them:
private var ex1:MyExample;
private function init():void
{
ex1 = new MyExample(); // trace 'MyExample constructor!'
ex1.letsFlipSomeCards(); // trace 'I like this card game!'
}
If you perform some searches on this site about good tutorials for AS3 you will get a lot of advice on books or video tutorials that should give you a good start on the language.
Upvotes: 2