我正在尝试使用循环更改各种 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/