我收到了堆栈溢出错误,我不确定堆栈溢出错误是什么或者如何解决它。我已经在网上试图了解堆栈溢出但我很丢失。如果有人能够向我解释我的代码中的哪些内容会导致溢出,那就太棒了
这是我的英雄课程:
package {
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
public class Hero extends DocumentMainNew
{
public var health:Number;
public var mana:Number
public var vx:Number;
public var vy:Number;
public var allowJump:Boolean;
public var collision:Boolean;
public function Hero():void
{
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
public function enterFrameHandler(e:Event):void
{
//Gravitates the player
vy += 2;
//Moves the player
angela.x += vx;
angela.y += vy;
//processes collisions
processCollisions();
//scrolls the stage
scrollStage();
}
public function keyDownHandler(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37: //left arrow
vx = -7;
break;
case 39: //right arrow
vx = 7;
break;
case 38: //up arrow
if(allowJump)
{
vy = -20;
}
break;
default:
}
}
public function keyUpHandler(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37:
vx = 0;
break;
case 39:
vx = 0;
break;
case 38:
break;
default:
}
}
public function processCollisions():void
{
//detects when player is falling
if(vy > 0)
{
//respawns the player if they fell of the stage
if(angela.y > stage.stageHeight)
{
angela.x = _startMarker.x;
angela.y = _startMarker.y;
_boundaries.x = 0;
_boundaries.y = 0;
vy = 0;
}
//otherwise, processes collisions with boundaries
while(_boundaries.hitTestPoint(angela.x, angela.y, true))
{
allowJump = true;
angela.y -= 0.1
vy = 0;
}
}
}
}
}
这是我的主要文件:
package {
import flash.display.MovieClip
import flash.events.Event
import flash.events.KeyboardEvent
import Hero
public class DocumentMainNew extends MovieClip
{
public var angela:Hero = new Hero;
public var enemy1:Enemy = new Enemy;
public var _boundaries:Boundaries;
var _startMarker:StartMarker;
public function DocumentMainNew():void
{
_startMarker.visible = false;
addChild(angela);
addChild(enemy1);
angela.health = 100;
angela.mana = 100;
stage.focus = stage;
}
public function scrollStage():void
{
_boundaries.x += (stage.stageWidth * 0.5) - angela.x;
angela.x = stage.stageWidth * 0.5;
enemy1.x = _boundaries.x + 30;
_powerup.x = _boundaries.x - 200;
}
}
}
继承错误:
错误:错误#1023:发生堆栈溢出。
在Hero()的DocumentMainNew()[/ Users / s2111908 / Desktop / Game / DocumentMainNew.as:9] [/ Users / s2111908 / Desktop / Game / Hero.as:16]
DocumentMainNew的第9行是:public var angela:Hero = new Hero; 英雄的第16行是:公共功能英雄():无效
它在DocumentMainNew()和Hero()上多次说,但我并不想粘贴所有这些。如果重要次数让我知道,我会给你一张照片。我是AS3的新手,所以如果有任何关于如何改进我的代码的提示,请告诉我。
答案 0 :(得分:0)
<强>第一即可。请了解如何在发布时格式化代码。整个代码块必须缩进。有一个 {} 按钮。
<强>第二即可。你的错误是 Hero 扩展 DocumentMainNew 类。问题不在于扩展,而是在 DocumentMainNew 变量声明区域中调用 new Hero 。让我们看看它是怎么回事。
我不知道你为什么从 DocumentMainNew 类继承了 Hero 类,但这是一个完全错误的举动。您应该解释您尝试解决的问题。它应该以一种非常不同的方式解决。