AS3角色动作 - 让角色跳跃

时间:2014-07-08 12:30:54

标签: actionscript-3 flash flash-cs5

我正在为一个学校项目制作一个(非常)简单的平台游戏,并在不同的在线投资中提供一些帮助。

有几天我一直试图让角色跳跃。我没有得到任何错误,如果我用trace语句测试它,语句会出现在输出中。我尝试了几种不同的方式但却无法实现的方法。

这是我为我的角色使用的代码:

 package  {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.geom.Point;


    public class ana extends MovieClip {

        private var _vx:Number;
        private var _halfWidth:Number;
        private var _previousKey:uint;
        public var speler:ana;

        // variabelen die bij de jump functie horen
        var upKeyDown:Boolean = false;
        var mainJumping:Boolean = false;
        var jumpSpeedLimit:int = 15;
        var jumpSpeed:Number = jumpSpeedLimit;

        public function ana() {
            this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        }

        private function onAddedToStage(event:Event):void {

            this._vx = 0; 
            this._halfWidth = this.width / 2;
            this._previousKey = Keyboard.RIGHT;

            this.stop();// toegevoegd zodat de loop cycle van de speler stopt aan het begin van het spel

            /*EVENT LISTENERS*/
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);

            this.addEventListener(Event.ENTER_FRAME, onEnter);
            this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
        }

        private function onRemovedFromStage(event:Event):void {
            this.removeEventListener(Event.ENTER_FRAME, onEnter);
            this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);

        }
        private function onKeyboardDown(event:KeyboardEvent):void {
            //zorgt ervoor dat het character de juiste richting op rent

            var currentKey:uint = event.keyCode;

                //Voor de linker toets
                if (event.keyCode == Keyboard.LEFT){
                    // Als de huidige toets niet gelijk is aan vorige, dan wordt het character gedraait
                    if (currentKey != _previousKey) {
                        _previousKey = currentKey;
                        this.scaleX *= -1;
                    }
                    // De run cycle wordt aangeroepen, de positie van het character verplaatst naar links met X frames per seconden
                    loopRun();
                    _vx = -8;
                }
                // voor de rechter toets
                if (event.keyCode == Keyboard.RIGHT){
                    if (currentKey != _previousKey) {
                        _previousKey = currentKey;
                        this.scaleX *= -1;
                    }
                    // zelfde als vorige if statement, alleen -X wordt X om de richting van links naar rechts te veranderen
                    loopRun();
                    _vx = 8;
                }
                /*Het is me niet gelukt om de speler te laten springen*/
                if (event.keyCode == Keyboard.SPACE) {
                    mainJump();
                    trace("spring");
                }

                        /*Functie om de speler te laten springen
                        werkt helaas niet... krijg ook geen error messages...
                        heb verschillende manieren en codes geprobeert...
                        Deze code komt uit de volgende tutorial: http://goo.gl/S5VRd0 */

                        function mainJump():void{

                            if(!mainJumping){

                                mainJumping = true;
                                jumpSpeed = jumpSpeedLimit*-1;
                                this.y += jumpSpeed;
                            } else {

                                if(jumpSpeed < 0){
                                    jumpSpeed *= 1 - jumpSpeedLimit/75;
                                    if(jumpSpeed > -jumpSpeedLimit/5){
                                        jumpSpeed *= -1;
                                    }
                                }
                                if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
                                    jumpSpeed *= 1 + jumpSpeedLimit/50;
                                }
                                this.y += jumpSpeed;

                                if(this.y >= stage.stageHeight - this.height){
                                    mainJumping = false;
                                    this.y = stage.stageHeight - this.height;
                                }
                            }
                        }
        }




        // bron: Flash AS3 Simple Game 04: player class http://www.youtube.com/watch?v=u4dwuAUYrkg
        private function onKeyboardUp(event:KeyboardEvent):void {
            if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT) {
                _vx = 0;
                gotoAndStop(1);

            }
        }
        private function onEnter(event:Event):void {
            this.x += _vx;
            checkStageBoundaries();

        }
        private function loopRun():void {
            //zorgt ervoor dat de run cycle van het character blijft 'loopen'
            if (currentFrame == 12) {
                gotoAndPlay(1);
            }
            else {
                play();
            }

        }
        private function checkStageBoundaries():void {
            //controleert of de speler zich buiten de grenzen van de stage begint en blokkeerd de speler als hij de stage wil verlaten
            //van volgende tutorial overgenomen: Flash as3 simple game 04: Player Class http://www.youtube.com/watch?v=u4dwuAUYrkg
            if (this.x - _halfWidth < 0) {
                this.x = 0 + _halfWidth;
            }
            else if (this.x + _halfWidth > stage.stageWidth) {
                this.x = stage.stageWidth - _halfWidth;
            }
        }


    }

}

由于作业的性质,代码在荷兰语中被大量评论。

以防这是我的主要课程:

package  {

import flash.display.MovieClip;
import flash.events.*;
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;




public class Main extends MovieClip {


    /*VARIABELEN*/
    public var startScreen:StartScreen; //beginscherm
    public var playScreen:PlayScreen; // speel scherm
    public var startBtn:StartBtn; // start knop die naar het speel scherm gaat
    public var speler:ana = new ana; // speler

    /*variabelen voor de diamantjes*/
    public var diamant:Diamond = new Diamond;
    public var diamant2:Diamond = new Diamond;
    public var diamant3:Diamond = new Diamond;
    public var diamant4:Diamond = new Diamond;

    public var info:infoTxt = new infoTxt;

    public function Main() {
    //Start scherm op stage & startknop
    show_startScreen(); // roept functie aan om het startscherm te laten zien
    startBtn.addEventListener(MouseEvent.CLICK, playGame); //voegt muisevent toe aan de startknop
    }

    // speel scherm
    public function show_startScreen() { // functie voor startscherm
        /*variabelen voor de mc's*/
        startScreen = new StartScreen(this);
        startBtn = new StartBtn;
        /*toevoegen startscherm en startknop*/
        addChild(startScreen); 
        addChild(startBtn);
        /*positionering startknop*/
        startBtn.y = 230;
        startBtn.x = 400;
    }



    public function playGame(event:MouseEvent) { //roept functie voor de startknop aan
        play_game();    
        }



    public function play_game() { //deze code wordt uitgevoerd nadat de playGame functie is aangeroepen
        playScreen = new PlayScreen(this); //variabel voor het speel scherm

        if (startScreen) { //deze code zorgt ervoor dat het beginscherm wordt verwijderd
            removeChild(startScreen);
            startScreen = null;
        }

        addChild(playScreen); //voegt speelscherm toe
        addChild(speler); //voegt speler toe
        addChild(info); // voegt de informatie text toe

        /* positionering info text*/
        info.x = 20;
        info.y = 20;

        /*positionering speler*/
        speler.y = 400;
        speler.x = 0;

        /*toevoegen en positioneren van diamantjes, de y as staat al gedefinieerd in de Diamanond class*/
        addChild(diamant);
        diamant.x = 450;

        addChild(diamant2);
        diamant2.x = 570;

        addChild(diamant3);
        diamant3.x = 690;

        addChild(diamant4);
        diamant4.x = 810;

        // event listener voor de speler, die wordt aangeroepen bij elk frame
        speler.addEventListener(Event.ENTER_FRAME, diamondCollision);

        // Collision functie, die ervoor zorgt dat de diamantjes verdwijnen als de speler ze aanraakt
        // over hitTestObject staat heel veel info op internet, hier heb ik geen speciefieke tutorial voor gebruikt
        // heb hitTestObject functie ook eerder gebruikt voor de groeps game

        function diamondCollision (event:Event):void {
            if (diamant != null) {// als diamant er nog is
                if (speler.hitTestObject(diamant)){// als de speler de diamant aanraakt
                removeChild(diamant);// verwijder de mc
                diamant = null; // zorgt ervoor dat het niet herhaald kan worden
            }
            }

            /*Hetzelfde voor de andere 3 diamantjes*/
            if (diamant2 != null) {
                if (speler.hitTestObject(diamant2)) {
                    removeChild(diamant2);
                    diamant2 = null;
                }
            }

            if (diamant3 != null) {
                if (speler.hitTestObject(diamant3)) {
                    removeChild(diamant3);
                    diamant3 = null;
                }
            }

            if (diamant4 != null) {
                if (speler.hitTestObject(diamant4)) {
                    removeChild(diamant4);
                    diamant4 = null;
                }
            }

        }

    }




    }






}

我将swf文件上传到swfcabin,所以你可以看到其余的工作正常:http://www.swfcabin.com/open/1404819729

任何人都可以帮助我让我的角色跳跃吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的jumpSpeed更改应位于enterframe监听器onEnter中,而且,您应将mainJump()代码重定位到那里,因为重力应该每帧一次。只有跳转初始化代码应保留在mainJump函数中,即检查是否跳跃,如果没有跳转,则指定y速度并离开函数。

 private function onEnter(event:Event):void {
        this.x += _vx;
        if (mainJumping) {
            if(jumpSpeed < 0){
                jumpSpeed *= 1 - jumpSpeedLimit/75;
                if(jumpSpeed > -jumpSpeedLimit/5){
                     jumpSpeed *= -1;
                }
            }
            if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
                jumpSpeed *= 1 + jumpSpeedLimit/50;
            }
            this.y+=jumpSpeed;
            if(this.y >= stage.stageHeight - this.height){
                mainJumping = false;
                this.y = stage.stageHeight - this.height;
                jumpSpeed=0;
            }
      }
      checkStageBoundaries();
    }

    function mainJump():void{

                        if(!mainJumping){

                            mainJumping = true;
                            jumpSpeed = jumpSpeedLimit*-1;
                            this.y += jumpSpeed;
                        } // enough!
    }