Mark Pagan
Mark Pagan

Reputation: 1

AS3 Jumping (flying) issue

Ive searched around a bit but I couldnt find an answer I can work with.I probably didnt look hard enough. I have been out of school for about a year now and I came across a flash game we made in class. I was trying to fix some errors on it that Ive made to make it function properly. The issues Ive come across are a Jumping issue : The character can endlessly jump as if it is flying through the sky. The next error is the attack graphic doesnt play unless the player is in the sky. Im rusty with the coding and I am looking to get back into as3 and using the flash program. I have a "player.as" file, I will paste its code below. Any help is appreciated, thanks in advance.

package  {

import flash.display.MovieClip;
import CollisionObject;
import flash.events.Event;
import flash.events.TimerEvent; 
import flash.utils.Timer;

public class Player extends CollisionObject {
    private var xMovement:Number;
    public var isAttacking:Boolean;
    private var attackTimer:Timer = new Timer (500, 1);

    public function Player() {
        // constructor code
        trace("I am the player");
        xMovement = 0;
        isAttacking:false;

        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    } // end constructor

    private function enterFrameHandler(event:Event):void{
        this.x += xMovement;
    } //end function

    public function attack() {
        isAttacking = true;
        this.gotoAndStop("attack");
        attackTimer.addEventListener(TimerEvent.TIMER_COMPLETE, doneAttacking);
        attackTimer.start();
    }

    public function startJumping(){
        if (isJumping == false) {
            isJumping == true;
            this.gotoAndStop("jump");
            downwardVelocity = -20; 
        }
    }

    public function doneAttacking  (event:TimerEvent):void{
        isAttacking = false;
        this.gotoAndStop("stop");
    }

    public function moveLeft() : void{
        xMovement =-7;
        this.scaleX = -1;
        this.gotoAndStop("run");
        isRunning = true;
    } //end function

    public function moveRight() : void{
        xMovement =7;
        this.scaleX = 1;
        this.gotoAndStop("run");
        isRunning = true;
    } //end function

    public function standStill() : void{
        xMovement = 0;          
        isRunning = false;
    } //end function

    override public function positionOnLanding(){
        if(isRunning == true){
            this.gotoAndStop("run");
        }else{
            this.gotoAndStop("stop");
        } //end else
    } //end function
} // end class

Upvotes: 0

Views: 48

Answers (1)

Philarmon
Philarmon

Reputation: 1806

Your player is jumping endlessly because you never set the iSJumping to true. It should be

isJumping = true;

instead of

isJumping == true;

Attack is probably not working because you have an own frame for the attack and the frame is probably reset by the running or positionOnLanding ?

And please move your attackTimer event listener into the constructor, now a new event listener is created each time you are attacking, this leads to a memory leak and there is really no reason to do so.

Upvotes: 2

Related Questions