2013-06-06 51 views
0

我試圖使用循環更改各個精靈的屬性。但是,我收到此錯誤消息,我不知道如何解決它: ReferenceError:錯誤#1069:在字符串中找不到屬性轉換,並且沒有默認值。在LevelScreen()[/用戶/ jakub/Cloud Drive/Documents/JH_WORK/IMPOSSIBLE_SCREEN/LevelScreen上的LevelScreen/LevelUnlockedColor()[0128]爲:31] 在功能/ MainMenu的/ LoadMenu/taphandler2Click()[/用戶/的Jakub /雲驅動器/文檔/ JH_WORK/IMPOSSIBLE_SCREEN/MainMenu.as:94]AS3 - 使用循環動態更改精靈屬性將不起作用

下面是代碼:

public function LevelUnlockedColor() 
    { 
     mySharedObject.data.Unlocked = 3; 
     mySharedObject.flush(); 
     var boxes:Array = []; 
     for (var i:int = 0; i<mySharedObject.data.Unlocked; i++) 
     { 
      var spriteName:Sprite = new Sprite(); 
      spriteName.name = "LVL" + i;// "sprite_0" "sprite_1" ... 

      boxes[i] = spriteName.name; 
      boxes.push(spriteName); 

      var trans:ColorTransform = boxes[i].transform.colorTransform; 
      trans.color = uint(0xd982ab); 
      boxes[i].transform.colorTransform = trans; 

      trace(boxes[i]); //output LVL0, LVL1, LVL2 
     } 

    } 

謝謝。

這裏是代碼的其餘部分:

package 

{

import flash.display.*; 
import flash.ui.*; 
import flash.events.*; 
import flash.filesystem.*; 
import flash.net.URLLoader; 
import flashx.textLayout.accessibility.TextAccImpl; 
import flash.net.*; 
import flash.geom.*; 


public class LevelScreen extends Sprite 
{ 

    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; 
    public var LVL1:Sprite = new Sprite ; 
    public var LVL2:Sprite = new Sprite ; 
    public var LVL3:Sprite = new Sprite ; 
    public var LVL4:Sprite = new Sprite ; 
    public var LVL5:Sprite = new Sprite ; 
    public var LVL6:Sprite = new Sprite ; 

    public var mySharedObject:SharedObject = SharedObject.getLocal("LevelsSaved"); 

    public function LevelScreen() 
    { 

     DrawLevels(); 
     LevelUnlockedColor(); 

     var PauseBtn:MainMenu = new MainMenu(); 
     PauseBtn.GamePaused(); 
     addChild(PauseBtn); 

     LVL1.addEventListener(TouchEvent.TOUCH_TAP,taphandler1); 
     LVL2.addEventListener(TouchEvent.TOUCH_TAP,taphandler2); 
     LVL2.addEventListener(MouseEvent.CLICK,taphandler2Click); 



     LevelCheck(); 


    } 

    public function DrawLevels() 
    { 
     LVL1.graphics.beginFill(0xe6e7e8); 
     LVL1.graphics.drawRect(50,90,260,260); 
     addChild(LVL1); 


     LVL2.graphics.beginFill(0xd1d3d4); 
     LVL2.graphics.drawRect(330,90,260,260); 
     addChild(LVL2); 


     LVL3.graphics.beginFill(0xbcbec0); 
     LVL3.graphics.drawRect(50,370,260,260); 
     addChild(LVL3); 


     LVL4.graphics.beginFill(0xa7a9ac); 
     LVL4.graphics.drawRect(330,370,260,260); 
     addChild(LVL4); 


     LVL5.graphics.beginFill(0x939598); 
     LVL5.graphics.drawRect(50,650,260,260); 
     addChild(LVL5); 


     LVL6.graphics.beginFill(0x808285); 
     LVL6.graphics.drawRect(330,650,260,260); 
     addChild(LVL6); 
    } 

    public function HideAll() 
    { 

     removeChild(LVL1); 
     removeChild(LVL2); 
     removeChild(LVL3); 
     removeChild(LVL4); 
     removeChild(LVL5); 
     removeChild(LVL6); 
    } 


    public function taphandler1(event:TouchEvent):void 
    { 

     var LoadLVL1:LEVEL_01 = new LEVEL_01(); 
     LoadLVL1.drawLVL_01(); 
     addChild(LoadLVL1); 
     HideAll(); 
    } 



    public function taphandler2(event:TouchEvent):void 
    { 

     if (mySharedObject.data.Unlocked >= 1) 
     { 

      var LoadLVL2:LEVEL_02 = new LEVEL_02(); 
      LoadLVL2.LoadLevel2(); 
      addChild(LoadLVL2); 
      HideAll(); 
     } 
     else 
     { 
      trace("LEVEL 02 is locked"); 
     } 
    } 

    public function taphandler2Click(event:MouseEvent):void 
    { 

     if (mySharedObject.data.Unlocked >= 1) 
     { 

      var LoadLVL2:LEVEL_02 = new LEVEL_02(); 
      LoadLVL2.LoadLevel2(); 
      addChild(LoadLVL2); 
      HideAll(); 
     } 
     else 
     { 
      trace("LEVEL 02 is locked"); 
     } 
    } 




    public function LevelCheck() 
    { 


     if (mySharedObject.data.Unlocked >= 1) 
     { 

      trace("some levels unlocked already " + mySharedObject.data.Unlocked); 

     } 
     else 
     { 
      mySharedObject.data.Unlocked = 0; 
      mySharedObject.flush(); 
      trace("new sharedobject created " + mySharedObject.data.Unlocked); 
     } 
    } 

    public function LevelUnlockedColor() 
    { 

     mySharedObject.data.Unlocked = 3; 
     mySharedObject.flush(); 
     var boxes:Array = []; 
     for (var i:int = 1; i<mySharedObject.data.Unlocked; i++) 
     { 
      var spriteName:Sprite = new Sprite(); 
      spriteName.name = "LVL" + i;// LVL0 , LVL1 ... 

      boxes[i] = spriteName; 
      //boxes.push(spriteName); 

      var trans:ColorTransform = boxes[i].transform.colorTransform; 
      trans.color = uint(0xd982ab); 
      boxes[i].transform.colorTransform = trans; 
      addChild(boxes[i]); 
      trace(boxes[i].name);//output LVL0, LVL1, LVL2 
      trace(trans); 
     } 

    } 
} 

}

回答

1

更改for循環是這樣的:

for (var i:int = 0; i<mySharedObject.data.Unlocked; i++) 
     { 
      var spriteName:Sprite = new Sprite(); 
      spriteName.name = "LVL" + i;// LVL0 , LVL1 ... 

      boxes[i] = spriteName; 
      //boxes.push(spriteName); 

      var trans:ColorTransform = boxes[i].transform.colorTransform; 
      trans.color = uint(0xd982ab); 
      boxes[i].transform.colorTransform = trans; 

      trace(boxes[i].name); //output LVL0, LVL1, LVL2 
     } 

這樣你會激勵盟友對精靈進行轉換,而不是他們的名字。如果你還需要添加在舞臺上這些對象,把這個線你trace前:問題編輯後

addChild(boxes[i]); 

更新。 好吧,試圖改變for循環在這樣的LevelUnlockedColor功能:

for (var i:int = 1; i<mySharedObject.data.Unlocked; i++) 
     { 
      var lvl:Sprite = this["LVL" + i]; 

      var trans:ColorTransform = lvl.transform.colorTransform; 
      trans.color = uint(0xd982ab); 
      lvl.transform.colorTransform = trans; 

     } 
+0

非常感謝你。代碼似乎工作正常(沒有錯誤,耶)。然而,精靈的顏色不會改變。所有的精靈都已經可見。我在這裏要做的是當你解鎖這個特定的關卡時,改變一個瓷磚的顏色(新的關卡)。你能想到什麼明顯的東西?如果你想我可以在這裏發佈剩餘的代碼... – cubeec

+0

@cubeec是的,如果你更新你的問題更新代碼 – Cherniv

+0

請給我2分鐘請:-)謝謝! – cubeec