Actionscript 3.0随机传送

时间:2016-06-23 09:38:35

标签: actionscript-3 flash flash-cs6

我使用Flash并决定将我的动画制作成一种游戏。所以我开始编码,而且我已经很远了。但是当我按下我选择的按键时,我遇到了一个小问题,它完成了我想要的操作,但随机地传送到角色的原始位置。

//FRAME 1
stop();
//--Developer William Richmond

import flash.events.KeyboardEvent;
import flash.events.TimerEvent;

stage.addEventListener(KeyboardEvent.KEY_UP,moveClipdown);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveClipup);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveClipright);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveClipleft);


//Attempting the border restriction code
function limitStageBorder(object:MovieClip) {
//1.
var objectHalfWidth:uint=object.width/2;
var objectHalfHeight:uint=object.height/2;

//2.
if (object.x+objectHalfWidth>stage.stageWidth) {
   object.x=stage.stageWidth-objectHalfWidth;
}
else if (object.x -  objectHalfWidth <0) {        
   object.x=0+objectHalfWidth;    
}     

//3.    
if (object.y-objectHalfHeight<0) {        
   object.y=0+objectHalfHeight;    
}    
else if (object.y + objectHalfHeight > stage.stageHeight) {
   object.y=stage.stageHeight-objectHalfHeight;
}
}
//down
function moveClipdown(event:KeyboardEvent):void
              {
                  if(event.keyCode == 83)
                  {
                      mcSquare1.y += 25
                  }
              }
//up
function moveClipup(event:KeyboardEvent):void
              {
                  if(event.keyCode == 32)
                  {
                      mcSquare1.y -= 10
                      mcSquare1.gotoAndPlay(20);
                  }
              }
            //right  
function moveClipright(event:KeyboardEvent):void{
                  if(event.keyCode == 68){
                      trace("I am running to the right");
                      mcSquare1.scaleX = 0.75;
                      mcSquare1.x += 20
                      gotoAndStop(10);
                  }
}

            //Left  
function moveClipleft(event:KeyboardEvent):void{
                  if(event.keyCode == 65)
                  {
                      trace("I am running to the left");
                      mcSquare1.scaleX = -0.75;
                      mcSquare1.x -= 20
                      gotoAndStop(10);
                  }
              }

FRAME 10

stop();
//--Developer William Richmond

import flash.events.KeyboardEvent;
import flash.events.TimerEvent;

stage.addEventListener(KeyboardEvent.KEY_UP,moveClipright10);
stage.addEventListener(KeyboardEvent.KEY_UP,moveClipleft10);

            //right  
function moveClipright10(event:KeyboardEvent):void{
                  if(event.keyCode != 68){
                      trace("I am standing");
                      mcSquare1.scaleX = 0.75;
                      mcSquare1.x += 20
                      gotoAndStop(1);
                  }
}

            //Left  
function moveClipleft10(event:KeyboardEvent):void{
                  if(event.keyCode != 65)
                  {
                      trace("I am standing");
                      mcSquare1.scaleX = -0.75;
                      mcSquare1.x -= 20
                      gotoAndStop(1);
                  }
              }

因此,如果有人可以帮忙,那将非常感谢!

1 个答案:

答案 0 :(得分:0)

直接在帧中编码可能会产生各种问题,尤其是在更改帧时。看看代码还不足以说明什么是错的,sinc ein时间轴编程可以在舞台上发生各种各样的事情。我强烈建议你在那里使用基类和代码。或者,如果您确实需要在时间轴中编码,请将所有内容放在一个框架中。