2013-04-25 41 views
0

我想创建一个随机化的实例和一个数组中的对象的正确排序的实例,但由于某种原因它不能正常工作。 这是它的代码:Array重复不工作

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(); 
var i: Number = 0; 

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; 


choices.push (eating_breakfast); 
choices.push (walking); 
choices.push (swimming); 
choices.push (art); 


stage.addEventListener (MouseEvent.CLICK, switchpic); 
var indexcount = 0 ; 
var randomize : Number ; 
civilizedorder() ; 
randomizedorder(); 

function switchpic(d:MouseEvent){ 
removeChild (choices [indexcount - 1]); 
removeChild (choices [randomize]); 
civilizedorder(); 
randomizedorder(); 

} 

function civilizedorder() { 
    addChild (choices [indexcount]); 
    choices [indexcount].x = 300; 
    indexcount++; 
    trace (indexcount); 
} 
trace ("The number of choices in the choice array is " + choices.length); 

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

} 

请有人可以解释为什么发生这种情况。据我的分析,好像发生这种情况时indexcount ==随机..我该如何解决这个问题?

+0

你将不得不解释将发生什么,因为它不清楚。 – prototypical 2013-04-25 01:10:08

+0

哦,我的道歉..好吧,我有一个列表,我试图将该列表中的对象添加到阶段..但我需要2个实例随机生成一个,并且按照插入的顺序生成一个。他们都是同一个名单的一部分 – tailedmouse 2013-04-25 01:20:35

+0

你是说你想要每个副本?我仍然不明白你想要什么。 – prototypical 2013-04-25 01:26:50

回答

0

看起来像你有几个问题。 1你永远不会重置你的indexcount数字,这会导致它尝试访问你选择数组长度的长度的值(也就是说你允许indexcount大于choices.length)。 如果您尝试添加艺术两次,您也正确推测,您只在舞台上有一个实例。然而,当你去除精灵时,你并没有检查它是否存在或应该存在。

eating_breakfast = new Sprite(); 
eating_breakfast.name = "eating_breakfast"; 
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.name = "walking"; 
walking.graphics.beginFill(0xC3266C); 
walking.graphics.drawRect(0,0,50,50); 
walking.graphics.endFill(); 
walking.x = 100; 
walking.y = 100; 


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


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


choices.push(eating_breakfast); 
choices.push(walking); 
choices.push(swimming); 
choices.push(art); 

stage.addEventListener(MouseEvent.CLICK, switchpic); 
var indexcount = -1; 
var randomize:Number; 
civilizedorder(); 
randomizedorder(); 

function switchpic(d:MouseEvent) { 
    removeChild(choices[indexcount]); 
    if(choices[indexcount].name != choices[randomize].name){ 
     removeChild(choices[randomize]); 
    } 
    civilizedorder(); 
    randomizedorder(); 
}  

function civilizedorder() { 
    indexcount++; 
    if(indexcount == choices.length-1){ 
     indexcount = 0; 
    } 
    trace("adding " + choices[indexcount].name); 
    addChild(choices[indexcount]); 
    choices[indexcount].x = 300; 
} 
trace("The number of choices in the choice array is " + choices.length); 

function randomizedorder() { 
    randomize = Math.floor(Math.random() * choices.length); 
    trace("adding " + choices[randomize].name); 
    addChild(choices[randomize]); 

} 

对不起,我还添加了。名称的精灵,使它更容易看到发生了什么事情。

+0

哦,你可以告诉我如何检查目前在索引中的精灵是否不应该被随机挑选..我该怎么做?或者是我知道indexcount问题我忘了提及它..但我只是试图修复随机问题第一 – tailedmouse 2013-04-25 01:58:31

+0

尝试上述,对不起,我是新来这个网站,我仍然试图找出好方法添加代码:/ – 2013-04-25 02:12:09

+0

很多thx ..我有点不同意btu我从你的例子中找出它。 Thx非常:D – tailedmouse 2013-04-26 00:23:47

0

你需要做的是将你创建的实例包装在一个类或函数中。

public var choices:Array; 

function Main() 
{ 
    choices = [eatingBreakfast, walking]; 

    var randomIndex:int = Math.random() * choices.length; 
    var choice:Function = choices[randomIndex] as Function; 
    var yourSprite:Sprite = choice(); 

} 



function eatingBreakfast():Sprite 
{ 
    var instance:Sprite = new Sprite; 
    instance.graphics.beginFill(0xE39D43); 
    instance.graphics.drawRect(0,0,50,50); 
    instance.graphics.endFill(); 
    instance.x = 50; 
    instance.y = 50; 
    return instance; 
} 


function walking():Sprite 
{ 
    var instance:Sprite = new Sprite; 
    instance.graphics.beginFill(0xE39D43); 
    instance.graphics.drawRect(0,0,50,50); 
    instance.graphics.endFill(); 
    instance.x = 50; 
    instance.y = 50; 
    return instance; 
} 

这个例子是使用返回Sprite实例功能,但我会建议使用每个类型的类或返回基于某种标识的实例的功能。

+0

哦很酷很多thx! – tailedmouse 2013-04-26 00:24:04