互动对象?

问题描述:

有没有一种简单的方法来创建对象,您可以在动作中与鼠标进行交互。像几行代码?互动对象?

+0

这个问题不是很清楚。你能否更详细地解释你想如何与他们交互? – 2011-04-06 20:52:51

var sprite:Sprite = new Sprite(); // create a new sprite. 
sprite.graphics.beginFill(0); // set fill color to black 
sprite.graphics.drawRect(0,0,100,100); // draw a square 
addChild(sprite); // add the sprite to the display list making it appear on screen. 
sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); // add responders for mouse interaction. 
sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 

function onMouseDown(event:MouseEvent):void { 
    // When the mouse button is released over the object, this code executes. 
    Sprite(event.target).startDrag(); // make the object follow the mouse. 
} 

function onMouseUp(event:MouseEvent):void { 
    // When the mouse button is pressed over the object, this code executes. 
    Sprite(event.target).stopDrag(); // make the object stop following the mouse. 
}