actionscript3.0 相对运动小案例

 

分析:

上例中按键向右的话,小球向右边移动,按键向左的话,小球向左移动,当快移动到背景图的两端时候,小球还在移动,直到靠边。

其实当小球移动到舞台的中央过后,再向右移动的时候,背景图就同时开始在向左移动,直到小球移动到快要到背景图的另外一端,背景图才不移动。 从舞台一端开始,你移动小球到了舞台正中央的时候,你应该感觉到小球始终在正中央,此时的背景在向小球移动相反的方向移动。

你观看这个过程就像你观看一个人在扶手自动电梯上向电梯相反方向走步一样。电梯向上一台阶,人就向下一台阶。所以人始终还是在电梯原来那一阶,这个道理就和小球为什么始终在舞台中央一样,但电梯一直在向上运动。如果把人比作小球,电梯比作背景,那么背景就一直在向上运动,就会感觉背景里的小球没运动。 当然你也可以理解为,小球向下运动,而背景没运动。这个就看你选择什么作为参照物了。

程序中,背景图宽800像素,小球和背景分别用了两个视图容器来处理这种相对移动。

小球类:

Ball.as

   1:  package com.helloshp.view
   2:  {
   3:      import flash.display.Sprite;
   4:      
   5:      public class Ball extends Sprite
   6:      {
   7:          public function Ball()
   8:          {
   9:              super();
  10:              init();
  11:          }
  12:          
  13:          private function init():void{
  14:              this.graphics.beginFill(0xff0000);
  15:              this.graphics.drawCircle(0,0,20);
  16:              this.graphics.endFill();
  17:          }
  18:      }
  19:  }

背景类:

SceneView.as

   1:  package com.helloshp.view
   2:  {
   3:      import flash.display.Sprite;
   4:      
   5:      public class SceneView extends Sprite
   6:      {
   7:          [Embed(source="assets/bg.jpg")]
   8:          private var bg:Class;
   9:          
  10:          public function SceneView()
  11:          {
  12:              super();
  13:              init();
  14:          }
  15:          
  16:          private function init():void{
  17:              addChild( new bg() );
  18:          }
  19:      }
  20:  }

主视图类:

MainView.as

   1:  package com.helloshp
   2:  {
   3:      import com.helloshp.view.Ball;
   4:      import com.helloshp.view.SceneView;
   5:      
   6:      import flash.display.Sprite;
   7:      import flash.events.Event;
   8:      import flash.events.KeyboardEvent;
   9:      import flash.ui.Keyboard;
  10:      
  11:      public class MainView extends Sprite
  12:      {
  13:          private var ball:Ball;
  14:          private var sv:SceneView;
  15:          
  16:          private var isStart:Boolean;
  17:          private var speed:int;
  18:          private const LIMIT_LEFT:int=0;    //背景图左边界
  19:          private const LIMIT_RIGHT:int=800;//背景图右边界
  20:          
  21:          public function MainView()
  22:          {
  23:              super();
  24:              
  25:              
  26:              if(stage!=null){
  27:                  init();
  28:              }
  29:              else{
  30:                  this.addEventListener(Event.ADDED_TO_STAGE,addedHandler)
  31:              }
  32:   
  33:          }
  34:          
  35:          private function init():void{
  36:              ball = new Ball();
  37:              sv = new SceneView();
  38:              addChild( sv );      //把背景试图放到舞台上
  39:              sv.addChild(ball);//把小球放到背景试图里面,而不是放到舞台上
  40:              ball.y = 200;
  41:              
  42:              stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  43:              stage.addEventListener(KeyboardEvent.KEY_UP,kyUpHandler);
  44:              addEventListener(Event.ENTER_FRAME,enterFrameHandler);
  45:          }
  46:          
  47:          //键盘弹起就停止移动
  48:          private function kyUpHandler(e:KeyboardEvent):void{
  49:              isStart = false;
  50:          }
  51:          
  52:          //键盘按下就开始移动
  53:          private function keyDownHandler(e:KeyboardEvent):void{
  54:              switch( e.keyCode ){
  55:                  case Keyboard.RIGHT:
  56:                      isStart = true;
  57:                      speed = 10;
  58:                      break;
  59:                  case Keyboard.LEFT:
  60:                      isStart = true;
  61:                      speed = -10; 
  62:                      break;
  63:                  default:
  64:                      speed = 0;
  65:                      break;
  66:              }
  67:          }
  68:          
  69:          private function enterFrameHandler(e:Event):void{
  70:              if( !isStart) return;
  71:              
  72:              ball.x += speed;//小球在X轴加速移动                     
  73:              
  74:              
  75:              if(ball.x < LIMIT_LEFT)//如果小球的X坐标小于背景视图的左边界,就让小球一直停止在视图的左边界的位置
  76:                  ball.x = LIMIT_LEFT;
  77:              else if( ball.x > LIMIT_RIGHT  )//如果小球的X坐标大于背景视图的右边界,就让小球一直停止在视图的右边界位置
  78:                  ball.x = LIMIT_RIGHT;            
  79:              
  80:              
  81:              if( ball.x < stage.stageWidth/2 ){//如果小球X坐标运动小于舞台一半,就让场景视图停止在左边界
  82:                  sv.x = LIMIT_LEFT;
  83:              }
  84:              else if( ball.x > LIMIT_RIGHT - stage.stageWidth/2 ){//如果小球X坐标运动到 大于 背景视图右边界减去舞台宽度一半,就让背景视图右边界移动到舞台的右边界
  85:                  sv.x = -LIMIT_RIGHT + stage.stageWidth;
  86:              }
  87:              else{//当小球运动到舞台的一半的时候,背景视图 就在舞台上向小球相反的方向运动,
  88:                  sv.x = -(ball.x - stage.stageWidth/2);            
  89:              }
  90:          }
  91:          
  92:          private function addedHandler(e:Event):void{
  93:              init();
  94:          }
  95:      }
  96:  }

 

源代码下载:https://files.cnblogs.com/bigbigdotnet/relativeMove.rar

posted @ 2010-12-25 16:00  bigbigdotnet  阅读(1450)  评论(0编辑  收藏  举报