基本 Flash/AS3 扩展 Sprite。我缺少什么?
我刚刚开始尝试学习 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 处舞台(实际上没有)。
为了让这个精灵出现在舞台上,我错过了什么?
I'm right at the beginning of trying to learn Actionscript 3. I'm currently working with FlashDevelop as my IDE. Whilst I can code in other languages, AS3 is very new to me (as is Flash).
I have created a basic class:
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();
}
}
}
Then, from my Main() class, I do:
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);
}
}
}
What I expected to happen was the text to appear at the top (which it does), and the instance of BouncingBox to appear at 10, 10 on the stage (which is doesn't).
What have I missed to make this sprite appear on the stage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这。
当精灵为空时设置它,它会弄乱它的变换矩阵。当绘制矩形时,它会自己给精灵这个大小。如果您想稍后缩放,请设置宽度和高度。
This.
You set it when sprite is empty, and it messed up its transformation matrix. When rectangle is drawn, it will give sprite this size by itself. Set width and height if you want to scale it later.
直到附加到显示列表后,您的对象才真正初始化。
尝试添加以下代码。
另外,您可以附加您的盒子,然后初始化其位置等。
另外,请考虑@alxx 在他的答案中写的内容。
Your object is not really initialized until after is attached to the display list.
Try to add the following code.
Also, you could attach your box, and then initialize its position, etc.
Also, consider what @alxx wrote in his answer.