ios - AS3 - 使用循环动态更改 Sprite 属性不起作用

标签 ios for-loop sprite actionscript-3

我正在尝试使用循环更改各种 Sprite 的属性。但是我收到此错误消息,但我不知道如何修复它: ReferenceError:错误#1069:在字符串上找不到属性转换,并且没有默认值。 在 LevelScreen/LevelUnlockedColor()[/Users/jakub/Cloud Drive/Documents/JH_WORK/IMPOSSIBLE_SCREEN/LevelScreen.as:232] 在 LevelScreen()[/Users/jakub/Cloud Drive/Documents/JH_WORK/IMPOSSIBLE_SCREEN/LevelScreen.as:31] 在 Function/MainMenu/LoadMenu/taphandler2Click()[/Users/jakub/Cloud Drive/Documents/JH_WORK/IMPOSSIBLE_SCREEN/MainMenu.as:94]

代码如下:

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);
        }

    }
}

最佳答案

像这样更改您的 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
        }

通过这种方式,您实际上可以对 Sprite 进行转换,而不是对它们的名称进行转换。如果您还需要在舞台上添加这些对象,请将此行放在 trace 之前:

addChild(boxes[i]);

问题编辑后更新。 好的,尝试更改 LevelUnlockedColor 函数中的 for 循环,如下所示:

 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;

        }

关于ios - AS3 - 使用循环动态更改 Sprite 属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16956741/

相关文章:

python - 我将如何使用 .join 和 for 循环制作随机十六进制代码生成器?

javascript - 结合 for 循环与 setInterval()

ios - 在这种情况下如何避免 Optional.None 错误?

ios - 通过为 Swift 中的每个字符提供两种可能性来比较字符串

c++ - 如何遍历 C++ 中的对象列表?

java - 在 Libgdx 中创建另一个纹理时,我应该调用 dispose() 纹理吗?

swift - 如果 Sprite 的下半部分很重要,如何检查碰撞(想想有茎的樱桃)

asp.net - Sprite 图像质量差

通过用户代理检测 iPhone 应用程序操作系统

ios - Realm.io 删除 Realm 文件