1. package  
  2. {
  3.     import org.flixel.*;
  4.    
  5.     public class test extends FlxState
  6.     {
  7.         private var sprites:FlxGroup = new FlxGroup();
  8.        
  9.         override public function create():void
  10.         {
  11.             FlxG.bgColor = 0xff333333;
  12.            
  13.             const SPRITES_PER_ROW:uint = 15;
  14.             const SPRITE_SPACING:uint = 20;
  15.             var sprite:FlxSprite;
  16.             for (var i:uint = 0; i < 100; i++)
  17.             {
  18.                 sprite = new FlxSprite();
  19.                 sprite.makeGraphic(10, 10);
  20.                 sprite.x = 30 + SPRITE_SPACING * (i - FlxU.floor(i / SPRITES_PER_ROW) * SPRITES_PER_ROW);
  21.                 sprite.y = 30 + SPRITE_SPACING * FlxU.floor(i / SPRITES_PER_ROW);
  22.                 sprite.velocity.x = -100 + FlxG.random() * 200;
  23.                 sprite.velocity.y = -100 + FlxG.random() * 200;
  24.                 sprites.add(sprite);
  25.             }
  26.            
  27.             sprite = new FlxSprite();
  28.             sprite.makeGraphic(Main.WIDTH, 10, 0xffff0000);
  29.             sprite.immovable = true;
  30.             sprite.ID = 1;
  31.             sprites.add(sprite);
  32.            
  33.             sprite = new FlxSprite();
  34.             sprite.makeGraphic(Main.WIDTH, 10, 0xffff0000);
  35.             sprite.y = Main.HEIGHT - sprite.height;
  36.             sprite.immovable = true;
  37.             sprite.ID = 2;
  38.             sprites.add(sprite);
  39.            
  40.             sprite = new FlxSprite();
  41.             sprite.makeGraphic(10, Main.HEIGHT, 0xffff0000);
  42.             sprite.immovable = true;
  43.             sprite.ID = 3;
  44.             sprites.add(sprite);
  45.            
  46.             sprite = new FlxSprite();
  47.             sprite.makeGraphic(10, Main.HEIGHT, 0xffff0000);
  48.             sprite.x = Main.WIDTH - sprite.width;
  49.             sprite.immovable = true;
  50.             sprite.ID = 4;
  51.             sprites.add(sprite);
  52.            
  53.             add(sprites);
  54.         }
  55.  
  56.         override public function update():void
  57.         {
  58.             super.update();
  59.            
  60.             FlxG.overlap(sprites, sprites, handleOverlap);
  61.         }
  62.        
  63.         private function handleOverlap(A:FlxSprite, B:FlxSprite):void
  64.         {
  65.             if (!A.immovable || !B.immovable)
  66.             {
  67.                 if (A.immovable || B.immovable)
  68.                 {
  69.                     var wall:FlxSprite;
  70.                     var ball:FlxSprite;
  71.                     if (A.immovable) { wall = A; ball = B; }
  72.                     if (B.immovable) { wall = B; ball = A; }
  73.                     if (wall.ID == 1) { ball.velocity.y *= -1; ball.y = 10; }
  74.                     if (wall.ID == 2) { ball.velocity.y *= -1; ball.y = Main.HEIGHT - ball.height - 10; }
  75.                     if (wall.ID == 3) { ball.velocity.x *= -1; ball.x = 10; }
  76.                     if (wall.ID == 4) { ball.velocity.x *= -1; ball.x = Main.WIDTH - ball.width - 10; }
  77.                 }
  78.                 else
  79.                 {
  80.                     FlxObject.separate(A, B);
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }