2013-05-05 2 views
0

Это код, для которого я получаю ошибку StackOverflow. Я не совсем уверен, что с ним не так. Код подключен и воспроизводится, так что c cna подключите его и проверьте сами. Может кто-нибудь, пожалуйста, помогите мне с этим? Я в основном размножаю 2 разных объекта из одного массива и пытаюсь избавиться от объекта, на который нажимают, и затем я помещаю этот объект в другой массив.Ошибка 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

Переполнение стека означает, что у вас слишком много рекурсии. В этом случае это возможно в функции randomizedorder, когда choices.length равно 1 и indexcount равно 0 (то есть, первый вызов goNext), он делает бесконечный цикл.

Вам необходимо переосмыслить структуру этой программы. Избегайте рекурсии, где это возможно. Петли лучше, но они вам тоже не нужны; чтобы исправить эту одну функцию:

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

Вы все еще, вероятно, получите странные результаты, так как он не вызывается, как вы ожидаете, но переполнение стека должен уйти.

+0

О, я очень благодарен: D Я действительно крутился по кругу. Это стало более ясным. – tailedmouse