我刚开始尝试学习Actionscript 3.我目前正在使用FlashDevelop作为我的IDE。虽然我可以使用其他语言编写代码,但AS3对我来说是一个新手(就像Flash一样)。
我创建了一个基础类:
package
{
import flash.display.Sprite;
public class BouncingBox extends Sprite
{
public function BouncingBox()
{
x = 10;
y = 10;
width = 100;
height = 100;
graphics.clear();
graphics.beginFill(0xD4D4D4); // grey color
graphics.drawRoundRect(0, 0, 100, 100, 10, 10); // x, y, width, height, ellipseW, ellipseH
graphics.endFill();
}
}
}
然后,从我的Main()类开始:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
public class Main extends Sprite
{
var mySprite:BouncingBox;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
mySprite = new BouncingBox();
addChild(mySprite);
var t:TextField = new TextField();
t.text = "Testing!";
addChild(t);
}
}
}
我期望发生的是文本出现在顶部(它做了),BouncingBox的实例出现在舞台上的10,10(不是)。
我错过了什么让这个精灵出现在舞台上?
答案 0 :(得分:1)
此。
width = 100;
height = 100;
当精灵为空时你设置它,它搞砸了它的变换矩阵。绘制矩形时,它将自己赋予sprite这个大小。如果您想稍后缩放它,请设置宽度和高度。
答案 1 :(得分:0)
您的对象在添加到显示列表之后才会真正初始化。 尝试添加以下代码。
package
{
import flash.display.Sprite;
public class BouncingBox extends Sprite
{
public function BouncingBox()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
x = 10;
y = 10;
//width = 100;
//height = 100;
graphics.clear();
graphics.beginFill(0xD4D4D4); // grey color
graphics.drawRoundRect(0, 0, 100, 100, 10, 10); // x, y, width, height, ellipseW, ellipseH
graphics.endFill();
}
}
}
此外,你可以附上你的盒子,然后初始化它的位置等。
mySprite = new BouncingBox();
addChild(mySprite);
mySprite.x = mySprite.y = 10;
另外,请考虑@alxx在答案中所写的内容。