平台游戏中的等级进展? (Actionscript 3)

时间:2013-11-29 00:49:08

标签: actionscript-3

我已经在Platformer上工作了几个星期,而且我似乎无法让关卡升级。我在一个方框上使用基本的hitTest来表示从第1帧(第1级)到第2帧(第2级)的移动,但是一旦该字符出现在退出框中,我就会收到以下错误

  

TypeError:错误#1009:无法访问空对象引用的属性或方法。   在LevelProg_fla :: MainTimeline / loop()

我能做什么让它移动到下一帧(关卡)?如果它有帮助,我的代码是在一个框架而不是一个类。基本上,我用于级别进展的唯一代码是

 if (player.hitTestObject(exit))
{
    gotoAndPlay("levelTwo");
}

with" levelTwo"是下一帧的名称(下一级)。游戏的其余部分代码如下(原谅它的混乱,我更专注于让这个糟糕的事情发挥作用:b)

import flash.events.KeyboardEvent;
import flash.events.Event;

//some variables to track the player's speed
var speedX = 0;
var speedY = 0;
player.height = 80.0;
player.width = 60.0;

//loop through all the platform objects to generate the level
var level:Array = new Array();
for (var i=0; i<numChildren; i++)
{
if (getChildAt(i) is platform)
{
    level.push(getChildAt(i).getRect(this));
}
}

//make variables to store key states
var kUp = false;
var kDown = false;
var kLeft = false;
var kRight = false;

//listen for key presses and releases 
stage.addEventListener(KeyboardEvent.KEY_DOWN, kD);
stage.addEventListener(KeyboardEvent.KEY_UP, kU);
function kD(k:KeyboardEvent) 
{
if (k.keyCode==37) kLeft=true;
if (k.keyCode==38) kUp=true;
if (k.keyCode==39) kRight=true; 
if (k.keyCode==40) kDown=true;
}
function kU(k:KeyboardEvent) 
{
if (k.keyCode==37) kLeft=false;
if (k.keyCode==38) kUp=false;
if (k.keyCode==39) kRight=false;
if (k.keyCode==40) kDown=false;
}


//Make a looping function
addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event)
{
//lateral movement checks
if (kLeft) 
{
    speedX=-10;
}
else if (kRight)
{
    speedX=10;
} 
else 
{
    speedX*=0.5; //friction to slow player down (kinda like mario)
}
//does not stop the character 
//move player based on the above
player.x+=speedX;

//animations
if (speedX > 0)
{
trace("right pressed");
//player.gotoAndPlay("walkRight");
}
if (speedX < 0)
{
trace("left pressed");
//player.gotoAndPlay("walkLeft");
}
//sidewards hit tests
for (i=0; i<level.length; i++)
{
    if (player.getRect(this).intersects(level[i]))
    {
        if (speedX > 0) ////moving right collision and stuffs
            { 
                player.x = level[i].left-player.width/2;
            }
        if (speedX < 0) ////moving left collision and stuffs
            { 
                player.x = level[i].right+player.width/2;
            }
        //RIGHTIO
        speedX = 0 //kills the speed
    }
}
//vertical checks
speedY+=1;
player.y+=speedY;
var jumpable = false
//vertical hitTests
//note that it's the same as the x code but just changed like 5 things.
    for (i=0; i<level.length; i++)
    {
        if (player.getRect(this).intersects(level[i]))
        {
            if (speedY > 0) ////moving down collision and stuffs
            { 
                player.y = level[i].top-player.height/2;
                speedY=0;
                jumpable = true;
            }
            if (speedY < 0) ////moving up collision and stuffs
            { 
                player.y = level[i].bottom+player.height/2;
                speedY*=-0.5; //bounces off cielings
            }
            //RIGHTIO
            speedX = 0 //kills the speed
        }
    }
    //jumps if possible
    if (kUp && jumpable) 
    {
        speedY=-15;
    }
    //gravity might help}

    //move player with camera function/code/script/tired
    this.x=-player.x+(stage.stageWidth/2); //centers the player
    this.y=-player.y+(stage.stageWidth/2); //centers the player

//Progresses the player to level 2
if (player.hitTestObject(exit))
{
    gotoAndPlay("levelTwo");
}
}

0 个答案:

没有答案