Hi! I want to make a game with some blocks but not tetris and i need a hand . You know the minigame from professor layton?
In this one you have to change the position of the spheres, in mine you have to put a wooden block out of the game screen.
I started studying programming just a month ago and i'm a noob yet. The first test i did i used this code to move a block: [code] public function DocumentClass() { block1 = new Block(100, 200); addChild ( block1 ); block1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); block1.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); } public function mouseDownHandler(evt:MouseEvent):void { var object = evt.target; // we should limit dragging to the area inside the canvas object.startDrag();
} public function mouseUpHandler(evt:MouseEvent):void { var obj = evt.target; obj.stopDrag(); } [/Code]
Whit this code i can move the block everywhere on the screen: that's not what i want. How can i write a code that give me the possibility of moving on lines set in advance? I explain better: the blocks have to move in only four directions: up, down, left and right and have to stop or don't move when collide with another block.
I know, i'm noob! That's why i'm here! Thanks in advance
This is my plan: I will divide the screen in an immaginary Grid: if it is 400x400 my grid will be divided bye ten 40x40 dowels.
I will have one array of boolean types with ten elements: true if the dowel is taken, false if it's free. Everytime a player moves a block i take mouseX and mouseY when he click on the block and then mouseX and mouseY when he release the block. Then i will subtract them from the old x and y obtaining xMovement and yMovement. Then i will do this:
if (Math.abs(xMovement) > Math.abs(yMovement)) { //moves on x if (movimentoX<0) { direction = 'left'; } else { direction = 'right'; } } else if (Math.abs(movimentoX) < Math.abs(movimentoY) // muove su Y { //moves on the y if (movimentoY<0) { direction = 'up'; } else { direction = 'down'; } }
Then, after obtaining the direction variable i'll check in what dowel the block is. Then i'll take the direction (imagine the block is on dowel 3) and..
If the direction is "up" i will subtract 10, if it's "down" i will add 10, if it's "left" i will subtract 1, if it's "right" i will add 1. Then i'll make a check if the move is legal and if it is, i will move the dowel on the number i found.
if (Math.abs(xMovement) > Math.abs(yMovement)) { //moves on x if (xMovement<0) { direction = 'left'; } else { direction = 'right'; } } else if (Math.abs(movimentoX) < Math.abs(movimentoY) // muove su Y { //moves on the y if (yMovement<0) { direction = 'up'; } else { direction = 'down'; } }
Hmm, I don't quite understand how to play or what I am supposed to do. Are you trying to map that one block to movement with the arrow keys? Whatever the problem is, just keep at it.