I am new to Action Script and very confused about why I am getting a stack overflow. I will post all 3 classes (what's relevant). And the output I get. Can someone explain to me why this will cause a stack overflow?
Main Class:
public function Main() {
//Problem starts here
player1 = new Player();
player2 = new Player();
// More code that does not matter
}
Player Class:
package {
import fl.motion.Color;
import flash.display.MovieClip;
public class Player extends MovieClip {
public function Player() {
var index:Finger = new Finger();
var middle:Finger = new Finger();
var ring:Finger = new Finger();
var pinkie:Finger = new Finger();
}
}
}
Finger Class:
package {
public class Finger extends Player {
var colorOn:String;
public function Finger(){
colorOn = ""
}
function SetColor(colour:String):void {
this.colorOn = colour;
}
}
}
Output:
Is this where stack overflow accuros?
Error: Error #1023: Stack overflow occurred.
at flash.display::DisplayObject()
at flash.display::InteractiveObject()
at flash.display::DisplayObjectContainer()
at flash.display::Sprite()
at flash.display::MovieClip()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
at Finger()
at Player()
In order to create a Player
, you create four new Finger
objects... but each Finger
object is also a Player
(because Finger
extends Player
). Therefore creating each of those four Finger
objects requires creating four more Fingers
, etc... hence the stack overflow.
That's why it's happening - unfortunately we don't have enough context of what you're trying to achieve to suggest the right fix, necessarily. The simplest approach may well be to stop Finger
from extending Player
- are you sure it should be doing so?
See more on this question at Stackoverflow