【问题标题】:Directly access Sprite after swf embed嵌入swf后直接访问Sprite
【发布时间】:2010-12-18 09:25:30
【问题描述】:

我有以下课程:

package  {
 import flash.display.Sprite;
 import flash.events.KeyboardEvent;
 import flash.ui.Keyboard;

 public class Ship extends Sprite {
  private var parentStage:Sprite;
  public var ship:Sprite;
  [Embed(source = '../lib/ship.swf')] private var swfShip:Class;
  public function Ship(parent:Sprite) {
   this.parentStage = parent;
   ship = new swfShip();
   parent.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
   parent.addChild(ship);
  }

  private function keyDown(e:KeyboardEvent):void {
   switch (e.keyCode) {
    case Keyboard.RIGHT:
     ship.x += 10;
    break;
    default:

    break;
   }
  }

 }

}

但是要从这个类之外访问精灵属性,我必须做类似的事情

ship = new Ship(this);
ship.ship.y = 320;
ship.ship.x = 320;

有没有办法直接访问属性?我尝试制作 this = new new swfShip() 但这不起作用。

【问题讨论】:

    标签: actionscript-3 sprite embedding


    【解决方案1】:

    如果你的 Ship 类不会扩展 Sprite(你真的不需要根据你的代码扩展它)最好的方法是:

     public class Ship{
      private var parentStage:Sprite;
      public var ship:Sprite;
      //...
      public function set x(newval: int):void{
         //check value
         ship.x = newval;
      }
    
      public function set y(newval: int):void{
         //check value
         ship.y = newval;
      }
    }
    

    【讨论】:

      【解决方案2】:

      您创建了扩展 Sprite 的类,但它只将其他 sprite(船)添加到给定的父级。它包含船,但它不是船本身。将 Ship addChild(ship) 设为自身,然后根据需要使用 new Ship()

      【讨论】:

        【解决方案3】:

        您可以添加方法setPosition(x:int, y:int) 来更轻松地执行此操作。 您也可以使用with 语句(详情请参阅here

        【讨论】:

          猜你喜欢
          • 2016-11-22
          • 1970-01-01
          • 1970-01-01
          • 2011-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多