import flash.display.Shape;
import flash.display.Graphics;
stage.addEventListener(Event.ENTER_FRAME, startAnim);
function startAnim(e:Event):void
{
var shape1:Shape = new Shape();
shape1.graphics.beginFill(0x333333,1);
shape1.graphics.drawRect(40,50,250,125);
shape1.graphics.endFill();
addChild(shape1); // this will add a shape of rectangle to stage
}
这是一个在舞台上创建矩形形状的非常简单的功能。好的但问题是我如何才能使用ActionScript将此 SHAPE 转换为 MOVIECLIP ,以便我可以将事件添加到同一个( shape1 )。< / p>
答案 0 :(得分:1)
使用MovieClip而不是Shape。 MovieClip还有一个Graphics对象。
import flash.display.MovieClip ;
//import flash.display.Graphics;//not needed
//stage.addEventListener(Event.ENTER_FRAME, startAnim); //remove enterframe
//function startAnim(e:Event):void { //no need for a handler
var shape1:MovieClip = new MovieClip();
shape1.graphics.beginFill(0x333333,1);
shape1.graphics.drawRect(40,50,250,125);
shape1.graphics.endFill();
addChild(shape1); // this will add a MovieClip of rectangle to stage
shape1.addEventListener(MouseEvent.MOUSE_DOWN, dragShape);
function dragShape(E:MouseEvent)
{
shape1.startDrag()
}
shape1.addEventListener(MouseEvent.MOUSE_UP, dropShape);
function dropShape(E:MouseEvent)
{
shape1.stopDrag()
}
//} no need for that either :)
请注意,因此,您的函数在ENTER_FRAME =每秒25次或更多次上调用,因此您将每秒创建并添加一个剪辑25次或更多次 +在函数中本地创建引用,因此一旦创建了对象,就无法从外部访问“shape1”。
答案 1 :(得分:0)
我认为你不能将Shape转换为MovieClip。你可以做的是创建一个MovieClip类,并在构造函数中生成Shape对象,并将其添加到MovieClip。
public class Car extends MovieClip {
private var shape1:Shape = new Shape();
public function Car() {
shape1.graphics.beginFill(0x333333,1);
shape1.graphics.drawRect(40,50,250,125);
shape1.graphics.endFill();
addChild(shape1); // this will add a shape of rectangle to stage
}
}
Shape也有事件。
但由于它没有从 InteractiveObject 扩展,因此无法处理输入。