Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3.0使用数组创建边界_Actionscript - Fatal编程技术网

Actionscript 3.0使用数组创建边界

Actionscript 3.0使用数组创建边界,actionscript,Actionscript,目前,我正在进行一个重建mario级别的项目,我的问题是,当我不硬编码bottomLimit(下限,当前设置为400)时,mario最终将崩溃 另一件我不太清楚的事情是,我如何移动我的隐形块来创建地板边界以容纳地板。选择的关卡是《超级马里奥兄弟3》中的堡垒关卡,如果这有助于描述我想用它做什么的话 有几个。作为我的代码的文件,我将把我的麻烦文件和我的冲突代码放在一起 package { import flash.display.MovieClip; import flash.events.Eve

目前,我正在进行一个重建mario级别的项目,我的问题是,当我不硬编码bottomLimit(下限,当前设置为400)时,mario最终将崩溃

另一件我不太清楚的事情是,我如何移动我的隐形块来创建地板边界以容纳地板。选择的关卡是《超级马里奥兄弟3》中的堡垒关卡,如果这有助于描述我想用它做什么的话

有几个。作为我的代码的文件,我将把我的麻烦文件和我的冲突代码放在一起

package {

import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.media.Sound;


public class FortressMap extends MovieClip
{
    private var _mario:SmallMario;
    private var vx:Number = 0;
    private var vy:Number = 0;
    private var _ceiling:Array = new Array();
    private var _floor:Array = new Array();
    public const accy:Number = 0.20;
    public const termv:Number = 15;
    public var onGround:Boolean;
    public var bottomLimit:Number;

    public function FortressMap()
    {
        addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

        _mario = new SmallMario();
        addChild(_mario);
        _mario.x = 50;
        _mario.y = 400;

        //Creating the blocks for the floor
        createFloor(16, 416);

        //Creating the blocks for the ceiling
        createCeiling(16, 352);
}

    private function createFloor(xPos:Number, yPos:Number):void
    {
        var floor:Floor = new Floor();
        addChild(floor);
        floor.x = xPos;
        floor.y = yPos;
        floor.height = 16;
        _floor.push(floor);
        floor.visible = false;
    }

    private function createCeiling(xPos:Number, yPos:Number):void
    {
        var ceiling:Ceiling = new Ceiling();
        addChild(ceiling);
        ceiling.x = xPos;
        ceiling.y = yPos;
        ceiling.height = 16;
        _ceiling.push(ceiling);
        ceiling.visible = false;
    }

    private function addedToStageHandler(event:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        addEventListener(Event.ENTER_FRAME, frameHandler);
        addEventListener(Event.REMOVED_FROM_STAGE, removeStageHandler);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
    }

    private function frameHandler(event:Event):void
    {

        _mario.x +=  vx;
        _mario.y +=  vy;


        if (_mario.x < 16)
        {
            _mario.x = 16;
        }

        vy +=  accy;

        for (var i:int = 0; i < _ceiling.length; ++i)
        {
            Collision.block(_mario, _ceiling[i]);
        }
        for (var j:int = 0; j < _floor.length; ++j)
        {
            Collision.block(_mario, _floor[j]);
        }

        bottomLimit = 400;

        if(_mario.y >= bottomLimit)
        {
            _mario.y = bottomLimit;
            vy = 0;
            onGround = true;

        }
        else
        {
            onGround = false;
        }           
    }

    private function keyDownHandler(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT)
        {
            vx = -5;
            return;
        }
        if (event.keyCode == Keyboard.RIGHT)
        {
            vx = 5;
            return;
        }
        if (event.keyCode == Keyboard.UP)
        {
            if(onGround == true)
            {
                vy = -5;
                trace("My people need me!");
            }
            return;
        }
        if (event.keyCode == Keyboard.DOWN)
        {
            //vy = 5;
            return;
        }
    }

    private function keyUpHandler(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
        {
            vx = 0;
            return;
        }
        if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
        {
            //vy = 0;
            return;
        }
    }

    private function removeStageHandler(event:Event):void
    {
        removeEventListener(Event.ENTER_FRAME, frameHandler);
        removeEventListener(Event.REMOVED_FROM_STAGE, removeStageHandler);
        stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
    }
}
包{
导入flash.display.MovieClip;
导入flash.events.Event;
导入flash.ui.Keyboard;
导入flash.events.KeyboardEvent;
导入flash.media.Sound;
公共类FortressMap扩展了MovieClip
{
私人马里奥:斯莫尔马里奥;
私有变量vx:Number=0;
私有变量vy:Number=0;
私有变量上限:数组=新数组();
私有变量:数组=新数组();
公用工程量:数量=0.20;
公共施工条件V:数量=15;
公共变量:布尔值;
公共限制:数量;
公共功能地图()
{
addEventListener(Event.ADDED_TO_STAGE,AddedStatageHandler);
_马里奥=新的小马里奥();
addChild(_mario);
_mario.x=50;
_马里奥y=400;
//为楼板创建块
createFloor(16416);
//为天花板创建块
创造上限(16352);
}
私有函数createFloor(xPos:Number,yPos:Number):void
{
可变楼层:楼层=新楼层();
addChild(楼层);
地板x=xpo;
楼层y=yPos;
楼层高度=16;
_推(地板);
floor.visible=假;
}
私有函数create天花板(xPos:Number,yPos:Number):void
{
var天花板:天花板=新天花板();
addChild(天花板);
上限x=xpo;
上限y=yPos;
天花板高度=16;
_天花板。推(天花板);
天花板可见=假;
}
私有函数AddedStatageHandler(事件:event):void
{
removeEventListener(Event.ADDED_TO_STAGE,addedStatageHandler);
addEventListener(Event.ENTER_FRAME,frameHandler);
addEventListener(Event.REMOVED_FROM_STAGE,removeStageHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydowHandler);
stage.addEventListener(KeyboardEvent.KEY\u UP,keyUpHandler);
}
私有函数frameHandler(事件:事件):void
{
_mario.x+=vx;
_mario.y+=vy;
如果(_mario.x<16)
{
_mario.x=16;
}
vy+=accy;
对于(变量i:int=0;i<\u天花板长度;++i)
{
碰撞块(_mario,_天花板[i]);
}
对于(变量j:int=0;j<_floor.length;++j)
{
碰撞块(_mario,_floor[j]);
}
下限=400;
如果(_mario.y>=下限)
{
_mario.y=下限;
vy=0;
onGround=正确;
}
其他的
{
onGround=假;
}           
}
私有函数keyDownHandler(事件:KeyboardEvent):void
{
if(event.keyCode==Keyboard.LEFT)
{
vx=-5;
返回;
}
if(event.keyCode==Keyboard.RIGHT)
{
vx=5;
返回;
}
if(event.keyCode==Keyboard.UP)
{
如果(onGround==真)
{
vy=-5;
trace(“我的人需要我!”);
}
返回;
}
if(event.keyCode==Keyboard.DOWN)
{
//vy=5;
返回;
}
}
私有函数keyUpHandler(事件:KeyboardEvent):void
{
if(event.keyCode==Keyboard.LEFT | | event.keyCode==Keyboard.RIGHT)
{
vx=0;
返回;
}
if(event.keyCode==Keyboard.UP | | event.keyCode==Keyboard.DOWN)
{
//vy=0;
返回;
}
}
私有函数removeStageHandler(事件:event):void
{
removeEventListener(Event.ENTER_FRAME,frameHandler);
removeEventListener(Event.REMOVED_FROM_STAGE,removeStageHandler);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keydowHandler);
stage.removeEventListener(KeyboardEvent.KEY\u UP,keyUpHandler);
}
}
包装 { 导入flash.display.Sprite

public class Collision
{
    static public var collisionSide:String = ""; 

    public function Collision()
    {
    }
    static public function block(r1:Sprite, r2:Sprite):Boolean
    {
        var isBlocked:Boolean;
        //Calculate the distance vector
        var vx:Number 
            = (r1.x + (r1.width / 2)) 
            - (r2.x + (r2.width / 2));

        var vy:Number 
            = (r1.y + (r1.height / 2)) 
            - (r2.y + (r2.height / 2));

        //Check whether vx 
        //is less than the combined half widths
        if(Math.abs(vx) < r1.width / 2 + r2.width / 2)
        {
            //A collision might be occurring! Check 
            //whether vy is less than the combined half heights
            if(Math.abs(vy) < r1.height / 2 + r2.height / 2)
            {
                //A collision has ocurred! This is good!

                //Find out the size of the overlap on both the X and Y axes
                var overlap_X:Number 
                = r1.width / 2 
                    + r2.width / 2 
                    - Math.abs(vx);

                var overlap_Y:Number 
                = r1.height / 2 
                    + r2.height / 2 
                    - Math.abs(vy);

                //The collision has occurred on the axis with the
                //*smallest* amount of overlap. Let's figure out which
                //axis that is

                if(overlap_X >=  overlap_Y)
                {
                    //The collision is happening on the X axis
                    //But on which side? _v0's vy can tell us 
                    if(vy > 0)
                    {
                        collisionSide = "Top";

                        //Move the rectangle out of the collision
                        r1.y = r1.y + overlap_Y;
                        //r1 is being blocked
                        isBlocked = true;
                    }
                    else
                    {
                        collisionSide = "Bottom";

                        //Move the rectangle out of the collision
                        r1.y = r1.y - overlap_Y;
                        //r1 is being blocked
                        isBlocked = true;
                    }
                }
                else
                {
                    //The collision is happening on the Y axis
                    //But on which side? _v0's vx can tell us 
                    if(vx > 0)
                    {
                        collisionSide = "Left";

                        //Move the rectangle out of the collision
                        r1.x = r1.x + overlap_X;
                        //r1 is being blocked
                        isBlocked = true;
                    }
                    else
                    {
                        collisionSide = "Right"; 

                        //Move the rectangle out of the collision
                        r1.x = r1.x - overlap_X;
                        //r1 is being blocked
                        isBlocked = true;
                    }
                }
            }
            else
            {
                //No collision
                collisionSide = "No collision";
                //r1 is not being blocked
                isBlocked = false;
            }
        }
        else
        {
            //No collision
            collisionSide = "No collision";
            //r1 is not being blocked
            isBlocked = false;
        }
        return isBlocked;
    }
}
公共类冲突
{
静态公共var collisionSide:String=“”;
公共功能冲突()
{
}
静态公共功能块(r1:Sprite,r2:Sprite):布尔值
{
变量isBlocked:Boolean;
//计算距离向量
var vx:编号
=(r1.x+(r1.x/2))
-(r2.x+(r2.width/2));
变量vy:数字
=(r1.y+(r1.H/2))
-(r2.y+(r2.H/2));
//检查vx
//小于组合半宽度
如果(数学绝对值(vx)private function createFloor(xPos:Number, yPos:Number):void
{
    var floor:Floor = new Floor();
    addChild(floor);
    floor.x = xPos;
    floor.y = yPos;
    floor.height = 16;
    _floor.push(floor);
    floor.visible = false;

    // set bottom limit here
    bottomLimit = yPos;
}
    if(_mario.y >= Floor(_floor[0]).y)
    {
        _mario.y = Floor(_floor[0]).y;
        vy = 0;
        onGround = true;

    }
    else
    {
        onGround = false;
    }