开发者

Actionscrpipt 3 OOP

开发者 https://www.devze.com 2023-04-10 06:10 出处:网络
I am developing player which has several my own custom developed buttons which has their own classes. Also the player has its own class which is the main class and instansiate all the buttons I have.

I am developing player which has several my own custom developed buttons which has their own classes. Also the player has its own class which is the main class and instansiate all the buttons I have.

So this is a simple example of "has a" relationship i.e composition. I used to past a reference of the player trought every buttons constructor in order buttons to be able to access properties and methods from the players class. This method is working good, but the problem is it has a lot of duplicate code than needs to be added to every button class.

So I tried to workit out by using inheritance, i.e buttons extend the playes main class.

But this way although i declare all properties protected I get the final swf blank white. So there must a problem.

Am I doing the structure wrong or what? Any idea?

Here is a sample code

     public class Player extends MovieClip{

           protected var prop1:Number;
           protected var prop2:String;
           protected var playButton:PlayButton;
           ....

           public function Player(){

                 // with composition
                 playButton=new PlayBUtton(this);

                 // with inhgeritance
                 playButton=new PlayButton();

                 addC开发者_JAVA百科hild(playBUtton);

           } 

      }  

      //first implementation with composition
      public class PlayButton extends MovieCLip{

           public function PlayButton(player:Player){
                 //access Player trough player parameter
           }
      }

      //second implementation with inheritance
      public class PlayButton extends Player{

           public function PlayButton(){
                 //access Player trough inheritance

           }
      }


You could use the state design pattern like in the following example I made:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var player:Player = new Player();
            addChild(player);

        }// end function

    }// end class

}// end package

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;

internal class TextButton extends Sprite
{
    public function TextButton(text:String)
    {
        graphics.beginFill(0xFF0000);
        graphics.drawRect(0, 0, 100, 25);
        graphics.endFill();

        var textField:TextField = new TextField();
        textField.text = text;
        textField.mouseEnabled = false;
        textField.x = this.width / 2 - textField.textWidth /2;
        textField.y = this.height / 2 - textField.textHeight / 2;
        addChild(textField);

    }// end function

}// end class

internal interface IState
{
    function play():void
    function stop():void

}// end interface

internal class Player extends Sprite
{
    private var _playState:IState;
    private var _stopState:IState;
    private var _state:IState;
    private var _playButton:TextButton;
    private var _stopButton:TextButton;

    public function get playState():IState { return _playState }
    public function get stopState():IState { return _stopState }
    public function get state():IState { return _state }
    public function set state(state:IState):void { _state = state }

    public function Player()
    {
        _playState = new PlayState(this);
        _stopState = new StopState(this);
        _state = stopState;

        _playButton = new TextButton("PLAY");
        _playButton.addEventListener(MouseEvent.CLICK, onClick);
        addChild(_playButton);

        _stopButton = new TextButton("STOP");
        _stopButton.addEventListener(MouseEvent.CLICK, onClick);
        _stopButton.x = 110;
        addChild(_stopButton);

    }// end function

    private function onClick(e:MouseEvent):void
    {
        var textButton:TextButton = e.target as TextButton;

        switch(textButton)
        {
            case _playButton : _state.play(); break;
            case _stopButton : _state.stop(); break;

        }// end function

    }// end function

}// end class

internal class PlayState implements IState
{
    private var _player:Player; 

    public function PlayState(player:Player)
    {
        _player = player;

    }// end function

    public function play():void
    {
        trace("player already playing");

    }// end class

    public function stop():void
    {
        _player.state = _player.stopState;
        trace("stopping player");

    }// end function

}// end class

internal class StopState implements IState
{
    private var _player:Player; 

    public function StopState(player:Player)
    {
        _player = player;

    }// end function

    public function play():void
    {
        _player.state = _player.playState;
        trace("playing player");

    }// end function

    public function stop():void
    {
        trace("player already stopped");

    }// end function

}// end class
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号