动作脚本3,侧滚动平台。

时间:2014-04-01 13:26:49

标签: actionscript-3 flash game-engine

我尝试使用Flash CC和flash Develop在Action脚本3中创建一个侧滚动平台游戏。

这是我实施的代码。

    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();




    }

此代码处理碰撞和事物。

我有一个名为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;
        }
    }
}

}

我的目的是在玩家向右/向左移动时滚动关卡。我不想做的是在玩家按下控制键时移动关卡。

我试图实现一个vCam,但是这使得游戏非常迟钝,因为vCam是前动画,我听说过。

很抱歉,如果需要这么做的话。

2 个答案:

答案 0 :(得分:1)

是的Moynul,这是一种方法,但如果你想要更顺畅的结果,试试这个:

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;
    }   
}

主角的实例名称是标准&#34; char&#34;。此代码将允许&#34;相机&#34;观看以方便玩家的位置。

答案 1 :(得分:0)

    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 
        }

    }

这是我提出的解决方案!我希望其他人同意

更新回答

    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;
            }               
        }
    }

顶部代码将玩家设置为舞台的中心。

你看我有一个容器(movieclip)里面有玩家,地面和敌人。

我正在做的是移动容器,而不是播放器?

您认为德雷克,您的代码会更顺畅吗?