参考网上的一个例子,自己实现了相应的操作,具体看代码
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.text.TextField;
public class MovieClipExample extends MovieClip {
public var bl : Boolean;
public var ball : MovieClip;
public function MovieClipExample() {
ball = new MovieClip();
ball.graphics.beginFill( 0xCCFF00 );
ball.graphics.drawCircle( 50, 60, 40 );
ball.graphics.endFill();
addChild( ball );
stage.addEventListener( KeyboardEvent.KEY_DOWN, KeyDown );
stage.addEventListener( KeyboardEvent.KEY_UP, KeyUp );
}
public function KeyDown( e : KeyboardEvent ) : void
{
if ( e.ctrlKey && e.keyLocation == 1 )
{
bl = true;
}
if ( bl )
{
switch( e.keyCode )
{
case 37 :
ball.x -= 20;
break;
case 38:
ball.y -= 20;
break;
case 39:
ball.x += 20;
break;
case 40 :
ball.y += 20;
break;
}
}
}
public function KeyUp( e : KeyboardEvent ) : void
{
if ( e.ctrlKey == false )
{
bl = false;
}
}
}
}
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.text.TextField;
public class MovieClipExample extends MovieClip {
public var bl : Boolean;
public var ball : MovieClip;
public function MovieClipExample() {
ball = new MovieClip();
ball.graphics.beginFill( 0xCCFF00 );
ball.graphics.drawCircle( 50, 60, 40 );
ball.graphics.endFill();
addChild( ball );
stage.addEventListener( KeyboardEvent.KEY_DOWN, KeyDown );
stage.addEventListener( KeyboardEvent.KEY_UP, KeyUp );
}
public function KeyDown( e : KeyboardEvent ) : void
{
if ( e.ctrlKey && e.keyLocation == 1 )
{
bl = true;
}
if ( bl )
{
switch( e.keyCode )
{
case 37 :
ball.x -= 20;
break;
case 38:
ball.y -= 20;
break;
case 39:
ball.x += 20;
break;
case 40 :
ball.y += 20;
break;
}
}
}
public function KeyUp( e : KeyboardEvent ) : void
{
if ( e.ctrlKey == false )
{
bl = false;
}
}
}
}