krulos
04-14-2009, 07:02 PM
Hello everybody, Im trying to make a tetris style game.. Ive made a grid which allows the pieces to be flipped and moved. Ive also added a hittestObject to the bottom to allow the pieces to stop once they hit the bottom.
My problem is trying to define when a piece hits the bottom that it is now part of the wall - so blocks are ale to stack on top of it.
Im assuming its using a then statement based of an if.. but anyways is there anyone that can help me out?
Thanks :)
lordofduct
04-14-2009, 07:56 PM
Well each tetromino is constructed of rectangles.
So all I did was consider the whole board as a grid.
Then to check every falling tetromino, I'd loop through every sub block of the tetromino and see if it collided with the block making up the play field. If any did, stop dropping the tetromino, and add it to the grid that represents the playfield.
Personally I used an Array to represent this of length ( rows * columns ). Made for easy checking and pushing new blocks in. Also to delete rows only required splicing a set off values from it and redrawing from the array.
not sure if you'll make any sense of it as I'm not showing the classes for Tetromino or my custom events:
It's also a little dated... wrote it a while ago when I was first learning to code.
package game.utils
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;
import game.Game;
import game.events.CollisionEvent;
import game.events.CollisionObject;
public class DropManager extends EventDispatcher
{
protected var _game:Game;
protected var _timer:Timer;
public function DropManager( game:Game )
{
_game = game;
_timer = new Timer( 10000 );
}
public function init():void
{
_timer.start();
_game.stage.addEventListener( Event.ENTER_FRAME, update, false, 0, true );
_timer.addEventListener( TimerEvent.TIMER, pushRandom, false, 0, true );
}
public function stop():void
{
_timer.reset();
_game.stage.removeEventListener( Event.ENTER_FRAME, update);
_timer.removeEventListener( TimerEvent.TIMER, pushRandom );
}
public function reset():void
{
stop();
init();
}
protected function update(e:Event):void
{
var tetros:Array = _game.gameManager.tetrosClone;
while (tetros.length)
{
var tetromino:Tetromino = tetros.pop();
tetromino.shiftY( 1 );
var blocks:Array = tetromino.blocksClone;
if (!blocks.every( checkTetromino ))
{
_game.gameManager.removeTetromino( tetromino );
while (blocks.length)
{
var block:Block = blocks.pop();
var ix:int = Math.floor( block.x / Game.TILE_WIDTH );
var iy:int = Math.floor( block.y / Game.TILE_HEIGHT );
_game.blockGrid.setBlock( ix, iy, tetromino.type );
}
}
}
var rows:int = Game.GRID_HEIGHT;
var count:int = 0;
var rowArr:Array = new Array();
while( rows-- )
{
if ( _game.blockGrid.checkRow( rows ) )
{
_game.blockGrid.deleteRow( rows );
count++;
rowArr.push( rows )
rows++;
}
}
if (count)
{
var obj:CollisionObject = new CollisionObject();
obj.multiplier = count;
obj.rows = rowArr;
dispatchEvent( new CollisionEvent( CollisionEvent.DELETE_LINE, obj ) );
}
}
protected function checkTetromino(item:Block, index:int, array:Array):Boolean
{
var ix:int = Math.floor( item.x / Game.TILE_WIDTH );
var iy:int = Math.floor( item.y / Game.TILE_HEIGHT );
iy++;
if (iy >= _game.blockGrid.height) return false;
if ( _game.blockGrid.getBlock( ix, iy ) ) return false;
return true;
}
protected function pushRandom(e:TimerEvent):void
{
var arr:Array = new Array();
while( arr.length < 10 ) arr.push(0);
for (var i:int = 0; i < 5; i++)
{
arr[ Math.floor( Math.random() * 10 ) ] = (Game.NUM_TYPES + 1);
}
_game.blockGrid.pushRowBottom( arr );
}
}
}
you can see it here:
http://www.lordofduct.com/assets/flash/tetrisPong/Main.html
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.