Author Topic: FlxTileBlock and collide  (Read 842 times)

emcyroyale

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
FlxTileBlock and collide
« on: Sun, Oct 14, 2012 »
Hello everyone,
   
    The problem that I am having is after I change the the x/y coordinate of a flxtileblock object, it won't collide with the player. The way the project is organized is that there is Block class that extends to flxgroup which holds the flxtileblock objects, a player class and the playstate. The collide statement is in playstate update().  I tried using super.update in all of these classes but it doesn't help. Anyone know how to remedy this problem?

pWEN

  • Cicadomorph
  • Member
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
    • Portfolio
Re: FlxTileBlock and collide
« Reply #1 on: Mon, Oct 15, 2012 »
Can you post some code?

emcyroyale

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: FlxTileBlock and collide
« Reply #2 on: Mon, Oct 15, 2012 »

These are the relevant classes


Playstate

Code: [Select]
package 
{
   import org.flixel.*;
   
   public class playState extends FlxState
   {
     
     
      protected var _player:Player;
      protected var _bound:Bounds;
     
      protected var _leveltext:FlxText; //used for showing the distance
      protected var _level:int;
     
     
     
     
     
      //////////////////////////////////////////////////////////////////////////////////
      override public function create():void
      {
         
         
         _player = new Player(92, 16);
         add(_player);
         
         _leveltext = new FlxText(0, 0, 100, "Hello");
         _leveltext.color = 0xffffffff;
         _leveltext.shadow = 0xff000000;
         _leveltext.scrollFactor.x = 0
         _leveltext.scrollFactor.y = 0;
         
         
         FlxG.camera.width = 208;
         FlxG.camera.height = 400;
         


         _bound = new Bounds(_player);  //**
         add(_bound);
         
         


         add(_leveltext);
         
         FlxG.worldBounds = new FlxRect(0, 0, FlxG.width, FlxG.height);
         
         FlxG.camera.follow(_player);
         FlxG.camera.deadzone = new FlxRect(50, 50, 120, 100);
         
         
      }
     
     
      //////////////////////////////////////////////////////////////////////////////////
      override public function update():void
      {
         textLevelUpdate();
         FlxG.collide(_player, _bound);
         super.update();
      }
     
     
      //////////////////////////////////////////////////shows the distance of the player
      private function textLevelUpdate():void
      {
         _level = Math.round(_player.y - 16);
         _leveltext.text = " " + _level;
      }
     
   }
}


Bounds Class: FlxGroup which holds the flxtileblock objects
Code: [Select]
package 
{
   import org.flixel.FlxG;
   import org.flixel.FlxGroup;
   import org.flixel.FlxTileblock;
   
   public class Bounds extends FlxGroup
   {
      protected var block:FlxTileblock;
      protected var player:Player;
     
      //////////////////////////////////////////////////////////////////////////////////
      override public function Bounds(play:Player)
      {
         player = play;
         maxSize = 50;
         
         for (var a : int = 0; a < FlxG.camera.height/16; a++)
         {
            block = new FlxTileblock(0, a * 16, 16, 16);
            block.makeGraphic(16, 16, 0xffFF4040);
            add(block);
            block = new FlxTileblock(FlxG.camera.width*15/16, a * 16, 16, 16);
            block.makeGraphic(16, 16, 0xff33CCCC);
            add(block);
         }
      }
     
     
      //////////////////////////////////////////////////////////////////////////////////
      override public function update():void
      {
         moveBlock();
         super.update();
      }
     
     
      //////////////////////////////////////////////////////////////////////////////////
      public function moveBlock():void
      {
         if ((members[0].y < player.y - 10 * 16))
         {
         
               members[0].y += 26 * 15;
               members[members.length] = (members[0]);
               members.splice(0, 1);
         }
      }
     
   }

}

Player Class
Code: [Select]
package 
{
   import org.flixel.*;
   
   public class Player extends FlxSprite
   {
     
      public function Player(x:int, y:int)
      {
         super(x, y);
         maxVelocity.x = 150;
         maxVelocity.y = 150;
         
         width = 10;
      }
      override public function update():void
      {
         controls();
         super.update();
      }
      private function controls():void
      {
         acceleration.x = 0;
         acceleration.y = 0;
         if (FlxG.keys.LEFT)
            acceleration.x -= 150*5;
         else if (FlxG.keys.RIGHT)
            acceleration.x += 150*5;
         else if (FlxG.keys.UP)
            acceleration.y -= 150*5;
         else if (FlxG.keys.DOWN)
            acceleration.y += 150*5;
         
      }
   }
}



mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: FlxTileBlock and collide
« Reply #3 on: Thu, Oct 18, 2012 »
i think because collide doesn't check collision if object moved using x or y. try using velocity/acceleration like when you moving player