Frage

I'm trying to create a side scrolling platform game in Action script 3 by using flash CC and flash Develop.

Here is the code that I have implemented.

    private function startLevel1():void 
    {
        stage.removeEventListener(Event.ENTER_FRAME, mainGameLoop)

        //adds event listener to loop 
        stage.addEventListener(Event.ENTER_FRAME, level1)
    }



    private function level1(e:Event):void 
    {

        stage.focus = this



        processCollision(); 
        var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 );
        //trace( mem ); // eg traces “24.94MbLv1”   
        backBtn.addEventListener(MouseEvent.CLICK, fromLevtoStart)

        scrollwithPlayer();



    }

    private function scrollwithPlayer():void 
    {

    }


    private function processCollision():void 
    {




        if (ground.hitTestPoint(character.x, character.y, true))
        {
            //trace("hits ground");
            player.yGravity = 0
            player.touchingGround = true;
            character.y -= 1;
        }
        else
        {
            player.touchingGround = false;
        }
    /*  while (character.y > ground.y) 
        {
            character.y=ground.y;
            player.yGravity = 0;
            //character.incrementUp();
        }*/

        character.movementChar();
        character.keyListner();




    }

This code processes collision and things.

I have a class called player and this moves the player

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
/**
 * ...
 * @author Moynul Hussain
 */
public class player extends MovieClip 
{
    public static var yGravity:int = 0;
    public static var gravity:Boolean;
    public static var xSpeed:int;
    public var ySpeed:int;
    private var rightKey:Boolean;
    private var leftKey:Boolean;
    private var upKey:Boolean;      
    public static var touchingGround:Boolean;


    public function player() 
    {
        xSpeed = 3;
        ySpeed = 6;


        addEventListener(Event.ADDED_TO_STAGE, init)
        addEventListener(Event.REMOVED_FROM_STAGE, reset)

    }

    private function reset(e:Event):void 
    {
        //removeEventListener(Event.REMOVED_FROM_STAGE, reset);
        trace("removed");
        this.x = 300;
        this.y = 500;
        yGravity = 0;
        //do something
    }

    private function init(e:Event):void 
    {
        //removeEventListener(Event.ADDED_TO_STAGE, init);
        trace("added");
        this.x = 500;
        this.y = 400;


    }


    public function keyListner():void 
    {
        this.y += yGravity++;
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);                        
    }

    public function movementChar()
    {



        if (leftKey)
        {
            this.x -= xSpeed;
            this.gotoAndStop("run");
            this.scaleX = -1;
        this.x += stage.x ;
        }

        if (rightKey)
        {
            this.x += xSpeed;
            this.gotoAndStop("run");
            this.scaleX = 1;
        }

        if (upKey)
        {
            this.y -= 10;
            this.gotoAndStop("jump");
            //this.scaleX = -1;     
        }

        if (!upKey && !leftKey && !rightKey && touchingGround)
        {
            this.gotoAndStop("stop");
        }




    }       

    private function keyUp(e:KeyboardEvent):void 
    {
        if (e.keyCode == 37)
        {
            leftKey = false;
        }
        if (e.keyCode == 39)
        {
            rightKey = false;
        }   
        if (e.keyCode == 38)
        {
            upKey = false;
        }       
    }

    private function keyDown(e:KeyboardEvent):void 
    {
        if (e.keyCode == 37)
        {
            leftKey = true;
        }
        if (e.keyCode == 39)
        {
            rightKey = true;
        }
        if (e.keyCode == 38)
        {
            upKey = true;
        }
    }
}

}

My intentions is to scroll the level when the players moves right/ or left. What I don't want to do is move the level when the player presses control keys.

I have attempted to implement a vCam, but this makes the game very laggy, since vCam is fore animation, a I hear.

Sorry if this is a lot to take in.

War es hilfreich?

Lösung

Yeah Moynul, that's one way to do it, but if you want smoother results try this:

stage.addEventListener(Event.ENTER_FRAME,stage_x);
function stage_x(e:Event){
    var distance:Number = char.x-((stage.stageWidth/2)+offset);
    var ease:int = 5;
    var offset:int=10;
    if(distance<0){
        distance*=-1;
    }

    if(char.x<(stage.stageWidth/2)){
        var variable:int=distance/ease;
        ground.x+=variable;
        char.x+=variable;
    }
    if(char.x>(stage.stageWidth/2)){
        var variable2:int=distance/ease;
        ground.x-=variable2;
        char.x-=variable2;
    }   
}

The main character's instance name is the standard "char". This code will allow the "camera" view to ease to the player's location.

Andere Tipps

    private function scrollwithPlayer():void 
    {


        container.getChildByName("player").x = stage.stageWidth * 0.5;
        //character.x = stage.stageWidth / 2;//set player to the middle

        if (player.leftKey)
        {

            //scroll the container opposite way, but not player 
        }
        if (player.rightKey)
        {
            //scroll the container opposite way, but not player 
        }

    }

This is the solution i have come up with! I hope others agree

Updated answer

    private function scrollwithPlayer():void 
    {

        //which would should I have, i don't want to use the stages co-ordinates now. 
        container.getChildByName("player").x = stage.stageWidth* 0.5;
        //character.x = container.width/ 2;

        var i:int = 0;
        for (i = 0; i<container.numChildren - 1; i++)
        {
            //trace(container.getChildAt(i));
            if (player.leftKey)
            {
                //player.leftKey and right key goes to animation and flips the image 

                container.getChildAt(i).x += 3;
            }
            if (player.rightKey)
            {

                container.getChildAt(i).x -= 3;
            }               
        }
    }

the top code keeps the player set to the center of the stage.

You see I have a container (movieclip) that has the player, ground, and enemy inside.

what I'm doing is moving the container, but not the player?

What do you think Drake, will your code be smoother?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top