0

我正在尝试稍微重构我的代码(目前所有内容都在主类中)。我将我所有的播放器代码放在一个新的播放器类中。现在我尝试在主类中创建它的一个实例,但没有任何反应。我究竟做错了什么?

    var figur:Player;

    public function Main() {
        init();
    }

    private function init():void{
        figur = new Player();
        addChild(figur);
    }

播放器.as

package  {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.Event;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.display.LineScaleMode;
import flashx.textLayout.utils.CharacterUtil;

public class Player extends Sprite{

    public var line:Shape = new Shape();
    public var char = new CharSprite();
    public var _xStart:Number;
    public var _yStart:Number;
    public var _xZiel:Number;
    public var _yZiel:Number;var _mx:Number;
    public var _my:Number;
    public var _platformCollision:Boolean;
    const ACCELERATION:Number = 10;

    public function Main() {
        init();
    }

    private function init():void{
        _platformCollision = false;
        _xStart = 40;
        _yStart = 350;
        _xZiel = 40;
        _yZiel = 350;

        line.x = 40;
        line.y = 350;
        line.graphics.lineStyle(2,0x0000FF,1, false, LineScaleMode.NONE);
        line.graphics.lineTo(1,1);
        addChild(line);
        char.x = 40;
        char.y = 350;
        addChild(char);
        addEventListener(Event.ENTER_FRAME, mainLoop);
        stage.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
    }

    private function mainLoop(e:Event):void{
        checkCollisionPlatform();
        drawHook();


    }

    private function onClick(e:Event):void{
        if(_xStart == _xZiel && _yStart == _yZiel){
            _platformCollision = false;
            _xZiel = mouseX;
            _yZiel = mouseY;
            _mx = (mouseX - _xStart)/ Math.sqrt(Math.pow((mouseX - _xStart),2)+Math.pow((mouseY - _yStart),2));
            _my = (mouseY - _yStart)/ Math.sqrt(Math.pow((mouseX - _xStart),2)+Math.pow((mouseY - _yStart),2));
        }
    }

    private function drawHook():void{
        trace(_xStart+" "+_yStart);
        line.graphics.lineStyle(2,0x0000FF,1, false, LineScaleMode.NONE);
        line.graphics.lineTo(1,1);
        if(_xZiel>_xStart){
            if((_xStart+_mx*ACCELERATION)>_xZiel){
                _xStart = _xZiel;
            }
        }else if(_xZiel<_xStart){
            if((_xStart+_mx*ACCELERATION)<_xZiel){
                _xStart = _xZiel;
            }
        }
        if(_yZiel>_yStart){
            if((_yStart+_my*ACCELERATION)>_yZiel){
                _yStart = _yZiel;
            }
        }else if(_yZiel<_yStart){
            if((_yStart+_my*ACCELERATION)<_yZiel){
                _yStart = _yZiel;
            }
        }
        if(_xStart != _xZiel && _yStart != _yZiel){
            _xStart += _mx * ACCELERATION;
            _yStart += _my * ACCELERATION;
            line.scaleX += _mx * ACCELERATION;
            line.scaleY += _my * ACCELERATION;
        }
        if(_xStart == _xZiel && _yStart == _yZiel && _platformCollision == false){
            line.graphics.clear();
            line.x = 40;
            line.y = 350;
            line.scaleX = 1;
            line.scaleY = 1;
            _xStart = 40;
            _yStart = 350;
            _xZiel = 40;
            _yZiel = 350;
        }
    }

    private function checkCollisionPlatform():void{
        if(MovieClip(parent).balken1.hitTestPoint(_xStart,_yStart)){
            MovieClip(parent).textfield.text = "1 getroffen";
            _xZiel = _xStart;
            _yZiel = _yStart;
            _platformCollision = true;
            retractHook();
        }
        if(MovieClip(parent).balken2.hitTestPoint(_xStart,_yStart)){
            MovieClip(parent).textfield.text = "2 getroffen";
            _xZiel = _xStart;
            _yZiel = _yStart;
            _platformCollision = true;
            retractHook();
        }
    }

    private function retractHook():void{
        //trace(line.scaleX/ACCELERATION +" "+line.scaleY/ACCELERATION+" "+_platformCollision);
        line.rotation = 180;
        line.x = _xZiel;
        line.y = _yZiel;
        line.scaleX -= _mx * ACCELERATION;
        line.scaleY -= _my * ACCELERATION;

        char.x += _mx * ACCELERATION
        char.y += _my * ACCELERATION;

        if ((Math.floor((line.scaleX/ACCELERATION))) == 0 && (Math.floor((line.scaleY/ACCELERATION))) == 0){
            trace("Klappt");
            _platformCollision = false;
            line.rotation = 0;
        }

    }

}

}

4

2 回答 2

0

在播放器类中,调用播放器构造函数中的 init 函数。

我认为你应该在玩家添加到舞台时调用这一行,而不是在 init 函数中

 stage.addEventListener(MouseEvent.MOUSE_DOWN, onClick);

尝试这个

private function init():void {
  //your code

   this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
}

private function onAddToStage(e:Event):void {
     stage.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
}
于 2013-07-10T01:09:01.767 回答
0

首先,您的构造函数名称错误。其次,您可以收听 addedtostage 并在那里调用 init,而不是在 init 中收听 addedtostage (其他方式)

所以,改变这个

public function Main() {
    init();
}

//rename Main to Player
public function Player() {
    //wait untill added to stage
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(e:Event):void {
    //remove addedtostage event
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    //call init
    init();
}
于 2013-07-10T08:38:45.923 回答