AS3 Tile Collision平台游戏

时间:2014-11-19 08:51:39

标签: actionscript-3 flash collision tile

我是AS3的新手,不得不为学校做一个平台游戏,遗憾的是我的老师并没有那么大的帮助。关于如何在我的比赛中发生碰撞,我没有丝毫的线索。我一直在网上搜索一段时间,真的找不到解决方案。如果您知道任何可以帮助我取得进展的信息,请发表评论!

package  {

    import flash.display.*;
    import flash.events.*;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.geom.Rectangle;
    import flashx.textLayout.formats.BlockProgression;
    import flashx.textLayout.formats.BackgroundColor;


    public class jump extends MovieClip {

        //Var player
        var persoon:testSubject = new testSubject();
        var grond:Ground = new Ground();
        var backGround:Background = new Background();
        var health:Health = new Health();

        var mapWidth = 10;
        var mapHeight = 10;
        var tileSide = 50;
        var totalTiles = mapWidth * mapHeight;

        var accel:Number = 2;
        var speed:Number = 5;
        var jumpSpeed:Number = 0;
        var gravity:Number = 5; 

        var ground:Number = 745 - persoon.height;

        var nr:Number = 3;

        var sprint:Boolean = true;
        var shiftButton:Boolean = false;
        var leftButton:Boolean = false;
        var rightButton:Boolean = false;
        var upButton:Boolean = false;
        var downButton:Boolean = false;
        var isJumping:Boolean = true;

        //Array map
        var myMap:Array = [
           [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
           [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,1,1,1,1,1,1,1,1,0,0,0,1,3,2,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
           [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
           ];

        public function jump() {
            //Event listener up key
            stage.addEventListener(KeyboardEvent.KEY_DOWN, upKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, upKeyUp);
            stage.addEventListener(Event.ENTER_FRAME, moveChar);

            //backGround
            backGround.x = -1000;
            backGround.y = -1000;
            backGround.height = 2000;
            backGround.width = 6000;
            addChild(backGround);

            //Map
            for (var i:int=0; i<13; i++)
            {
             for (var u:int=0; u<36; u++){
              var cell:MovieClip = new tile();
              cell.gotoAndStop(myMap[i][u]+1);
              cell.x=tileSide*u
              cell.y=tileSide*i
              addChild(cell);
             }
            }

            //Player
            persoon.x = 450;
            persoon.y = 490;
            persoon.width = 50;
            persoon.height = 60;
            addChild(persoon);

            //Healthbar
            health.x = 10;
            health.y = 65;
            health.gotoAndStop(4);
            addChild(health);
        }

        //Spring function when key is pressed
        public function upKeyDown(e:KeyboardEvent):void {
            //Buttons  Left - Up - Right - Down - Shift
            if ( e.keyCode == 37 || e.keyCode == 65 ) {
                leftButton = true;
                trace(persoon.y);
            }
            if ( e.keyCode == 38 || e.keyCode == 87 ) {
                upButton = true;
                    if(!isJumping) {  
                        jumpSpeed = 31;  
                        isJumping = true;  
                    }
            }
            if ( e.keyCode == 39 || e.keyCode == 68 ) {
                rightButton = true;
            }
            if ( e.keyCode == 40 || e.keyCode == 83 ) {
                downButton = true;
            }
            if ( e.keyCode == 16 ) {
                shiftButton = true;
                if ( sprint == true ) {
                    speed = speed * accel;
                    sprint = false;
                }
            }
        }

        //Spring function when key is released
        public function upKeyUp(e:KeyboardEvent):void {
            //Buttons  Left - Up - Right - Down - Shift
            if ( e.keyCode == 37 || e.keyCode == 65 ) {
                leftButton = false;
            }
            if ( e.keyCode == 38 || e.keyCode == 87 ) {
                upButton = false;
            }
            if ( e.keyCode == 39 || e.keyCode == 68 ) {
                rightButton = false;
            }
            if ( e.keyCode == 40 || e.keyCode == 83 ) {
                downButton = false;
            }
            if ( e.keyCode == 16 ) {
                shiftButton = false;
                speed = 5;
                sprint = true;
            }
        }

        //Character Movement
        public function moveChar(event:Event):void {
            //Collision


            //Walk Left & Right
            if ( leftButton == true ) {
                if (persoon.x > 78) {
                    persoon.x -= speed;
                    health.x = persoon.x - 440;
                }
            } else if ( rightButton == true ) {
                if ( persoon.x < 1722 ) {
                    persoon.x += speed;
                    health.x = persoon.x - 440;
                }
            }

            //Jump
            if(isJumping) {
                health.y -= jumpSpeed;
                persoon.y -= jumpSpeed;  
                jumpSpeed -= 2;
            }

            if (persoon.y + gravity < ground) {
                persoon.y += gravity; 
                health.y += gravity;
                trace(persoon.y);
            } else {
                health.y = ground - 420;
                persoon.y = ground;  
                isJumping = false;
            }

            //Camera
            root.scrollRect = new Rectangle ( persoon.x - stage.stageWidth/2, persoon.y - 430, stage.stageWidth, stage.stageHeight );
        }
    }
}

3 个答案:

答案 0 :(得分:1)

hitTestObject()是扩展DisplayObject类的任何函数。它允许您检测两个对象是否正在接触或相交。

它的实施是公平的直接:

if (obj1.hitTestObject(obj2) == true)
{
    //hit
}

根据您的屏幕资产的实现方式,您还可以使用BitmapData.hitTest()来检测两个位图是否发生了冲突。这更精确,因为它允许您声明alph通道。您可以在here

上找到一个很好的教程

答案 1 :(得分:0)

如果你想拥有真正好的碰撞(物理),那么你最终需要一个像Box2D这样的物理引擎!但要进入它,你必须掌握一点学习曲线,但我保证绝对值得深入研究。

请注意:Box2D最初是用C ++开发的,但是有很多不同语言的端口,如AS3,Javascript,Java等。这就是为什么你有时必须参考原始文档,因为flash端口的文档非常好。

这里有一些很好的教程:

由于您正在构建平铺平台游戏,请查看tiled,它将通过手动编写地图为您服务。另外我建议你看一下melonjs一个很好的基于平铺的平台游戏引擎的Javascript。教程可以向您展示工作流程,闪存应该与您完全相似。如果您需要一个浏览器游戏并且不受闪存限制,我建议您切换到HTML5并使用melonjs ......

祝你好运!

答案 2 :(得分:0)

对于你的情况下非常简单的碰撞检测,我会假设你的瓷砖可以用方块表示,而玩家是一个矩形。鉴于这两点,您需要编写的新代码的主要部分是找到2个通用矩形的交集量(因为正方形也是一个矩形)。如果您愿意,可以在矩形类中使用内置方法,或者您可以编写自己的方法(它不是太复杂)。

接下来,在您的播放器移动中,您可以存储它并使用上面的矩形与矩形交叉方法,而不是立即将移动应用到播放器,检查播放器是否会运行到实心图块。如果他们这样做,那么通过限制允许移动的数量来停止/限制玩家进入它,然后将其应用到玩家。

这只是众多方式中的一种,甚至我在这里描述的内容也可以得到改善。