Collision in flixel not working with updated tilemap

517 views Asked by At

I have been following the tutorial on jumper and I am now up to part 3. Here is the link for

referencing: [url]http://chipacabra.blogspot.in/2010/12/project-jumper-part-3.html[/url]

For part 2, I have successfully made my player collide with the map but the collision stopped and my character started free falling again when I updated the game to include instructions from part 3. I made some modifications to the code in order to make the program compile (since some of the code written in the tutorial is out of date, and even the code written in the comments was out of date). Which leads to believe there's something more that might be missing in the code. The cvs and png files for the maps that I am using are those which are included in the source code made available at the end of part 3 through a download link.

Here is the code I have:

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

    }

}

Edit: Um...I seem to have found out that the code I got from the comment to the tutorial...was almost correct...the only thing that was wrong was that the camera's view style was not specified. It also seems that FlxG.camera.setBounds(0, 0, 1600, 800); does not needs to be called... Although I would like to know why I would want to use FlxG.camera.setBounds(); I this something I would use perhaps in an airplane/spaceship shooter game?

1

There are 1 answers

0
dinorider On

FlxG.camera.setBounds() sets the boundaries of the level, which can be used to tell the camera where it is and is not allowed to move. If the setBounds are smaller than the actual world dimensions, then the game will not update in that area and the camera will stop moving as well.

In your example, FlxG.camera.setBounds(0, 0, 1600, 800) sets up a static width of 1600, which is probably more than you need, so you don't really see any effects on the camera.

I think it's best to set your bounds based on the actual width of your game. Some of my game code looks something like this:

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

Hope that helps!