2010-12-18 66 views
0

我有下面的类:SWF嵌入后直接访问雪碧

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; 

是否有办法直接访问属性?我试过这个= new new swfShip()但这不起作用。

回答

1

如果您Ship类不会延长Sprite(你真的不需要根据自己的代码来扩展它)最好way'd是:

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; 
    } 
} 
0

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

0

您可以添加方法setPosition(x:int, y:int)以更轻松地完成此操作。 您也可以使用with发言(见例如here了解详细信息)