package { import org.flixel.*; public class test extends FlxState { private var sprites:FlxGroup = new FlxGroup(); override public function create():void { FlxG.bgColor = 0xff333333; const SPRITES_PER_ROW:uint = 15; const SPRITE_SPACING:uint = 20; var sprite:FlxSprite; for (var i:uint = 0; i < 100; i++) { sprite = new FlxSprite(); sprite.makeGraphic(10, 10); sprite.x = 30 + SPRITE_SPACING * (i - FlxU.floor(i / SPRITES_PER_ROW) * SPRITES_PER_ROW); sprite.y = 30 + SPRITE_SPACING * FlxU.floor(i / SPRITES_PER_ROW); sprite.velocity.x = -100 + FlxG.random() * 200; sprite.velocity.y = -100 + FlxG.random() * 200; sprites.add(sprite); } sprite = new FlxSprite(); sprite.makeGraphic(Main.WIDTH, 10, 0xffff0000); sprite.immovable = true; sprite.ID = 1; sprites.add(sprite); sprite = new FlxSprite(); sprite.makeGraphic(Main.WIDTH, 10, 0xffff0000); sprite.y = Main.HEIGHT - sprite.height; sprite.immovable = true; sprite.ID = 2; sprites.add(sprite); sprite = new FlxSprite(); sprite.makeGraphic(10, Main.HEIGHT, 0xffff0000); sprite.immovable = true; sprite.ID = 3; sprites.add(sprite); sprite = new FlxSprite(); sprite.makeGraphic(10, Main.HEIGHT, 0xffff0000); sprite.x = Main.WIDTH - sprite.width; sprite.immovable = true; sprite.ID = 4; sprites.add(sprite); add(sprites); } override public function update():void { super.update(); FlxG.overlap(sprites, sprites, handleOverlap); } private function handleOverlap(A:FlxSprite, B:FlxSprite):void { if (!A.immovable || !B.immovable) { if (A.immovable || B.immovable) { var wall:FlxSprite; var ball:FlxSprite; if (A.immovable) { wall = A; ball = B; } if (B.immovable) { wall = B; ball = A; } if (wall.ID == 1) { ball.velocity.y *= -1; ball.y = 10; } if (wall.ID == 2) { ball.velocity.y *= -1; ball.y = Main.HEIGHT - ball.height - 10; } if (wall.ID == 3) { ball.velocity.x *= -1; ball.x = 10; } if (wall.ID == 4) { ball.velocity.x *= -1; ball.x = Main.WIDTH - ball.width - 10; } } else { FlxObject.separate(A, B); } } } } }