当您按下W,A,S和D时,我正在使用此代码移动图像。
package
{
import com.adobe.tvsdk.mediacore.CustomRangeType;
import flash.display.Bitmap;
import flash.display.BitmapEncodingColorSpace;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.NetStatusEvent;
import flash.events.TimerEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.system.ImageDecodingPolicy;
import flash.ui.Keyboard;
import flash.utils.ByteArray;
import flash.utils.Timer;
public class Main extends Sprite {
public var xcoord:int = 50;
public var ycoord:int = 50;
public var movingUp:Boolean;
public var movingDown:Boolean;
public var movingLeft:Boolean;
public var movingRight:Boolean;
public var onFrontPage:Boolean;
public var onDiner:Boolean;
public var clock:Timer = new Timer(15, 0);
// Image Initializations
public var player:Bitmap = new Assets.hariidle1 as Bitmap;
public var dinerBackground:Bitmap = new Assets.diner as Bitmap;
public var hariIdle1:Bitmap = new Assets.hariidle1 as Bitmap;
public var hariIdle2:Bitmap = new Assets.hariidle2 as Bitmap;
public function Main():void {
if (stage){
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
// Set booleans to true
onFrontPage = true;
addChild(Assets.video);
var nc:NetConnection = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS , onConnect);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR , trace);
var metaSniffer:Object=new Object();
nc.client=metaSniffer;
metaSniffer.onMetaData=getMeta;
nc.connect(null);
}
public function init(e:Event = null):void {
stage.removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
clock.addEventListener(TimerEvent.TIMER, gameLoop);
clock.start();
}
private function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore error
}
private function getMeta (mdata:Object):void {
Assets.video.width=1280;
Assets.video.height=720;
};
private function onConnect(e:NetStatusEvent):void {
if (e.info.code == 'NetConnection.Connect.Success') {
trace(e.target as NetConnection);
var ns:NetStream = new NetStream(e.target as NetConnection);
ns.client = {};
var file:ByteArray = new Assets.bytes();
ns.play(null);
ns.appendBytes(file);
Assets.video.attachNetStream(ns);
}
}
private function gameLoop(evt:Event):void {
// Draw diner background
if (!onFrontPage && !onDiner){
onDiner = true;
addChild(dinerBackground);
addChild(player);
}
if (!onFrontPage){
removeChild(Assets.video);
}
if (movingUp){
player.y += 50;
moveUp();
}
if (movingDown){
player.y -= 50;
moveDown();
}
if (movingLeft){
player.x += 50;
moveLeft();
}
if (movingRight){
player.x -= 50;
moveRight();
}
}
public function moveUp():void {
player.y += 50;
}
public function moveDown():void {
player.y -= 50;
}
public function moveLeft():void {
player.x += 50;
}
public function moveRight():void {
player.x -= 50;
}
public function onKeyDown(e:KeyboardEvent):void {
if (onFrontPage){
onFrontPage = false;
}
if (e.keyCode == 87){
movingUp = true;
}
if (e.keyCode == 83){
movingDown = true;
}
if (e.keyCode == 65){
movingLeft = true;
}
if (e.keyCode == 68){
movingRight = true;
}
}
public function onKeyUp(e:KeyboardEvent):void {
if (e.keyCode == 87){
movingUp = false;
}
if (e.keyCode == 83){
movingDown = false;
}
if (e.keyCode == 65){
movingLeft = false;
}
if (e.keyCode == 68){
movingRight = false;
}
}
}
}
还有另一个名为Assets的类,它初始化了所有图像。按下W,A,S和D时,图像“播放器”不移动。
按箭头键如何让图像移动?
编辑:我已将代码更改为建议的内容。角色仍然不动。
答案 0 :(得分:0)
你的旗帜全都搞砸了。您的代码应在keyUP事件上将其设置为false
,并在keydown事件上将其设置为true
。此外,您还在keydown上添加了两个侦听器,一个将标志设置为true,另一个设置为false。下定决心!
作为旁注,不需要在游戏循环中调用addChild()
,这些应该在侦听器中处理半永久性对象,如玩家。
最后,你在构造函数中调用stage
,点击"舞台不可用"并使你的听众不能完全工作。您应该将此代码用作Main()
中的唯一代码:
public function Main():void {
if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init);
}
你的init
函数应该像这样开始:
public function init(e:Event=null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
...然后包含Main
中的所有代码。一旦你完成了所有这些,你可能会让你的运动工作。
最后,永远不要进行货物编码,并始终考虑您的代码所做的以及何时调用,甚至在您进行编译之前。