2013-05-05 96 views
0

这是我得到一个StackOverflow错误的代码。我不完全确定它有什么问题。代码是即插即用的,所以你可以插入并测试你的自我。有人可以帮助我吗?我基本上是从一个数组中产生两个不同的对象,并试图摆脱被点击的对象,然后我把这个对象放到不同的数组中。StackOverflow错误AS3

import flash.sampler.NewObjectSample; 
import flash.display.Sprite; 
import flash.events.MouseEvent; 

var eating_breakfast:Sprite; 
var walking:Sprite; 
var swimming:Sprite; 
var art:Sprite; 
var choices:Array = new Array(); 

//Sprite Creation 
eating_breakfast = new Sprite(); 
eating_breakfast.graphics.beginFill(0xE39D43); 
eating_breakfast.graphics.drawRect(0,0,50,50); 
eating_breakfast.graphics.endFill(); 
eating_breakfast.x = 50; 
eating_breakfast.y = 50; 


walking = new Sprite(); 
walking.graphics.beginFill(0xC3266C); 
walking.graphics.drawRect(0,0,50,50); 
walking.graphics.endFill(); 
walking.x = 100; 
walking.y = 100; 


swimming = new Sprite(); 
swimming.graphics.beginFill(0x48AFD1); 
swimming.graphics.drawRect(0,0,50,50); 
swimming.graphics.endFill(); 
swimming.x = 150; 
swimming.y = 150; 


art = new Sprite(); 
art.graphics.beginFill(0xafdb44); 
art.graphics.drawRect(0,0,50,50); 
art.graphics.endFill(); 
art.x = 200; 
art.y = 200; 

//adding sprites into array 
choices.push(eating_breakfast); 
choices.push(walking); 
choices.push(swimming); 
choices.push(art); 


var indexcount = 0; 
var randomize:Number; 
var storageArray: Array = new Array(); 
civilizedorder(); 
randomizedorder(); 
this.addEventListener(MouseEvent.CLICK,switchpic); 

//pick the target generated object 
function switchpic(t:MouseEvent) 
{ 
    //for index count 
    // this works as a target so if your mouse target is the object generated by indexcount this will initiate 
    if (t.target == choices[indexcount]) 
    { 
     storageArray.push(choices[indexcount]); 
     removeChild(choices [indexcount]); 
     removeChild(choices [randomize]); 
     choices.splice(indexcount,1); 
     goNext(); 

    } 
    // for randomize 
    if (t.target == choices[randomize]) 
    { 
     // this works as a target so if your mouse target is the object generated by randomize this will initiate 
     storageArray.push(choices[randomize]); 
     removeChild(choices [indexcount]); 
     removeChild(choices [randomize]); 
     choices.splice(randomize,1); 
     indexcount++; 

     goNext(); 
    } 
} 

//generates the index count object 
function civilizedorder() 
{ 
    trace("The Index count is" + indexcount); 
    addChild(choices [indexcount]); 
    choices[indexcount].x = 300; 


} 
trace("The number of choices in the choice array is " + choices.length); 
//generates the randomized object 
function randomizedorder() 
{ 

    randomize = Math.floor(Math.random() * choices.length); 
    trace("the random number is" + randomize); 
    if (randomize == indexcount) 
    { 
     randomizedorder(); 
    } 
    else 
    { 
     addChild(choices [randomize]); 
    } 

} 


function goNext() 
{ 
    trace("The storagearray has " + (storageArray.length)); 
    if (choices.length < 0 || choices.length > 0) 
    { 
     if (indexcount > choices.length-1) 
     { 
      indexcount = choices.length - 1; 
     } 
     civilizedorder(); 
     randomizedorder(); 
    } 
} 

回答

2

堆栈溢出意味着你有太多的递归。在这种情况下,当choices.length为1并且indexcount为0(即第一次调用goNext)时,可能在randomizedorder函数中,它会造成无限循环。

你需要重新考虑这个程序的结构。尽可能避免递归。循环更好,但你也不需要它们;修复一个功能:

randomize = Math.floor(Math.random() * (choices.length - 1)); 
if (randomize >= indexcount) { 
    randomize ++; 
} 

你仍然可能会得到奇怪的结果,因为它没有被像您期望的调用,但堆栈溢出应该消失。

+0

哦,我非常感谢:D我真的在圈子里四处走动。这使得它更清晰。 – tailedmouse 2013-05-05 18:32:39