0

我一直在关注关于跳线的教程,现在我已经到了第 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);
        }
    }
}

播放器.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();
        }
    }

}

跳线.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;
        }
    }
}

预加载器.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(); 我可能会在飞机/宇宙飞船射击游戏中使用这个东西?

4

1 回答 1

3

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

希望有帮助!

于 2014-12-22T19:40:03.243 回答