我想编写一个程序,该程序将使用键盘箭头键运行动画进行导航。请帮忙。此 ActionScript 是编程语言
问问题
62 次
1 回答
0
Flash Pro 中的示例设置:
你可以从我的网站下载这个项目:
Flash Pro CS5.5:http
://www.jasonsturges.com/labs/stack-overflow/examples/hero/Hero-cs5.5.fla
Flash Pro CS5:http ://www.jasonsturges.com/labs/stack -overflow/examples/hero/Hero-cs5.fla
SWF:http ://www.jasonsturges.com/labs/stack-overflow/examples/hero/Hero.swf
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.DisplayObject;
// put walls in collection
var walls:Array = [ northWall, southWall, eastWall, westWall ];
// animtion listener
addEventListener(Event.ENTER_FRAME, frameHandler);
// key listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydownHandler);
// keyboard handling
function keydownHandler(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT:
moveWalls(-5, 0);
break;
case Keyboard.LEFT:
moveWalls(5, 0);
break;
case Keyboard.UP:
moveWalls(0, -5);
break;
case Keyboard.DOWN:
moveWalls(0, 5);
break;
}
}
// utility to move all walls
function moveWalls(x:Number, y:Number):void
{
for each (var wall:DisplayObject in walls)
{
wall.x += x;
wall.y += y;
}
}
// animation handler
function frameHandler(event:Event):void
{
for each (var wall:DisplayObject in walls)
{
if (hero.hitTestObject(wall))
{
switch (wall)
{
case northWall:
moveWalls(0, -5);
break;
case southWall:
moveWalls(0, 5);
break;
case westWall:
moveWalls(-5, 0);
break;
case eastWall:
moveWalls(5, 0);
break;
}
}
}
}
于 2012-06-24T19:31:34.287 回答