我是个新手。下面是创建一个StageObject类的尝试,我可以设置宽度、高度、xy和背景颜色。
package
{
import flash.display.MovieClip;
public class StageObjects extends MovieClip
{
public function StageObjects()
{
// constructor code
}
public function setUpStageObject(w:int, h:int, X:int, Y:int, color:Number):void
{
this.width = w;
this.height = h;
this.x = X;
this.y = Y;
this.cacheAsBitmap = true;
this.graphics.beginFill(color,1);
this.graphics.drawRect(0,0,w,h);
this.graphics.endFill();
this.opaqueBackground = color;
trace("parameters: " + w + " - " + h + " - " + X + " - " + Y + " - " + color);
}
/*~~~ adjust position and scale functions ~~~*/
public function adjustXY(ch:Object, par:Object):void
{
var w = par.width;
var h = par.height;
ch.x = par.x + (w - ch.width) / 2;
ch.y = par.y + (h - ch.height) / 2;
}
public function adjustWH(ch:Object, par:Object):void
{
var w = par.width;
var h = par.height;
}
}
}在主时间线(Flash)中,我这样做:
var titleBkg:StageObjects = new StageObjects();
titleBkg.setUpStageObject(imageBoxWidth, titleBkgHeight, -1, imageBoxHeight +1, 0x589199);
this.addChild(titleBkg);但它并没有出现。我有提到“这个”吗?不对?
发布于 2013-03-28 23:54:15
您没有使用addChild正确创建图形并将其设置为父图形。
实际上,你的舞台看起来像这样:
Stage ¬
0: MainTimeline:MovieClip ¬
0: instance1:StageObjects它需要看起来像这样:
Stage ¬
0: MainTimeline:MovieClip ¬
0: instance1:StageObjects ¬
0: instance1:Shape图形调用应该在形状上调用,而不是在电影剪辑上调用。您也可以在第一次呼叫时使用一条线路而不是两条线路进行此设置。
package {
import flash.display.MovieClip;
public class StageObjects extends MovieClip {
public function StageObjects(w:int, h:int, X:int, Y:int, color:uint) {
// Constructor
this.x = X;
this.y = Y;
var rect:Shape = new Shape();
rect.graphics.beginFill(color,1);
rect.graphics.drawRect(0,0,w,h);
rect.graphics.endFill();
addChild(rect);
trace("parameters: " + w + " - " + h + " - " + X + " - " + Y + " - " + color);
}
public function adjustXY(ch:Object, par:Object):void {
// adjust position and scale functions
var w = par.width;
var h = par.height;
ch.x = par.x + (w - ch.width) / 2;
ch.y = par.y + (h - ch.height) / 2;
}
public function adjustWH(ch:Object, par:Object):void {
var w = par.width;
var h = par.height;
}
}
}并且创建对象将被简化为:
var titleBkg:StageObjects = new StageObjects(imageBoxWidth, titleBkgHeight, -1, imageBoxHeight +1, 0x589199);
this.addChild(titleBkg);发布于 2013-03-28 22:59:49
我想你已经在构造函数中声明了宽度、高度、图形等。在我的类代码中,我从来没有使用过'this.‘。如果变量被声明为类,则私有/公共编译器将不允许您声明同名的其他变量。所以你不必使用'this.',但使用'this.‘会更具可读性。(您将知道它是类变量)。
是否已将您的图形添加到stage (不是titleBkg,而是您在titleBkg对象中创建的对象)?;)
https://stackoverflow.com/questions/15684903
复制相似问题