flixel中的碰撞无法使用更新的tilemap

时间:2014-01-12 20:45:43

标签: actionscript-3 flash flixel

我一直在关注跳线教程,现在我已经到了第3部分。这是

的链接

参考:  [URL] http://chipacabra.blogspot.in/2010/12/project-jumper-part-3.html [/ URL]

对于第2部分,我已成功让我的玩家与地图发生碰撞但碰撞停止了,当我更新游戏以包含第3部分的说明时,我的角色又开始自由落下。我对代码进行了一些修改以便使程序编译(因为教程中编写的一些代码已经过时,甚至评论中写的代码也已过时)。这导致相信代码中可能缺少更多内容。我正在使用的地图的cvs和png文件是通过下载链接在第3部分末尾提供的源代码中包含的文件。

这是我的代码:

Playstate.as:

package com.chipacabra.Jumper
{
    import org.flixel.*;

    public class PlayState extends FlxState
    {
        [Embed(source = '../../../../levels/mapCSV_Group1_Map1.csv', mimeType = 

'application/octet-stream')]public var levelMap:Class;
        [Embed(source = "../../../../levels/mapCSV_Group1_Map1back.csv", mimeType = 

"application/octet-stream")]public var backgroundMap:Class;
        //[Embed(source = '../../../../art/tilemap.png')]public var levelTiles:Class;
        // This was the old art, not using it anymore.
        [Embed(source = "../../../../art/area02_level_tiles2.png")]public var levelTiles:Class;
        //This is the new art.
        public var map:FlxTilemap = new FlxTilemap;
        public var background:FlxTilemap = new FlxTilemap;
        public var player:Player; 

        override public function create():void
        {
            add(background.loadMap(new backgroundMap, levelTiles, 16, 16));
            background.scrollFactor.x = background.scrollFactor.y = .5;
            add(map.loadMap(new levelMap, levelTiles, 16, 16));
            add(player = new Player(10, 10));
            FlxG.camera.setBounds(0, 0, 1600, 800);
            FlxG.camera.follow(player, FlxCamera.STYLE_PLATFORMER);
            //FlxG.follow(player);
            //FlxG.worldBounds.x = 0;
            //FlxG.worldBounds.y = 0;
            //FlxG.worldBounds.width = 1600;
            //FlxG.worldBounds.height = 800;

            super.create();
        }

        override public function update():void 
        {
            super.update();
            FlxG.collide(map, player);
        }
    }
}

Player.as:

package com.chipacabra.Jumper 
{
    import org.flixel.*;
    /**
     * ...
     * @author A. Velitsky
     */
    public class Player extends FlxSprite
    {
        [Embed(source = "../../../../art/helmutguy.png")]public var Helmutguy:Class;
        protected static const RUN_SPEED: int = 80;
        protected static const GRAVITY: int = 300; //originaly 420
        protected static const JUMP_SPEED: int = 200;

        public function Player(X:int,Y:int):void
        {
            super(X, Y);
            loadGraphic(Helmutguy, true, true);
            addAnimation("walking", [1, 2], 12, true);
            addAnimation("idle", [0]);
            drag.x = RUN_SPEED * 8 
            // Drag is how quickly you slow down when you're not
            // pushing a button.  By using a multiplier, it will
            // always scale to the run speed, even if we change it.
            acceleration.y = GRAVITY;
            // Always try to push helmutguy in the direction of gravity
            maxVelocity.x = RUN_SPEED;
            maxVelocity.y = JUMP_SPEED;
        }
        public override function update(): void
        {
            super.update();
            acceleration.x = 0; 
            // Reset to 0 when no button is

            if (FlxG.keys.LEFT)
            {
                facing = LEFT;
                acceleration.x = -drag.x;
            }
            else if (FlxG.keys.RIGHT)
            {
                facing = RIGHT;
                acceleration.x = drag.x;
            }
            if (FlxG.keys.justPressed("UP") && !velocity.y)
            {
                velocity.y = -JUMP_SPEED;
            }
            //Animation
            if (velocity.x != 0) { play("walking"); }
            else if (!velocity.x) { play("idle"); }

            super.update();
        }
    }

}

Jumper.as:

package
{
    import org.flixel.*; //Allows you to refer to flixel objects in your code
    import com.chipacabra.Jumper.PlayState;
    [SWF(width = "640", height = "480", backgroundColor = "#000000")] //Set the size and color of the 

Flash file
    [Frame(factoryClass="Preloader")]
    public class Jumper extends FlxGame
    {
        public function Jumper()
        {
            super(320, 240, PlayState, 2); //Create a new FlxGame object at 320x240 with 2x pixels, 

then load PlayState
            FlxG.bgColor = 0x8DEBFC;
        }
    }
}

Preloader.as:

package  
{
    import org.flixel.system.FlxPreloader;
    /**
     * ...
     * @author A. Velitsky
     */
    public class Preloader extends FlxPreloader
    {

        public function Preloader() 
        {
            className = "Jumper";
            super();
        }

    }

}

编辑:嗯......我似乎发现我从评论到教程的代码几乎是正确的...唯一错误的是没有指定相机的视图样式。它似乎也是FlxG.camera.setBounds(0,0,1600,800);不需要调用...虽然我想知道为什么我想要使用FlxG.camera.setBounds();我可能会在飞机/宇宙飞船射击游戏中使用这个东西吗?

1 个答案:

答案 0 :(得分:2)

FlxG.camera.setBounds()设置关卡的边界,可用于告诉相机它在哪里并且不允许移动。如果setBounds小于实际世界尺寸,那么游戏将不会在该区域更新,相机也将停止移动。

在您的示例中,FlxG.camera.setBounds(0,0,1600,800)设置的静态宽度为1600,这可能超出您的需要,因此您不会发现任何影响相机。

我认为最好根据游戏的实际宽度设置界限。我的一些游戏代码看起来像这样:

//Set the max bounds of the stage
FlxG.worldBounds = new FlxRect (0,0,collisionMap.width-TILE_WIDTH,collisionMap.height);

//Attach the camera
FlxG.camera.setBounds(32,-64,FlxG.worldBounds.width-64,FlxG.worldBounds.height);
FlxG.camera.follow(player,FlxCamera.STYLE_PLATFORMER);

希望有所帮助!