2014-01-28 43 views
0

隨機生成的精靈,我做一個遊戲我的課,不同的化學元素作爲在屏幕的頂部精靈生成,然後倒下。製作不同的類型,我希望學生將鼠標放在特定類型上,具體取決於他們在遊戲中的位置。 我的問題是怎麼寫的刪除這些功能時,他們選擇正確?我嘗試了很多不同的方式,但遇到了很多麻煩。我寫,使每個元素是下面,然後我有一個單獨的函數向下移動創建的所有精靈的代碼的例子。刪除與Mouse.Event

var spriteArray:Array = new Array(); 
var halogenArray:Array = new Array("F", "Cl", "Br", "I"); 
var rndnum:Number = Math.random();    
//Halogens 
if (rndnum < 0.05) 
{ 
var halo:Sprite = new Sprite(); 
halo.graphics.beginFill(0x00FF00, 1); 
halo.graphics.drawCircle(7.5, 7.5, 15); 
halo.graphics.endFill(); 
halo.addEventListener(MouseEvent.MOUSE_OVER, removeElement); 
halo.x = Math.random()*500 + 50; 
halo.y = -18; 
var textField = new TextField(); 
textField.text = halogenArray[int(Math.random()*4)]; 
textField.width = 30; 
textField.height = 30; 
textField.x = (15 - textField.textWidth)/2; // center it horizontally 
textField.y = (15 - textField.textHeight)/2; // center it vertically 
halo.addChild(textField); 
spriteArray.push(halo); 
addChild(halo); 
} 

回答

0

我相信你正在尋找的東西是這樣的:

//give your textfields a name, it isn't totally necessary as we can do getChildAt(0) 
//but it's more readable, and if you decide to add more children before you 
//add the text field, then this will still work 

var textField = new TextField(); 
textField.text = halogenArray[int(Math.random()*4)]; 
textField.width = 30; 
... 
textField.name = "haloTx"; //for tracking later 

//assuming you have some variable set to the correct answer 
var correctAnswer:String = "F"; 

function removeElement(e:MouseEvent):void { 
    var element:TextField = (e.target as Sprite).getChildByName("haloTx"); 

    //if we have the correct element, remove from parent and list 
    if (element && element.text == correctAnswer) { 
     var index:int = spriteArray.indexOf(e.target as Sprite); 
     removeChild(spriteArray.splice(index, 1)[0]); 
    } 
} 

雖然@VBCPP是正確的,這樣做,在一個單獨的類肯定是​​組織上的最佳途徑。這可能看起來像:

class ElementSprite extends Sprite { 
    public var textField:TextField; 

    //pass shapeArgs as string, so say a circle at x=7.5, y=7.5, and radius=15 -- shapeArgs = "7.5, 7.5, 15" 
    public function ElementSprite(element:String, drawShape:String="Circle", shapeArgs:String="7.5, 7.5, 15", fillColor:uint=0x00FF00) { 
     //set textfield properties etc. or textFormat 
     textField = new TextField(); 
     textField.text = element; 
     addChild(textField); 

     //if you passed some arguments to draw our shape 
     if (shapeArgs != "") { 
      graphics.beginFill(fillColor); 
      graphics[ "draw" + drawShape ].apply(this, shapeArgs.split(",")); 
     } 
    } 

    public function get currentElement():String { return textField.text } 
} 

然後你就在使用它,像這樣的if語句if (rndnum < 0.05)

var elementSprite:ElementSprite = new ElementSprite("A"); 
//elementSprite.x = set your x; 
//elementSprite.y = set your y; 
addChild(elementSprite); 

將被替換,如果聲明目前所有的代碼。如果您有任何問題可以隨時發表評論,這就是一個有效的例子。

0

在什麼時候,你掙扎?

我假設它是在確定類型的鹵素。 在你刪除功能我假定你已經想通了需要的類型,那麼您需要把它比作 element.getChildAt(0).text區段 ,你會通過在spriteArray跨越每一個元素或者循環,或者使用獲得元素mouseEvent的目標

我的建議是使用鹵素類來包含文本字段和一個載體來保存對象。然後,獲取該類型將比較容易,而不是搜索精靈的匿名孩子。