![]() |
|
||||||||||
|
|
|
|||||
|
Регистрация: Jan 2011
Сообщений: 247
|
Делая игру дошел до точки где надо хорошо обдумать а правильно ли я начал делать ...?
вот собственно вопросы по коду: 1)задумался о соприкосновении блоков. 2)также линия сгорания. 3)центральный куб, который будет осью при rotation,можно конечно через if else, ну это куча лишнего кода... package elements { import flash.display.*; import flash.events.*; import flash.text.*; import flash.utils.* public class Blocks extends Sprite { //////////////////////////////////////////////////////////////////// // Public properties //////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// // Private properties //////////////////////////////////////////////////////////////////// private var tetrisblock1: Sprite; private var tetrisblock2: Sprite; private var tetrisblock3: Sprite; private var tetrisblock4: Sprite; private var n1:int; private var move:Number = 20; private var tf:TextField; private var key_left:Boolean = false; private var key_right:Boolean = false; private var key_Up:Boolean = false; private var blockButton: Sprite; private var delay:uint = 400; private var repeat:uint ; private var myTimer:Timer = new Timer(delay, repeat); private var statusTextField:TextField = new TextField(); private var inputTextField:TextField = new TextField(); //////////////////////////////////////////////////////////////////// // Public methods //////////////////////////////////////////////////////////////////// public function Blocks() { this.tetrisblock1 = this.makeSquare(); this.tetrisblock2 = this.makeSquare(); this.tetrisblock3 = this.makeSquare(); this.tetrisblock4 = this.makeSquare(); this.blockButton = new Sprite(); var field: TextField = new TextField(); field.defaultTextFormat = new TextFormat('calibri', 12); field.text = 'ReBlock'; field.selectable = false; this.blockButton.addChild(field); var trigger: Sprite = new Sprite(); trigger.graphics.beginFill(0xff00ff, 0.3); trigger.graphics.drawRect( -3, -3, field.textWidth + 8, field.textHeight + 8); trigger.graphics.endFill(); this.blockButton.addChild(trigger); this.blockButton.buttonMode = true; this.blockButton.x = 245; this.blockButton.y = 150; this.addChild(this.blockButton); myTimer.start(); myTimer.addEventListener(TimerEvent.TIMER, blockDown); myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler); this.blockButton.addEventListener(MouseEvent.CLICK, this.this_onAddedToBlock); addEventListener(Event.ADDED_TO_STAGE, this_onAddedToBlock); addEventListener(Event.ENTER_FRAME, moveBlocks); } public function rect():void { this.tetrisblock1.x = 0 this.tetrisblock1.y = 0 this.tetrisblock2.x = 20 this.tetrisblock2.y = 0 this.tetrisblock3.x = 0 this.tetrisblock3.y = 20 this.tetrisblock4.x = 20 this.tetrisblock4.y = 20 } public function line():void { this.tetrisblock1.x = 0 this.tetrisblock1.y = 0 this.tetrisblock2.x = 20 this.tetrisblock2.y = 0 this.tetrisblock3.x = 40 this.tetrisblock3.y = 0 this.tetrisblock4.x = 60 this.tetrisblock4.y = 0 } public function snake():void { this.tetrisblock1.x = 0 this.tetrisblock1.y = 0 this.tetrisblock2.x = 20 this.tetrisblock2.y = 0 this.tetrisblock3.x = 20 this.tetrisblock3.y = 20 this.tetrisblock4.x = 40 this.tetrisblock4.y = 20 } public function plus():void { this.tetrisblock1.x = 0 this.tetrisblock1.y = 0 this.tetrisblock2.x = 20 this.tetrisblock2.y = 0 this.tetrisblock3.x = 40 this.tetrisblock3.y = 0 this.tetrisblock4.x = 20 this.tetrisblock4.y = 20 } public function horse():void { this.tetrisblock1.x = 0 this.tetrisblock1.y = 0 this.tetrisblock2.x = 20 this.tetrisblock2.y = 0 this.tetrisblock3.x = 40 this.tetrisblock3.y = 0 this.tetrisblock4.x = 40 this.tetrisblock4.y = 20 } //////////////////////////////////////////////////////////////////// // Private methods //////////////////////////////////////////////////////////////////// private function makeSquare() : Sprite { var spr: Sprite = new Sprite(); spr.graphics.beginFill(0xff00ff, 0.3); spr.graphics.drawRect(0, 0, 20, 20); spr.graphics.endFill(); this.addChild(spr); return spr; } private function blockDown(e:TimerEvent):void { repeat--; trace('таймера осталось : ' +repeat); if (tetrisblock1.y < 380 && tetrisblock2.y < 380 && tetrisblock3.y < 380 && tetrisblock4.y < 380 ) { tetrisblock1.y += move; tetrisblock2.y += move; tetrisblock3.y += move; tetrisblock4.y += move; trace('двигаюсь');} if ( tetrisblock4.y >= 380 && tetrisblock4.y <=400) { myTimer.removeEventListener(TimerEvent.TIMER, blockDown); trace('застыл');} } private function completeHandler(e:TimerEvent):void { trace('конец!'); } //////////////////////////////////////////////////////////////////// // Listeners //////////////////////////////////////////////////////////////////// private function this_text():void { var tf: TextField = new TextField(); this.addChild(tf); var format: TextFormat = new TextFormat('Calibri', 18, 0xcccccc, true); tf.defaultTextFormat = format; tf.width = 200; tf.height = 40; tf.text = 'fail'; tf.selectable = false; tf.x = 240; tf.y = 120 tf.width = tf.textWidth + 4; tf.height = tf.textHeight + 4; } private function this_onAddedToBlock(e:Event) : void { removeEventListener(Event.ADDED_TO_STAGE, this_onAddedToBlock); myTimer.addEventListener(TimerEvent.TIMER, blockDown); this.n1 = int(Math.round(Math.random() * 100)); if (n1 >= 0 && n1 < 20) { rect(); } if (n1 >= 20 && n1 < 40) { line(); } if (n1 >= 40 && n1 < 60) { snake(); } if (n1 >= 60 && n1 < 80) { plus(); } if (n1 >= 80 && n1 <= 100) { horse(); } } private function key_down(e:KeyboardEvent):void { trace('нажал'+e.keyCode); if (e.keyCode == 37) { key_left = true; } if (e.keyCode == 39) { key_right = true; } if (e.keyCode == 38) { key_Up = true; } } private function key_up(e:KeyboardEvent):void { trace('отпустил'+e.keyCode); if (e.keyCode == 37) { key_left = false;; } if (e.keyCode == 39) { key_right = false; } if (e.keyCode == 38) { key_Up = false; } } private function moveBlocks(e:Event):void { moveBlock(); stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down); stage.addEventListener(KeyboardEvent.KEY_UP, key_up); } private function moveBlock():void { if (tetrisblock1.x >=20 && tetrisblock2.x > 10 && tetrisblock3.x > 10 && tetrisblock4.x > 10 ) { if (key_left) { tetrisblock1.x -= move; tetrisblock2.x -= move; tetrisblock3.x -= move; tetrisblock4.x -= move; trace('left'); }} if (tetrisblock1.x < 220&& tetrisblock2.x < 220&& tetrisblock3.x < 220&& tetrisblock4.x < 220){ if (key_right){ tetrisblock1.x += move; tetrisblock2.x += move; tetrisblock3.x += move; tetrisblock4.x += move; trace('right'); }} if ( line || snake || plus || horse ) { if (key_Up) { tetrisblock1.rotation += 90; tetrisblock2.rotation += 90; tetrisblock3.rotation += 90; tetrisblock4.rotation += 90; } } } } } Сори что все в одном классе как все доделаю сразу все распределю... P.S.код начеркал за денек,двигаюсь в правильном направлении ? Последний раз редактировалось GrafMine; 05.01.2011 в 18:56. |
|
|||||
|
Регистрация: Jan 2011
Сообщений: 247
|
Up нужны советы.
|
|
|||||
|
По коду сложно судить, плохо читается. Лучше расскажите на словах как делаете, а там уж подскажем.
__________________
Тут мужик танцует и поёт про флэш |
|
|||||
|
Регистрация: Jan 2011
Сообщений: 247
|
Проще говоря я делаю тетрис, во Flash Develop я остановился над задачей появления второго блока на сцене и их "столкновние".
Зарание спасибо. |
|
|||||
|
Я бы всё поле делал как двумерный массив. В массиве есть 2 значения: 0 и 1. 1 - есть блок, 0 - нет блока. Блок движется вниз. Например, блок состоит из 4 "элементов", при движении вниз мы проверяем, может ли каждый блок пройти вниз. Если может - сдвигаем. Если не может - значит записываем в этот двумерный массив единицами места, где блок находится и проверяем, не образовалась ли линия, заполненная единицами.
__________________
Тут мужик танцует и поёт про флэш |
|
|||||
|
Регистрация: Mar 2007
Сообщений: 545
|
Цитата:
Думаю такие игры, как и платформеры проще делать через двумерный массив, чем отслеживать столкновения и тп. |
|
|||||
|
Регистрация: Jan 2011
Сообщений: 247
|
Цитата:
package { import flash.display.*; import flash.events.*; public class Main extends Sprite { private var world:Array = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]; //=Фигуры private var figure1:Array = [ [1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]; //======= private var container_world:MovieClip; private var container_figure:MovieClip; public function Main():void { addEventListener(Event.ADDED_TO_STAGE,initGame); } private function initGame(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE,initGame); container_world = new MovieClip ; container_figure = new MovieClip ; showWorld(); showFigures(); } private function showWorld():void { addChild(container_world); for (var i:int=0; i < world.length; i++) { for (var j:int=0; j < world[i].length; j++) { var world_tail:Block=new Block; world_tail.x=i * 20; world_tail.y=j * 20; container_world.addChild(world_tail); } } } private function showFigures():void { addChild(container_figure); for (var i:int=0; i < figure1.length; i++) { for (var j:int=0; j < figure1[i].length; j++) { var blok:BlockF=new BlockF; blok.x=i * 20; blok.y=j * 20; container_figure.addChild(blok); } } } } } 1)как сделать проверку в массиве на 1 или 0 ? 0 = белый блок, 1 = черный блок...? Немного полазил в инете и немного преобразовал под себя, вот что вышло с массивом: for (var j in figure1) { for (var i in figure1[j]) { var square:Shape = new Shape(); var squareMC:MovieClip = new MovieClip(); if (figure1[j][i] == 0) { square.graphics.beginFill(0x9CD55E); square.graphics.drawRect(0, 0, 20, 20); square.graphics.endFill(); } else if (figure1[j][i] == 1) { square.graphics.beginFill(0x000000); square.graphics.drawRect(0, 0, 20, 20); square.graphics.endFill(); } squareMC.addChild(square); squareMC.x =i * 20; squareMC.y =j * 20; addChild(squareMC); } } 2)как массив сдвинуть с места? Последний раз редактировалось GrafMine; 09.01.2011 в 03:49. |
|
|||||
|
Регистрация: Aug 2008
Адрес: Рязань
Сообщений: 723
|
или через битмапу, тоже удобно
__________________
low + |
|
|||||
|
Регистрация: Jan 2011
Сообщений: 247
|
Up нужны советы.
|
|
|||||
|
Регистрация: Mar 2007
Сообщений: 545
|
1)
2) Зачем сдвигать? Забиваете старое положение фигуры 0, новое 1 http://code.google.com/p/bigroom/wiki/TetrisGameEngine http://wonderfl.net/c/3jal Последний раз редактировалось andrew911; 13.01.2011 в 01:11. |
![]() |
![]() |
Часовой пояс GMT +4, время: 14:14. |
|
|
« Предыдущая тема | Следующая тема » |
|
|